import os
import gpflow
from sklearn import preprocessing
import numpy as np
import gzip
import pickle
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1'  # 这是默认的显示等级，显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'  # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'  # 只显示 Error

# read data
with gzip.open('./data/predict.pkl.gz') as fp:
    predict = np.array(pickle.load(fp))

with gzip.open('./data/start.pkl.gz') as fp:
    start = np.array(pickle.load(fp))

# clean data
start[start == -1.0E20] = 'nan'
start = start[:, ~np.all(np.isnan(start), axis=0)]

predict[predict == -1.0E20] = 'nan'
predict = predict[:, ~np.all(np.isnan(predict), axis=0)]

# preprocess
start = preprocessing.scale(start)
predict = preprocessing.scale(predict)
predict = predict-start

M = 3
start = start[0:10, 0:M]
predict = predict[0:10, 0:M]

m = gpflow.saver.Saver().load('./model/gp.mdl')

# construct x
X_augmented = np.vstack(
    (np.hstack((start[:, 0:1], np.zeros_like(start[:, 0:1]))), np.hstack((start[:, 1:2], np.ones_like(start[:, 1:2]))))
)
for i in range(M-2):
    X_augmented = np.vstack(
        (X_augmented, np.hstack((start[:, i+2:i+3], (i+2)*np.ones_like(start[:, i+2:i+3]))))
    )
# construct y
Y_augmented = np.vstack(
    (np.hstack((predict[:, 0:1], np.zeros_like(start[:, 0:1]))), np.hstack((predict[:, 1:2], np.ones_like(start[:, 1:2]))))
)
for i in range(M-2):
    Y_augmented = np.vstack(
        (Y_augmented, np.hstack((predict[:, i+2:i+3], (i+2)*np.ones_like(start[:, i+2:i+3]))))
    )

print(X_augmented)
pY, pYv = m.predict_f(X_augmented)
print(pY)
# print(pYv)
print(Y_augmented)
# print(pY - Y_augmented[:, 0])


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from users.models import CustomUser

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column, Field

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = UserChangeForm.Meta.fields

class CustomUserUpdateForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'avatar', 'phone','email', 'address')

    def __init__(self, *args, **kwargs):
        super(CustomUserUpdateForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Column('first_name', css_class='form-group col-md-6 mb-0'),
                Column('last_name', css_class='form-group col-md-6 mb-0'),
                css_class='form-row'
            ),
            Row(
                Column('phone', css_class='form-group col-md-6 mb-0'),
                Column('email', css_class='form-group col-md-6 mb-0'),
                css_class='form-row'
            ),
            Row(
                Column('address', css_class='form-group col-md-12 mb-0'),
                css_class='form-row'
            ),
            'avatar',

            Submit('submit', 'Update Information')
        )


##Imports##
import numpy as np
import networkx as nx
from networkx import normalized_laplacian_matrix
import sys

class featureSelection():

    '''Initialization creates a list of all of the graphs removing any with
    less than the correct number of vertices '''

    def __init__(self, graph_list):
        X = []
        len_ = [len(row) for row in graph_list]
        ind = np.argmax(np.bincount(len_))
        if ind >= len(graph_list):
            ind = 0
        cutoff = len_[ind]
        for row in graph_list:
            if len(row) == cutoff:
                X.append(row)
        self.graph_list = X
        self.all = len(X)
    '''The following flattens the adjacency matrix of each graph.
    It takes only the top diagnal of values and returns a feature
    matrix with those values as features. '''

    def adjacencyMatrixFeatures(self):
        featureMat = []
        for G in self.graph_list:
            adjMat = nx.to_numpy_matrix(G)
            topHalf = np.triu_indices(len(adjMat))
            row = adjMat[topHalf]
            featureMat.append(row)
        featureMat = np.vstack(featureMat)
        return featureMat

    '''The following calculated the eigenvector centrality
    of each vertex and returns the largest average value
    features for the number of features selected'''

    def eigenvectorCentrality(self, numFeatures = 0):
        #computes eigen vector centrality for the graph and returns a np matrix containing
        #eigenvector centrality values for each graph
        if numFeatures == 0:
                numFeatures = self.all
        featureMatrix = []
        for graph in self.graph_list:
            centrality = nx.eigenvector_centrality(graph)
            eigenVectorDict = dict(sorted((v, '{:0.2f}'.format(c)) for v, c in centrality.items()))
            list_ = list(eigenVectorDict.values())
            featureMatrix.append(list_)
        featureMatrix = np.stack(featureMatrix).astype("float")
        # Select highest feature centrality values
        averages = np.mean(featureMatrix, axis=0)
        featInds = np.argsort(averages)[-numFeatures:]
        return featureMatrix[:,featInds]

    '''A helper function to calculate the variance of each feature'''

    def getVariance(self, featureMatrix):
        varValues = []
        for i in range(0, featureMatrix.shape[1]):
            values = featureMatrix[:,i]
            meanSample = np.mean(values)
            sumSample = 0
            for j in range(0, featureMatrix.shape[0]):
                sumSample += (featureMatrix[j,i] - meanSample)**2
            varValues.append(sumSample/(featureMatrix.shape[0] - 1))
        return varValues

    ''' A helper function to calculate the eigenvalues of
    each graph laplacian '''

    def _calculate_eigenvalues(self):
        # Return eigenvalues of the Laplacian
        eigvals = []
        for G in self.graph_list:
            L = normalized_laplacian_matrix(G)
            e = list(np.linalg.eigvals(L.A))
            eigvals.append(e)
        return eigvals

    '''The following returns the eigenvalues of each
    graph as its features. It returns the highest
    variance features'''

    def calc_eigval_feature_matrix(self, numFeatures = 0):
        if numFeatures == 0:
            numFeatures = self.all
        X = self._calculate_eigenvalues()
        X = np.matrix(X)
        varValues = self.getVariance(X)
        featInds = np.argsort(varValues)[-numFeatures:]
        return X[:,featInds]

    '''The following functions create a feature matrix containing
    the one and two khop locality values for each matrix.
    The functions with 1 are for networkx version 1.
    Each selects the highest variance number of features'''

    def khop_locality_1(self, G):
        embed = []

        for node in G.nodes_iter():

            one_hop = list(nx.single_source_shortest_path_length(G, node, cutoff=1).keys())
            two_hop = list(nx.single_source_shortest_path_length(G, node, cutoff=2).keys())

            embed += len(G.subgraph(one_hop).edges()), len(G.subgraph(two_hop).edges())

        return embed

    def getKhopFeatMat1(self, numFeatures = 0):
        if numFeatures == 0:
            numFeatures = self.all
        M = []
        for i in range(0, len(self.graph_list)):
            if self.graph_list[i] is not None:
                embed = self.khop_locality_1(self.graph_list[i])
            if embed is not None:
                M.append(embed)
        X = np.matrix(M)
        varValues = self.getVariance(X)
        featInds = np.argsort(varValues)[-numFeatures:]
        return X[:,featInds]

    def khop_locality(self, G):
        embed = []
        for node in G.nodes:
            one_hop = list(nx.single_source_shortest_path_length(G, node, cutoff=1).keys())
            two_hop = list(nx.single_source_shortest_path_length(G, node, cutoff=2).keys())
            embed += len(G.subgraph(one_hop).edges()), len(G.subgraph(two_hop).edges())
        return embed

    def getKhopFeatMat(self, numFeatures = 0):
        if numFeatures == 0:
            numFeatures = self.all
        M = []
        for i in range(0, len(self.graph_list)):
            if self.graph_list[i] is not None:
                embed = self.khop_locality(self.graph_list[i])
            if embed is not None:
                M.append(embed)

        # Preserve only the necessary features
        X = np.matrix(M)
        varValues = self.getVariance(X)
        featInds = np.argsort(varValues)[-numFeatures:]
        return X[:,featInds]

    ''' Remove features that are all zero '''
    def removeZeroColumns(self, mat):
        all_zeros = []
        for i in range(mat.shape[1]):
            if sum(mat[:,i]) == 0:
                all_zeros.append(i)
        mat = np.delete(mat, all_zeros, 1)
        return mat


from rest_framework import serializers

from fix_the_news.communications import models

class CommunicationReadOnlySerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Communication
        fields = (
            'id',
            'text',
            'title',
            'type',
        )
        read_only_fields = fields


import wx
import os
from os.path import basename
from include.utils import  dict2

class ast_Call(dict2):
	def __init__(self, *args, **kwargs):
		dict2.__init__(self, *args, **kwargs)
	def setTree(self, tree):
		self.tree=tree
	def set_Call(self, **kwargs):
		key, nodes = kwargs['key'], kwargs['nodes']

		last0=self.append_Call(key= key,nodes= nodes, lineno=kwargs['lineno'])
		if last0: self.tree.Expand(last0)
	def append_Call(self, **kwargs):
		key, nodes, root = kwargs['key'], kwargs['nodes'], kwargs.get('root', None),
		lineno = kwargs['lineno']
		tree=self.tree
		if not root: return
		asrt (root, 'Root is not defined for leaf "1" nodes')
		for node in [node for node in nodes if node.col_offset==1]:
			#print(lineno, node.lineno, node.end_lineno)
			if key != node.name:
				btn = wx.Button(tree.GetMainWindow(), -1, node.name)
				btn.SetSize(btn.GetBestSize())
				if 1:

					tree.gen_bind(wx.EVT_BUTTON, btn, tree.OnEditFile, (tree.pyscript.script_loc, node.lineno))
				last1 = tree.AppendItem(root, node.name, wnd=btn)
			elif hasattr(node, 'names'):
				label = ','.join(node.names)
				if hasattr(node, 'module'):
					label = '%s [%s]' % (node.module, label)
				last1 = tree.AppendItem(root, label)
		return last1


# Makes a basic python plot of time series position
# Takes in a namedTuple with the data.
# Makes a basic plot.

import matplotlib.pyplot as plt
import collections
import gps_ts_functions
import gps_seasonal_removals
import offsets
import gps_io_functions
import gps_input_pipeline

# Parameters for controlling plotting
Parameters = collections.namedtuple("Parameters", ['station', 'outliers_remove', 'outliers_def',
                                                   'earthquakes_remove', 'offsets_remove', 'seasonals_remove',
                                                   'seasonals_type', 'datasource', 'refframe', 'data_config_file']);

# Types of seasonal options:
#   lssq: fits seasonals and linear trend by least squares inversion.
#  notch: removes the 1-year and 6-month components by notch filter.
#  grace: uses GRACE loading model interpolated between monthly points where available,
#  and linear inversion where not available.
#    stl: uses a pre-computed look-up table for STL time series

def view_single_station(station_name, offsets_remove=1, earthquakes_remove=0, outliers_remove=0, seasonals_remove=0,
                        starttime=None, endtime=None, outliers_def=15,
                        seasonals_type='lssq', datasource='pbo', refframe='NA',
                        data_config_file="data_config.txt", outdir="single_plots/"):
    MyParams = configure(station_name, offsets_remove, earthquakes_remove, outliers_remove, outliers_def,
                         seasonals_remove,
                         seasonals_type, datasource, refframe, data_config_file);
    [myData, offset_obj, eq_obj] = input_data(MyParams.station, MyParams.datasource, MyParams.refframe,
                                              MyParams.data_config_file);
    [updatedData, detrended] = compute(myData, offset_obj, eq_obj, MyParams, starttime, endtime);
    single_ts_plot(updatedData, detrended, MyParams, outdir);

# -------------- CONFIGURE ------------ #
def configure(station, offsets_remove, earthquakes_remove, outliers_remove, outliers_def,
              seasonals_remove, seasonals_type, datasource, refframe, data_config_file):
    # outliers_def : mm away from median filter.
    # refframe : itrf or nam
    # offsets_remove, earthquakes_remove, outliers_remove, seasonals_remove : booleans
    # seasonals type : lssq, nldas, gldas, grace, lsdm, shasta, stl, notch
    # datasource : unr, cwu, pbo, nmt, usgs
    MyParams = Parameters(station=station, outliers_remove=outliers_remove, outliers_def=outliers_def,
                          earthquakes_remove=earthquakes_remove,
                          offsets_remove=offsets_remove, seasonals_remove=seasonals_remove,
                          seasonals_type=seasonals_type,
                          datasource=datasource, refframe=refframe, data_config_file=data_config_file);
    print("------- %s --------" % station);
    print(
        "Viewing station %s, earthquakes_remove=%d, outliers_remove=%d, seasonals_remove=%d, datasource=%s, refframe=%s"
        % (station, earthquakes_remove, outliers_remove, seasonals_remove, datasource, refframe));
    return MyParams;

# ----------- INPUTS ---------------- #
def input_data(st_name, datasource, refframe, data_config_file):
    [myData, offset_obj, eq_obj] = gps_input_pipeline.get_station_data(st_name, datasource, data_config_file, refframe);
    # First, we embed the data with the eq object (always useful)
    myData = gps_io_functions.Timeseries(name=myData.name, coords=myData.coords, dtarray=myData.dtarray, dN=myData.dN,
                                         dE=myData.dE, dU=myData.dU, Sn=myData.Sn, Se=myData.Se, Su=myData.Su,
                                         EQtimes=eq_obj.evdts);
    return [myData, offset_obj, eq_obj];

# -------------- COMPUTE ------------ #
def compute(myData, offset_obj, eq_obj, MyParams, starttime, endtime):
    if starttime is None:
        starttime = myData.dtarray[0];
    if endtime is None:
        endtime = myData.dtarray[-1];
    newData = gps_ts_functions.impose_time_limits(myData, starttime, endtime);
    if MyParams.offsets_remove == 1:  # Remove offsets and antenna changes
        newData = offsets.remove_offsets(newData, offset_obj);
    if MyParams.outliers_remove == 1:  # Remove outliers
        newData = gps_ts_functions.remove_outliers(newData, MyParams.outliers_def);
    if MyParams.earthquakes_remove == 1:  # Remove earthquakes
        newData = offsets.remove_offsets(newData, eq_obj);
    trend_out = gps_seasonal_removals.make_detrended_ts(newData, MyParams.seasonals_remove, MyParams.seasonals_type,
                                                        MyParams.data_config_file);
    return [newData, trend_out];

# -------------- OUTPUTS ------------ #
def single_ts_plot(ts_obj, detrended, MyParams, outdir):
    label_fontsize = 18;

    # The major figure
    dpival = 500;
    [_, axarr] = plt.subplots(3, 1, sharex=True, figsize=(10, 7), dpi=dpival);
    axarr[0].plot_date(ts_obj.dtarray, ts_obj.dE, color='blue', markeredgecolor='black', markersize=1.5);
    axarr[0].grid(linestyle='--', linewidth=0.5);
    axarr[0].set_ylabel('east (mm)', fontsize=label_fontsize);
    bottom, top = axarr[0].get_ylim();
    for i in range(len(ts_obj.EQtimes)):
        axarr[0].plot_date([ts_obj.EQtimes[i], ts_obj.EQtimes[i]], [bottom, top], '--k', linewidth=0.5);
    ax1 = axarr[0].twinx();
    ax1.plot_date(detrended.dtarray, detrended.dE, marker='D', markersize=1.0, color='red');
    ax1.set_ylabel('detrended (mm)', fontsize=label_fontsize - 2, color='red');
    ax1.tick_params(labelcolor='red', labelsize=label_fontsize, axis='both')
    axarr[0].tick_params(labelsize=label_fontsize);

    axarr[1].plot_date(ts_obj.dtarray, ts_obj.dN, color='blue', markeredgecolor='black', markersize=1.5);
    axarr[1].grid(linestyle='--', linewidth=0.5);
    axarr[1].set_ylabel('north (mm)', fontsize=label_fontsize);
    bottom, top = axarr[1].get_ylim();
    for i in range(len(ts_obj.EQtimes)):
        axarr[1].plot_date([ts_obj.EQtimes[i], ts_obj.EQtimes[i]], [bottom, top], '--k', linewidth=0.5);
    ax2 = axarr[1].twinx();
    ax2.plot_date(detrended.dtarray, detrended.dN, marker='D', markersize=1.0, color='red');
    ax2.set_ylabel('detrended (mm)', fontsize=label_fontsize - 2, color='red');
    ax2.tick_params(labelcolor='red', labelsize=label_fontsize, axis='both')
    axarr[1].tick_params(labelsize=label_fontsize);

    axarr[2].plot_date(ts_obj.dtarray, ts_obj.dU, color='blue', markeredgecolor='black', markersize=1.5);
    axarr[2].grid(linestyle='--', linewidth=0.5);
    axarr[2].set_ylabel('vertical (mm)', fontsize=label_fontsize);
    bottom, top = axarr[2].get_ylim();
    for i in range(len(ts_obj.EQtimes)):
        axarr[2].plot_date([ts_obj.EQtimes[i], ts_obj.EQtimes[i]], [bottom, top], '--k', linewidth=0.5);
    ax3 = axarr[2].twinx();
    ax3.plot_date(detrended.dtarray, detrended.dU, marker='D', markersize=1.0, color='red');
    ax3.set_ylabel('detrended (mm)', fontsize=label_fontsize - 2, color='red');
    axarr[2].set_xlim([min(ts_obj.dtarray), max(ts_obj.dtarray)]);
    ax3.tick_params(labelcolor='red', labelsize=label_fontsize, axis='both')
    axarr[2].tick_params(labelsize=label_fontsize);

    title, savename = get_figure_name(MyParams, outdir);
    axarr[0].set_title(title, fontsize=label_fontsize + 2);
    plt.savefig(savename, dpi=dpival);
    print("Saving figure as %s " % savename)
    return;

def get_figure_name(MyParams, outdir):
    # Things that might go into the name:
    # 1. Station
    # 2. Earthquakes removed
    # 3. Outliers removed
    # 4. Seasonals removed
    # 5. Datasource
    # 6. Refframe

    savename = outdir + MyParams.station;
    title = MyParams.station;

    title = title + ', ' + MyParams.datasource + ' ' + MyParams.refframe
    if MyParams.earthquakes_remove == 0 and MyParams.offsets_remove == 0 and MyParams.seasonals_remove == 0:
        title = title + ', unaltered';
    if MyParams.earthquakes_remove:
        savename = savename + "_noeq";
        title = title + ', no earthquakes'
    if MyParams.seasonals_remove:  # If we are removing seasonals:
        savename = savename + "_noseasons";
        title = title + ', no seasonals'
        if MyParams.seasonals_type == "lssq":
            savename = savename + "_lssq"
            title = title + ' by least squares'
        if MyParams.seasonals_type == "notch":
            savename = savename + "_notch"
            title = title + ' by notch filter'
        if MyParams.seasonals_type == "grace":
            savename = savename + "_grace"
            title = title + ' by GRACE model'
        if MyParams.seasonals_type == "stl":
            savename = savename + "_stl"
            title = title + ' by STL'
        if MyParams.seasonals_type == "nldas":
            savename = savename + "_nldas";
            title = title + ' by NLDAS'
        if MyParams.seasonals_type == "gldas":
            savename = savename + "_gldas";
            title = title + ' by GLDAS'
        if MyParams.seasonals_type == "lsdm":
            savename = savename + "_lsdm";
            title = title + ' by LSDM'
        if MyParams.seasonals_type == "shasta":
            savename = savename + "_shasta";
            title = title + ' by Shasta';
        if MyParams.seasonals_type == "oroville":
            savename = savename + "_oroville";
            title = title + ' by Oroville';
    savename = savename + '_' + MyParams.datasource + '_' + MyParams.refframe;
    savename = savename + "_ts.jpg";
    return title, savename;


from collections import deque

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

board = []
init_virus = []
ans = 0

for y in range(N):
    board.append(list(map(int, input().split())))
    for x in range(M):
        if board[y][x] == 2:
            init_virus.append([x, y])

def get_next_wall(pos):
    x, y = pos

    x += 1
    if x >= M:
        x = 0
        y += 1

    while 1:
        if y == N:
            return 0
        if board[y][x] != 0:
            x += 1
            if x >= M:
                x = 0
                y += 1
        else:
            return [x, y]

dx = (0, 0, -1, 1)
dy = (-1, 1, 0, 0)

def set_wall(pos, wall_num):
    global ans
    if wall_num == 3:
        visit = [[0] * M for _ in range(N)]
        bfs_q = deque()
        for virus_pos in init_virus:
            bfs_q.append(virus_pos)
            visit[virus_pos[1]][virus_pos[0]] = 1

        while bfs_q:
            current = bfs_q.popleft()
            for df in range(4):
                new = [current[0] + dx[df], current[1] + dy[df]]
                if 0 <= new[0] < M and 0 <= new[1] < N and (board[new[1]][new[0]] == 0) and not visit[new[1]][new[0]]:
                    visit[new[1]][new[0]] = 1
                    bfs_q.append(new)

        partial_ans = 0
        for y in range(N):
            for x in range(M):
                if visit[y][x] == 0 and board[y][x] == 0:
                    partial_ans += 1

        if ans < partial_ans:
            ans = partial_ans

        return
    else:
        next_wall = get_next_wall(pos)
        while 1:
            if next_wall == 0:
                return
            board[next_wall[1]][next_wall[0]] = 1
            set_wall(next_wall, wall_num + 1)
            board[next_wall[1]][next_wall[0]] = 0
            next_wall = get_next_wall(next_wall)

set_wall([-1, 0], 0)

print(ans)


# -*- encoding: utf-8 -*-

__author__ = 'shuhao.wang'

import os
import uuid

from Desire.BaseFramework.Resource.comm import *
from Desire.BaseFramework.Logger import logger
from Desire.BaseFramework.Utils import host

from .rpc_stubs import *

if os.name == "posix":
    _LOCAL_UUID = "/etc/desire/node/local.uuid"
    _AUTH_UUID = "/etc/desire/node/auth.uuid"
else:
    _LOCAL_UUID = "c:/desire/etc/node/local.uuid"
    _AUTH_UUID = "c:/desire/etc/node/auth.uuid"

_LOGGER_NAME = "Node Server"

_SERVER_NODE = None

def _check_local():
    """
        检查本地环境

        本地UUID，服务授权ID
    :return:
        True or False
        成功返回True，失败返回False
    """

    global _LOCAL_UUID, _AUTH_UUID

    # 检查本地UUID文件
    if not os.path.exists(_LOCAL_UUID):
        return False

    # 检查服务授权ID文件
    if not os.path.exists(_AUTH_UUID):
        return False

def _read_self_uuid():

    global _LOCAL_UUID, _AUTH_UUID

    _LOGGER = logger.get_logger()
    # 打开文件
    local_uuid = None
    try:
        with open(_LOCAL_UUID, "rb") as f:
            # 36 len(uuid)
            local_uuid = f.read(36)
    except IOError as e:
        _LOGGER.warn(_LOGGER_NAME, "Open local uuid error: %s" % str(e))

    auth_uuid = None
    try:
        with open(_AUTH_UUID, "rb") as f:
            auth_uuid = f.read(36)
    except IOError as e:
        _LOGGER.warn(_LOGGER_NAME, "Open auth uuid error: %s" % str(e))

    return local_uuid, auth_uuid

# 暂不使用
def _make_local_uuid():
    global _LOCAL_UUID
    _LOGGER = logger.get_logger()

    if os.path.exists(_LOCAL_UUID):
        _LOGGER.warn(_LOGGER_NAME, "Local uuid file already exists")
        return

    if not os.path.exists(os.path.dirname(_LOCAL_UUID)):
        os.makedirs(os.path.dirname(_LOCAL_UUID))

    local_uuid = uuid.uuid4()

    try:
        with open(_LOCAL_UUID, "w") as f:
            f.write(str(local_uuid))
    except IOError as e:
        _LOGGER.error(_LOGGER_NAME, "Can not write local uuid to file")
        raise e

def _write_auth_uuid(auth_uuid):
    global _AUTH_UUID
    _LOGGER = logger.get_logger()

    if os.path.exists(_AUTH_UUID):
        _LOGGER.error(_LOGGER_NAME, "Auth UUID file already exists!")
        raise IOError("Auth UUID file already exists!")

    if not os.path.exists(os.path.dirname(_AUTH_UUID)):
        os.makedirs(os.path.dirname(_AUTH_UUID))

    try:
        with open(_AUTH_UUID) as f:
            f.write(auth_uuid)
    except IOError as e:
        _LOGGER.error(_LOGGER_NAME, "can not write auth uuid to file")
        raise e

def register():
    """
        想系统管理中心注册本服务器节点

        首次注册会被添加到系统管理中心，属于为认证的服务器，不会被调度和使用
        需要管理员在控制台中认证后才会被正式接纳为可提供服务的机器
    """
    _LOGGER = logger.get_logger()
    # 检查本地信息,本地UUID应在安装系统时确定，而不能动态生成
    if not _check_local():
        raise SystemError("No local uuid file")

    local_uuid, auth_uuid = _read_self_uuid()

    server_node = ResServer()

    server_node["name"] = host.name

    server_node["uuid"] = local_uuid
    server_node["auth_uuid"] = auth_uuid
    server_node["role"] = 0
    server_node["location"] = host.ip_addr
    # FIXME: 处理器的数量
    server_node["num_of_processor"] = 1
    # FIXME: 每处理器的核心数目
    server_node["cores_of_per_processor"] = 6
    # FIXME: 服务器的内存大小
    server_node["memory_size"] = 16

    try:
        global _SERVER_NODE
        server_node = stub_register(server_node)

        # auth_uuid为None时，说明服务器并没有被认证
        if (auth_uuid is None and server_node['auth_uuid'] is not None) or\
            (auth_uuid != server_node['auth_uuid']):
            # 管理中心返回的是已经认证的，本地没有记录
            # 管理中心的记录和本地记录不一致
            # 上述两种情况，说明服务器数据存在问题，按规矩本地应该罢工
            raise ValueError(
                "An error has occurred: local auth uuid is None, but remote auth uuid is %s" %
                server_node['auth_uuid'])

        _SERVER_NODE = server_node
    except Exception as e:
        _LOGGER.error(_LOGGER_NAME, "Register node server error: %s" % str(e))

        raise e

def is_authed():
    """
        判断本节点是否已经通过管理中心授权
    :return:
        True 已经通过管理中心授权
        False 没有通过管理中心授权
    """
    global _SERVER_NODE

    return False if _SERVER_NODE["auth_uuid"] is None else True

def handle_auth(auth_node):
    """
        管理中心认证时触发本地节点进行认证
    :param auth_node: 认证的节点信息，正常情况下指向本节点
    :return: None
    """
    global _SERVER_NODE

    if auth_node != _SERVER_NODE:
        raise ValueError("Invalid Server Node")

    _write_auth_uuid(auth_node["auth_uuid"])
    # 更新本地的ServerNode
    _SERVER_NODE['auth_uuid'] = auth_node["auth_uuid"]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]*$')
from django.shortcuts import render, redirect
from django.contrib import messages
from models import Email

# Create your views here.
def index(request):
    return render(request, "test_app/index.html")

def process(request):
    if request.method == "POST":
        if not EMAIL_REGEX.match(request.POST["email"]):
            messages.add_message(request, messages.ERROR, "Email is not valid!")
            return redirect("/")
        else:
            request.session['email'] = request.POST['email']
            Email.objects.create(name=request.POST['email'])
            context = {'emails': Email.objects.all()}
            return render(request, 'test_app/process.html', context)


import argparse
import sys

from gitopscli.io.yaml_util import yaml_load

def create_cli(args):
    parser, subparsers = __create_cli_parser()
    __add_deploy_command_parser(subparsers)
    __add_sync_apps_command_parser(subparsers)
    __add_pr_comment_command_parser(subparsers)
    __add_create_preview_command_parser(subparsers)
    __add_create_pr_preview_command_parser(subparsers)
    __add_delete_preview_command_parser(subparsers)
    __add_delete_pr_preview_command_parser(subparsers)
    __add_version_command_parser(subparsers)

    if len(args) == 0:
        parser.print_help(sys.stderr)
        sys.exit(2)

    return parser.parse_args(args)

def __create_cli_parser():
    parser = argparse.ArgumentParser(prog="gitopscli", description="GitOps CLI")
    subparsers = parser.add_subparsers(title="commands", dest="command")
    return parser, subparsers

def __add_deploy_command_parser(subparsers):
    deploy_p = subparsers.add_parser("deploy", help="Trigger a new deployment by changing YAML values")
    deploy_p.add_argument("--file", help="YAML file path", required=True)
    deploy_p.add_argument(
        "--values",
        help="YAML/JSON object with the YAML path as key and the desired value as value",
        type=yaml_load,
        required=True,
    )
    deploy_p.add_argument(
        "--single-commit",
        help="Create only single commit for all updates",
        type=__str2bool,
        nargs="?",
        const=True,
        default=False,
    )
    deploy_p.add_argument(
        "--commit-message", help="Specify exact commit message of deployment commit", type=str, default=None,
    )

    __add_git_parser_args(deploy_p)
    __add_branch_pr_parser_args(deploy_p)
    __add_verbose_parser(deploy_p)

def __add_sync_apps_command_parser(subparsers):
    sync_apps_p = subparsers.add_parser(
        "sync-apps", help="Synchronize applications (= every directory) from apps config repository to apps root config"
    )
    __add_git_parser_args(sync_apps_p)
    __add_verbose_parser(sync_apps_p)
    sync_apps_p.add_argument("--root-organisation", help="Root config repository organisation", required=True)
    sync_apps_p.add_argument("--root-repository-name", help="Root config repository name", required=True)

def __add_pr_comment_command_parser(subparsers):
    add_pr_comment_p = subparsers.add_parser("add-pr-comment", help="Create a comment on the pull request")
    __add_git_parser_args(add_pr_comment_p, api_only=True)
    __add_create_prid_parser(add_pr_comment_p)
    __add_verbose_parser(add_pr_comment_p)
    add_pr_comment_p.add_argument("--text", help="the text of the comment", required=True)

def __add_create_preview_command_parser(subparsers):
    add_create_preview_common_p = subparsers.add_parser("create-preview", help="Create a preview environment")
    __add_git_parser_args(add_create_preview_common_p)
    __add_create_githash_previewid_parser(add_create_preview_common_p)
    __add_verbose_parser(add_create_preview_common_p)

def __add_create_pr_preview_command_parser(subparsers):
    add_create_pr_preview_p = subparsers.add_parser("create-pr-preview", help="Create a preview environment")
    __add_git_parser_args(add_create_pr_preview_p)
    __add_create_prid_parser(add_create_pr_preview_p)
    __add_verbose_parser(add_create_pr_preview_p)

def __add_delete_preview_command_parser(subparsers):
    add_delete_preview_p = subparsers.add_parser("delete-preview", help="Delete a preview environment")
    __add_git_parser_args(add_delete_preview_p)
    add_delete_preview_p.add_argument(
        "--preview-id", help="The preview-id for which the preview was created for", required=True
    )
    __add_verbose_parser(add_delete_preview_p)

def __add_delete_pr_preview_command_parser(subparsers):
    add_delete_preview_p = subparsers.add_parser("delete-pr-preview", help="Delete a pr preview environment")
    __add_git_parser_args(add_delete_preview_p)
    add_delete_preview_p.add_argument(
        "--branch", help="The branch for which the preview was created for", required=True
    )
    __add_verbose_parser(add_delete_preview_p)

def __add_version_command_parser(subparsers):
    subparsers.add_parser("version", help="Show the GitOps CLI version information")

def __add_git_parser_args(deploy_p, api_only=False):
    deploy_p.add_argument("--username", help="Git username", required=True)
    deploy_p.add_argument("--password", help="Git password or token", required=True)
    if not api_only:
        deploy_p.add_argument("--git-user", help="Git Username", default="GitOpsCLI")
        deploy_p.add_argument("--git-email", help="Git User Email", default="gitopscli@baloise.dev")
    deploy_p.add_argument("--organisation", help="Apps Git organisation/projectKey", required=True)
    deploy_p.add_argument("--repository-name", help="Git repository name (not the URL, e.g. my-repo)", required=True)
    deploy_p.add_argument("--git-provider", help="Git server provider")
    deploy_p.add_argument("--git-provider-url", help="Git provider base API URL (e.g. https://bitbucket.example.tld)")

def __add_branch_pr_parser_args(deploy_p):
    deploy_p.add_argument(
        "--create-pr", help="Creates a Pull Request", type=__str2bool, nargs="?", const=True, default=False,
    )
    deploy_p.add_argument(
        "--auto-merge",
        help="Automatically merge the created PR (only valid with --create-pr)",
        type=__str2bool,
        nargs="?",
        const=True,
        default=False,
    )

def __add_create_githash_previewid_parser(subparsers):
    subparsers.add_argument("--git-hash", help="the git hash which should be deployed", type=str, required=True)
    subparsers.add_argument(
        "--preview-id", help="the id of folder in the config repo which will be created", type=str, required=True
    )

def __add_create_prid_parser(subparsers):
    subparsers.add_argument("--pr-id", help="the id of the pull request", type=int, required=True)
    subparsers.add_argument("--parent-id", help="the id of the parent comment, in case of a reply", type=int)

def __add_verbose_parser(subparsers):
    subparsers.add_argument(
        "-v", "--verbose", help="Verbose exception logging", type=__str2bool, nargs="?", const=True, default=False,
    )

def __str2bool(value):
    if isinstance(value, bool):
        return value
    if value.lower() in ("yes", "true", "t", "y", "1"):
        return True
    if value.lower() in ("no", "false", "f", "n", "0"):
        return False
    raise argparse.ArgumentTypeError("Boolean value expected.")


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import gym
import matplotlib.pyplot as plt
import copy
import collections
import random
# it is actually Dueling DDQN
# hyper-parameters
BATCH_SIZE = 128
LR = 0.01
GAMMA = 0.9
MEMORY_CAPACITY = 4000
Q_NETWORK_ITERATION = 100

# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cuda')
env = gym.make("CartPole-v1")
env = env.unwrapped
NUM_ACTIONS = env.action_space.n
NUM_STATES = env.observation_space.shape[0]
ENV_A_SHAPE = 0 if isinstance(env.action_space.sample(), int) else env.action_space.sample.shape

def setup_seed(seed):
    env.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
    # cudnn.benchmark = False
    # cudnn.enabled = False

setup_seed(1234)

class DDQN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(NUM_STATES, 128)
        self.fc2 = nn.Linear(128, 128)

        self.V = nn.Linear(128, 1)
        self.A = nn.Linear(128, NUM_ACTIONS)

    def forward(self,x):
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        x = F.relu(x)
        Value = self.V(x)
        Advantage = self.A(x)

        Q_value = Value + (Advantage - Advantage.mean(dim=1, keepdim=True))
        return Q_value

class Agent(nn.Module):
    """docstring for DQN"""

    def __init__(self):
        super().__init__()
        self.eval_net, self.target_net = DDQN(), DDQN()

        self.learn_step_counter = 0
        self.memory_counter = 0
        self.memory = collections.deque(maxlen=MEMORY_CAPACITY)
        # why the NUM_STATE*2 +2
        # When we store the memory, we put the state, action, reward and next_state in the memory
        # here reward and action is a number, state is a ndarray
        self.optimizer = torch.optim.Adam(self.eval_net.parameters(), lr=LR)
        self.loss_func = nn.MSELoss()

    def choose_action(self, state, epsilon):
        state = torch.unsqueeze(torch.FloatTensor(state), 0).to(device)  # get a 1D array
        if np.random.rand() > epsilon:  # greedy policy
            action_value = self.eval_net.forward(state)
            action = torch.max(action_value, 1)[1].cpu().data.numpy()
            action = action[0] if ENV_A_SHAPE == 0 else action.reshape(ENV_A_SHAPE)
        else:  # random policy
            action = np.random.randint(0, NUM_ACTIONS)
            action = action if ENV_A_SHAPE == 0 else action.reshape(ENV_A_SHAPE)
        return action

    def store_transition(self, state, action, reward, next_state):
        transition = np.hstack((state, [action, reward], next_state))
        index = self.memory_counter % MEMORY_CAPACITY
        self.memory.append(transition)
        self.memory_counter += 1

    def learn(self):

        # update the parameters
        if self.learn_step_counter % Q_NETWORK_ITERATION == 0:
            self.target_net.load_state_dict(self.eval_net.state_dict())
        self.learn_step_counter += 1

        # sample batch from memory

        batch_memory = torch.Tensor(random.sample(self.memory, BATCH_SIZE)).to(device)

        batch_state = batch_memory[:, :NUM_STATES]
        batch_action = batch_memory[:, NUM_STATES:NUM_STATES + 1].long()
        batch_reward = batch_memory[:, NUM_STATES + 1:NUM_STATES + 2]
        batch_next_state = batch_memory[:, -NUM_STATES:]

        # q_eval
        q_eval = self.eval_net(batch_state).gather(1, batch_action)
        target_action = self.eval_net(batch_state).argmax(dim=1).unsqueeze(1).long()
        # 使用eval_net找到最大Q值对应的动作，再用此动作带入target_Qnet计算target_Q
        q_next = self.target_net(batch_next_state).gather(1, target_action).detach()
        # q_next:[batch_sz, 2 (action dim)]
        q_target = batch_reward + GAMMA * q_next
        loss = self.loss_func(q_eval, q_target)

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

def reward_func(env, x, x_dot, theta, theta_dot):
    r1 = (env.x_threshold - abs(x)) / env.x_threshold - 0.5
    r2 = (env.theta_threshold_radians - abs(theta)) / env.theta_threshold_radians - 0.5
    reward = r1 + r2
    return reward

def main():
    dqn = Agent().to(device)

    time_list = []
    episodes = 300
    max_time = 500
    print("Collecting Experience....")
    reward_list = []
    plt.ion()
    fig, ax = plt.subplots()
    for i in range(episodes):
        state = env.reset()
        epsilon = max(0.01, 0.1 - (0.1 - 0.01) * (i / 200))
        ep_reward = 0
        play_times = 0
        for t in range(max_time):
            env.render()
            action = dqn.choose_action(state, epsilon)
            next_state, reward, done, info = env.step(action)
            x, x_dot, theta, theta_dot = next_state
            reward = reward_func(env, x, x_dot, theta, theta_dot)

            dqn.store_transition(state, action, reward, next_state)
            ep_reward += reward
            play_times += 1

            if dqn.memory_counter >= 2000:
                dqn.learn()
                if done or t == max_time - 1:
                    print("episode: {} , epsilon: {} , the episode reward is {}, it has played {} times".\
                          format(i, round(epsilon, 3), round(ep_reward, 3), play_times))
            if done or t == max_time - 1:
                break
            state = next_state

        time_list.append(play_times)
        r = copy.copy(ep_reward)
        reward_list.append(r)
        # ax.set_xlim(0, 300)
        # ax.cla()
        ax.plot(reward_list, 'g-', label='total_loss')
        plt.pause(0.001)

    plt.figure()
    plt.plot(time_list)
    plt.legend(['times it insists'])

if __name__ == '__main__':
    main()


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

#******************** Sobel、Laplacia 边缘检测 ********************
'''img=cv.imread('2.jpg',0)
laplacian=cv.Laplacian(img,cv.CV_64F)#64F代表每一个像素点元素占64位浮点数
sobelx=cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely=cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap='gray')
plt.title('Oridinal'),plt.xticks([]),plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap='gray')
plt.title('Laplacian'),plt.xticks([]),plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap='gray')
plt.title('sobelx'),plt.xticks([]),plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap='gray')
plt.title('sobely'),plt.xticks([]),plt.yticks([])
plt.show()'''
'''
但这有一个小问题。黑色到白色的过渡被视为正斜率（具有正值），而白色到黑色的过渡被视为负斜率（具有负值）。因此，当您将数据转换为np.uint8时，所有负斜率均设为零。简而言之，您会错过这一优势。
如果要检测两个边缘，更好的选择是将输出数据类型保留为更高的形式，例如cv.CV_16S，cv.CV_64F等，取其绝对值，然后转换回cv.CV_8U。下面的代码演示了水平Sobel滤波器的处理过程以及结果的差异。
'''

#******************** 如果要检测两个边缘，先将其输出为更高精度然后再转回低精度 ********************
'''img = cv.imread('edge.PNG',0)
# Output dtype = cv.CV_8U
sobelx8u = cv.Sobel(img,cv.CV_8U,1,0,ksize=5)
# Output dtype = cv.CV_64F. Then take its absolute and convert to cv.CV_8U
sobelx64f = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
plt.subplot(1,3,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2),plt.imshow(sobelx8u,cmap = 'gray')
plt.title('Sobel CV_8U'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3),plt.imshow(sobel_8u,cmap = 'gray')
plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([])
plt.show()'''

#******************** canny 边缘检测 ********************
#步骤：1、降噪 2、查找图像的强度梯度 3、非最大抑制 4、滞后阈值法
img=cv.imread('2.jpg',0)
edges=cv.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap='gray')
plt.title('Original Image'),plt.xticks([]),plt.yticks([])#plt.xticks([]),plt.yticks([])不显示坐标轴
plt.subplot(122),plt.imshow(edges,cmap='gray')
plt.title('Edge Image'),plt.xticks([]),plt.yticks([])
plt.show()


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

import tkinter as tk
from tkinter import ttk
from itertools import product
from PIL import Image, ImageTk
import numpy as np
import pandas as pd
import csv

class ApplicationState(object):
    def __init__(self):
        self.dataframe = pd.read_csv('data/twcs.csv', index_col=0, encoding='utf8', low_memory=True)
        #self.dataframe.set_index('tweet_id', inplace=True)
        self.dataframe['is_question'] = None
        self.dataframe['q_type'] = None
        self.idx = 0

    def save(self, event):
        self.dataframe.to_csv(
            'data/twcs.lbl.csv',
            sep='\t',
            na_rep='NaN',
            index=True,
            encoding='utf8',
            quoting=csv.QUOTE_ALL,
            line_terminator='\n',
        )

    def prev(self, event):
        self.idx = max(self.idx - 1, 0)

    def next(self, event):
        self.idx = min(self.idx + 1, len(self.dataframe) - 1)

class Application(tk.Tk):
    @classmethod
    def dataframe_widget(cls, parent):
        frame = ttk.Frame(parent)

        dataframe = parent.app_state.dataframe

        index = [dataframe.index.name]
        columns = dataframe.columns.tolist()
        tree = ttk.Treeview(frame, columns=columns, selectmode='browse')

        for i,v in enumerate(index + columns):
            c = '#{}'.format(i)
            tree.heading(c, text=v)

            tree.column(c, minwidth=25, width=50, stretch=True)

        k = columns.index('text')
        tree.column(k, minwidth=150, width=300, stretch=True)

        # Only load 500
        count = 31
        for i, r in dataframe.iterrows():
            if count==0: break
            count -= 1

            vals = [ r[v] for v in columns ]
            vals[k] = vals[k].encode('utf8')
            tree.insert('', 'end', iid=i, values=vals, text=i)

        vsb = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
        tree.configure(yscrollcommand=vsb.set)

        tree.grid(row=0, column=0, sticky=tk.N+tk.S+tk.W+tk.E)
        vsb.grid(row=0, column=1, sticky=tk.N+tk.S)

        frame.rowconfigure(0, weight=1)
        frame.columnconfigure(0, weight=1)

        return frame

    @classmethod
    def tweet_view_widget(cls, parent, text='Tweet'):
        frame = ttk.LabelFrame(parent, text=text)

        frame.elemets = {
            'tweet_id': ttk.Entry,
            'author_id': ttk.Entry,
            'inbound': ttk.Entry,
            'created_at': ttk.Entry,
            'text': tk.Text,
            'response_tweet_id': ttk.Entry,
            'in_response_to_tweet_id': ttk.Entry
        }

        # Place a label before each
        frame.elem_frames = {}
        for k in frame.elemets:
            frame.elem_frames[k] = ttk.LabelFrame(frame, text=k)
            kw = dict(cursor='arrow')
            frame.elemets[k] = frame.elemets[k](frame.elem_frames[k], **kw)
            frame.elemets[k].bind('<Key>', lambda _: 'break')
            frame.elemets[k].pack(fill='x')

        # Place the  widgets in layout
        frame.elem_frames['tweet_id'].grid(row=0, column=0, sticky=tk.E+tk.W)
        frame.elem_frames['author_id'].grid(row=0, column=1, sticky=tk.E+tk.W)
        frame.elem_frames['inbound'].grid(row=0, column=2, sticky=tk.E+tk.W)
        frame.elem_frames['created_at'].grid(row=1, column=0, sticky=tk.E+tk.W)
        frame.elem_frames['response_tweet_id'].grid(row=1, column=1, sticky=tk.E+tk.W)
        frame.elem_frames['in_response_to_tweet_id'].grid(row=1, column=2, sticky=tk.E+tk.W)
        frame.elem_frames['text'].grid(row=2, column=0, columnspan=3, sticky=tk.N+tk.S+tk.E+tk.W)

        for i in range(3): frame.rowconfigure(i, weight=1)
        for i in range(3): frame.columnconfigure(i, weight=1)

        return frame

    @classmethod
    def labeler_widget(cls, parent):
        labeler = ttk.Frame(parent)

        q_tv = cls.tweet_view_widget(labeler, 'Tweet')
        btn_frame = ttk.Frame(labeler)
        #a_tv = cls.tweet_view_widget(labeler, 'Answer')

        lbl_frame = ttk.Frame(btn_frame)
        p_btn = ttk.Button(btn_frame, text='<<')
        n_btn = ttk.Button(btn_frame, text='>>')
        s_btn = ttk.Button(btn_frame, text='Save')
        o_frame = ttk.Frame(btn_frame)
        ok_btn = ttk.Button(btn_frame, text='OK')
        nq_btn = ttk.Button(btn_frame, text='Not a Question')
        # ----

        p_btn.bind('<Button-1>', parent.app_state.prev)
        n_btn.bind('<Button-1>', parent.app_state.next)
        s_btn.bind('<Button-1>', parent.app_state.save)

        # ----

        parent.options = [
            {'text': 'Where'},
            {'text': 'When'},
            {'text': 'How'},
            {'text': 'Why'},
        ]

        for i, (r, c) in zip(range(len(parent.options)), product(range(len(parent.options)//2), range(2))):
            parent.options[i]['var'] = tk.BooleanVar(False)
            parent.options[i]['btn'] = ttk.Checkbutton(o_frame, text=parent.options[i]['text'], offvalue=False, onvalue=True, variable=parent.options[i]['var'])
            parent.options[i]['btn'].grid(row=r, column=c, padx=5, sticky=tk.W)
        # ----

        photo = tk.PhotoImage(file='data/hang-in-there.gif')
        label = ttk.Label(lbl_frame, image=photo)
        label.image = photo # keep a reference!
        label.pack()

        lbl_frame.grid(row=0, column=0, columnspan=2, sticky=tk.N)
        p_btn.grid(row=1, column=0, sticky=tk.E+tk.W)
        n_btn.grid(row=1, column=1, sticky=tk.E+tk.W)
        s_btn.grid(row=2, column=0, columnspan=2)
        o_frame.grid(row=3, column=0, columnspan=2)
        ok_btn.grid(row=4, column=0)
        nq_btn.grid(row=4, column=1)

        q_tv.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
        btn_frame.grid(row=0, column=1, sticky=tk.N+tk.S)
        #a_tv.grid(row=0, column=2)

        labeler.rowconfigure(0, weight=1)
        labeler.columnconfigure(0, weight=1)
        labeler.columnconfigure(0, weight=1)

        return labeler

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.tk.call('encoding', 'system', 'utf-8')
        self.title("Hang in There!")

        self.app_state = ApplicationState()

        # Widgets go here
        self.nb = ttk.Notebook(self)
        self.nb.pack(expand=True, fill='both', padx=2, pady=2)
        self.nb.bind("<<NotebookTabChanged>>", self._on_tab_changed)

        self.nb.app_state = self.app_state

        self.tabs = {
            'Labeler': Application.labeler_widget(self.nb),
            'Dataframe': Application.dataframe_widget(self.nb)
        }

        for name, tab in self.tabs.items():
            self.nb.add(tab, text=name)

        self.nb.select(self.tabs['Labeler'])
        self.nb.enable_traversal()

        #self.resizable(False, False)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

    def _on_tab_changed(self, event):
        event.widget.update_idletasks()
        tab = event.widget.nametowidget(event.widget.select())
        event.widget.configure(height=tab.winfo_reqheight(), width=tab.winfo_reqwidth())

if __name__ == '__main__':
    root = Application()
    root.mainloop()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data/L6-FOL-daily.csv', index_col=0, parse_dates=True)
# print(df[df.isnull().any(axis=1)])
df.fillna(method='ffill', inplace=True) # fill missing data

df.plot.scatter('storage', 'elevation')
plt.xlabel('Storage (TAF)')
plt.ylabel('Water Surface Elevation (ft)')

# # then fit a quadratic and keep the equation for the next code example.
p = np.polyfit(df.storage.values, df.elevation.values, 2)
print('Regression: Elevation = %f S**2 + %f S + %f' % tuple(p))
f = np.poly1d(p)

plt.plot(df.storage.values, f(df.storage.values), color='r', linewidth=2)
plt.show()


"""make related urls optional (when used for posts in the same source)

Revision ID: e571d49fe4aa
Revises: ab0709325ab6
Create Date: 2020-12-18 23:35:18.036992

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'e571d49fe4aa'
down_revision = 'ab0709325ab6'
branch_labels = None
depends_on = None

def upgrade():
    op.alter_column('related', 'url', nullable=True)

def downgrade():
    op.alter_column('related', 'url', nullable=False)


from datetime import timedelta
from timeit import default_timer as timer

import numpy as np
import pandas as pd
from sklearn import datasets

from neo_force_scheme.neo_force_scheme import NeoForceScheme, ProjectionMode

#################################
projection_n_dimensions = 3
nfs = NeoForceScheme(metric="euclidean", verbose=True, learning_rate0=0.2,
                     decay=0.95, max_it=300, cuda=False)
starting_projection_mode = ProjectionMode.TSNE
fix_column_to_z_projection_axis = -1
drop_columns_from_dataset = [-1]
scaler = (0, 1)
z_axis_moving_range = (-0.2, 0.2)
confidence_interval = 0.9
# by default z_axis_moving_range is (0, 0), which means not allowing any movement
plot = False

# data = np.loadtxt('./datasets/mammals.data', delimiter=",")
# data = pd.read_csv('./datasets/whr2019.csv', delimiter=",").values
# data = np.concatenate((datasets.load_iris().data.T,[datasets.load_iris().target.T])).T
data = np.concatenate((datasets.load_breast_cancer().data.T,[datasets.load_breast_cancer().target.T])).T
# data = np.tile(data, (100, 1)) # use this make the dataset 100x larger for performance test

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

# execute force scheme
start = timer()
projection = nfs.fit_transform(X=data,
                               starting_projection_mode=starting_projection_mode,
                               random_state=1,
                               n_dimension=projection_n_dimensions,
                               fix_column_to_z_projection_axis=fix_column_to_z_projection_axis,
                               drop_columns_from_dataset=drop_columns_from_dataset,
                               scaler=scaler,
                               z_axis_moving_range=z_axis_moving_range,
                               confidence_interval=confidence_interval)
error = nfs.projection_error_
end = timer()

print(f'ForceScheme took {timedelta(seconds=end - start)} to execute with error {error}')

# calculate stress
stress = nfs.score(projection)
print('Kruskal stress {0}:'.format(stress))

# save projection
# np.savetxt("projection.txt", projection, delimiter=" ", fmt="%s")

if plot:
    if projection_n_dimensions == 2:
        # show projection
        """
        # method1
        import plotly.express as px
        fig = px.scatter(x=projection[:, 0],
                         y=projection[:, 1],
                         labels={'x':'x', 'y':'y'})
        fig.show()
        """
        """
        # method2
        import numpy as np
        import plotly.express as px

        ar = np.arange(100).reshape((10, 10))
        fig = px.scatter(projection,
                         x=projection[:, 0],
                         y=projection[:, 1],
                         size=1,
                         color=1)
        fig.show()
        """

        # method3
        import plotly.graph_objects as go
        import numpy as np

        fig = go.Figure(
            data=go.Scatter(
                x=projection[:, 0],
                y=projection[:, 1],
                mode='markers',
                marker=dict(
                    size=16,
                    color=np.random.randn(500),
                    colorscale='Viridis',
                    showscale=True)
            )
        )
        fig.show()
    else:
        import plotly.graph_objects as go

        fig = go.Figure(
            data=[go.Scatter3d(x=projection[:, 0],
                               y=projection[:, 1],
                               z=projection[:, 2],
                               mode='markers',
                               marker=dict(
                                   size=12,
                                   color=projection[:, 2],  # set color to an array/list of desired values
                                   colorscale='Viridis',  # choose a colorscale
                                   opacity=0.8
                               )
                               )])

        fig.show()


"""Integration tests for DIDComm resolver."""

# pylint: disable=redefined-outer-name

from functools import wraps
import time

import pytest
import requests

from . import Agent, REQUESTER, RESOLVER, DID_MOCK, DID_KEY, DID_SOV, DID_MOCK_FAIL

@pytest.fixture(scope="session")
def requester():
    """requester agent fixture."""
    yield Agent(REQUESTER)

@pytest.fixture(scope="session")
def resolver():
    """resolver agent fixture."""
    yield Agent(RESOLVER)

@pytest.fixture(scope="session", autouse=True)
def established_connection(resolver, requester):
    """Established connection filter."""
    invite = resolver.create_invitation(auto_accept="true")["invitation"]
    resp = requester.receive_invite(invite, auto_accept="true")
    yield resp["connection_id"]

def test_mocked_resolver_connection(established_connection, requester: Agent):
    """Test resolution over DIDComm Connection."""
    requester.post(
        f"/resolver/register/{established_connection}",
        fail_with="Failed to register connection as resolver for mock method",
        json={"methods": ["mock"]},
    )
    resp = requester.get(
        f"/resolver/resolve/{DID_MOCK}", fail_with=f"Failed to resolve DID {DID_MOCK}"
    )
    assert resp == {
        "@context": "https://www.w3.org/ns/did/v1",
        "id": "did:mock:test:mocked_id",
    }


from Bio import SeqIO

# The script removes duplicates from fasta file
f_result = open('Nicotiana_miRNA_D_no_duplicates.fa', 'w')

sequences = []
for seq_record in SeqIO.parse('Nicotiana_miRNA_D.fa', 'fasta'):
    # print(seq_record.seq)
    if seq_record.seq not in sequences:
        sequences.append(seq_record.seq)
        final_result = ('>' +seq_record.description + '\n' + seq_record.seq + '\n')

# print(len(sequences))
f_result.close()


from django.conf.urls import url
from django.urls import path

from . import views

urlpatterns = [
    path('', views.Index.as_view(), name='index'),
    # url(r'^categories/', views.CategoryListView.as_view()),
    url(r'^categories/', views.CategoryListView.as_view()),
    url(r'^items/(?P<pk>\d+)/delete$', views.ItemDestroyView.as_view()),
    url(r'^items/(?P<pk>\d+)/update$', views.ItemUpdateView.as_view()),
    url(r'^items/(?P<pk>\d+)$', views.ItemDetailView.as_view()),
    url(r'^items/', views.ItemListView.as_view()),
]


from typing import Any, Dict, List, Optional

import attr

from ..models.validation_error import ValidationError

@attr.s(auto_attribs=True)
class HTTPValidationError:
    """  """

    detail: Optional[List[ValidationError]] = None

    def to_dict(self) -> Dict[str, Any]:
        if self.detail is None:
            detail = None
        else:
            detail = []
            for detail_item_data in self.detail:
                detail_item = detail_item_data.to_dict()

                detail.append(detail_item)

        return {
            "detail": detail,
        }

    @staticmethod
    def from_dict(d: Dict[str, Any]) -> "HTTPValidationError":
        detail = []
        for detail_item_data in d.get("detail") or []:
            detail_item = ValidationError.from_dict(detail_item_data)

            detail.append(detail_item)

        return HTTPValidationError(
            detail=detail,
        )


import numpy as np
from vizdoom import *
import random
import time
from skimage import transform
from collections import deque
import matplotlib.pyplot as plt
import cv2
from collections import deque

# root path for test scenarios
test_path = './test/'
# each folder will contain the target images for all steps in the scenario. also, a file that contains the final position of the
# agent at the end of the scenario.

# num of scenarios to test on, each scenario has num_action steps
num_scenarios = 100
num_actions = 20

# all actions
backward = [0, 0, 0, 1]
forward = [0, 0, 1, 0]
right = [1, 0, 0, 0]
left = [0, 1, 0, 0]
all_actions = [right, left, forward, backward]

# function to initialize the environment before each scenario
def init_env():
	game = DoomGame()
	game.load_config('dset_create.cfg')
	game.set_doom_scenario_path('test_map.wad')
	game.init()
	return game

game = init_env()

# generate n test scenarios, each with 10 actions
def get_test_scenario_actions():
	test_scenario_actions = []
	# generate 100 test scenario actions
	for i in range(num_scenarios):
		actions_in_scenario = []
		for j in range(num_actions):
			action_choice = np.random.choice(len(all_actions))
			action = all_actions[action_choice]
			actions_in_scenario.append(action)
		test_scenario_actions.append(actions_in_scenario)

	test_scenario_actions = np.array(test_scenario_actions).astype(np.float32)
	return test_scenario_actions

# Run a scenario in our doom environment and store all the observations (will be used as human demonstrations)
def get_target_states_for_scenario(scenario_num, test_scenario_actions):
	game.new_episode()
	end_pos_fname = test_path + str(scenario_num) + '/pos.txt' # will hold the final position of the agent for a scenario (target location)
	actions_in_scenario = test_scenario_actions[scenario_num]
	for i in range(num_actions): # represents the step number in the episode
		# perform action for the step
		action = list(actions_in_scenario[i])
		print('The action is : ', action)
		reward = game.make_action(action)
		print('Performed action.')

		state = game.get_state()
		frame = state.screen_buffer # (240, 320, 3)
		frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) # (240, 320, 1)
		target_image_single_channel = frame
		target_image = np.append(frame, frame)
		target_image = np.append(target_image, target_image_single_channel)
		target_image = np.reshape(target_image, (240, 320, 3))

		# get the position of the agent in the environment
		pos_x = game.get_game_variable(GameVariable.POSITION_X)
		pos_y = game.get_game_variable(GameVariable.POSITION_Y)

		target_fname = test_path +  str(scenario_num) + '/' + str(i) + '_.png'
		res = cv2.imwrite(target_fname, target_image)
		print('Saving : ', target_fname)
		print('Saved target state.')
		time.sleep(0.1)
	# get the final position of the agent in the environment and write it to file
	final_x = game.get_game_variable(GameVariable.POSITION_X)
	final_y = game.get_game_variable(GameVariable.POSITION_Y)
	f = open(end_pos_fname, 'w')
	f.write(str(final_x) + ',' + str(final_y))
	f.close()

# create the test dataset (iterate through the scenarios and execute the actions that are considered as human demonstrations)
def create_human_demonstrations():
	test_scenario_actions = get_test_scenario_actions()
	for i in range(num_scenarios):
		get_target_states_for_scenario(i, test_scenario_actions)
		time.sleep(1)
	print('Done creating targets to be used as human demonstrations.')
	game.close()

create_human_demonstrations()


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

#   Widgets.py por:
#       Flavio Danesse <fdanesse@gmail.com>
#       Uruguay
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os

from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import GdkPixbuf

from Globales import get_separador
from Globales import get_pixels
from Globales import describe_archivo
from Globales import describe_uri
from Globales import get_colors

BASEPATH = os.path.dirname(__file__)

#GObject.threads_init()

class Toolbar(Gtk.Toolbar):

    __gtype_name__ = 'JAMediaConverterToolbar'

    __gsignals__ = {
    'salir': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, []),
    'load': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT, ))}

    def __init__(self):

        Gtk.Toolbar.__init__(self)

        self.modify_bg(0, get_colors("window"))

        self.insert(get_separador(draw=False,
            ancho=3, expand=False), -1)

        item = Gtk.ToolItem()
        item.set_expand(True)
        self.menu = Menu()
        self.menu.show()
        item.add(self.menu)
        self.insert(item, -1)
        '''
        archivo = os.path.join(BASEPATH,
            "Iconos", "salir.svg")
        boton = get_boton(archivo, flip=False,
            pixels=get_pixels(1))
        boton.set_tooltip_text("Salir")
        boton.connect("clicked", self.__salir)
        self.insert(boton, -1)
        '''
        self.insert(get_separador(draw=False,
            ancho=3, expand=False), -1)

        self.show_all()

        self.menu.connect(
            "load", self.__emit_load)

    def __emit_load(self, widget, archivos):

        self.emit('load', archivos)

    def __salir(self, widget):
        """
        Cuando se hace click en el boton salir.
        """

        self.emit('salir')

class Menu(Gtk.MenuBar):
    """
    Toolbar Principal.
    """

    __gtype_name__ = 'JAMediaConverterMenu'

    __gsignals__ = {
    'load': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT, ))}

    def __init__(self):

        Gtk.MenuBar.__init__(self)

        item_Procesar = Gtk.MenuItem('Cargar')
        menu_Procesar = Gtk.Menu()
        item_Procesar.set_submenu(menu_Procesar)
        self.append(item_Procesar)

        item = Gtk.MenuItem('Directorio ...')
        item.connect("activate", self.__emit_accion)
        menu_Procesar.append(item)

        item = Gtk.MenuItem('Archivos ...')
        item.connect("activate", self.__emit_accion)
        menu_Procesar.append(item)

        self.show_all()

    def __emit_accion(self, widget):

        valor = widget.get_label()

        mtype = ["audio/*", "video/*"]

        if valor == 'Directorio ...':
            filechooser = FileChooser(
                parent_window=self.get_toplevel(),
                title="Cargar Archivos",
                action=Gtk.FileChooserAction.SELECT_FOLDER,
                #path=path,
                mime=mtype,
                )

            filechooser.connect('load', self.__emit_load)

        elif valor == 'Archivos ...':
            filechooser = FileChooser(
                parent_window=self.get_toplevel(),
                title="Cargar Archivos",
                action=Gtk.FileChooserAction.OPEN,
                #path=path,
                mime=mtype,
                )

            filechooser.connect('load', self.__emit_load)

    def __emit_load(self, widget, archivos):

        self.emit('load', archivos)

class FileChooser(Gtk.FileChooserDialog):

    __gtype_name__ = 'JAMediaConverterFileChooser'

    __gsignals__ = {
    'load': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT, ))}

    def __init__(self,
        parent_window=None,
        filter_type=[],
        title=None,
        #path=None,
        action=Gtk.FileChooserAction.OPEN,
        mime=[]):

        Gtk.FileChooserDialog.__init__(self,
            parent=parent_window,
            action=action,
            flags=Gtk.DialogFlags.MODAL,
            title=title)

        self.action = action
        self.set_default_size(640, 480)
        self.modify_bg(0, get_colors("window"))

        #if os.path.isfile(path):
        #    self.set_filename(path)

        #else:
        #    self.set_current_folder_uri("file://%s" % path)

        if self.action == Gtk.FileChooserAction.OPEN:
            self.set_select_multiple(True)

            if filter_type:
                filter = Gtk.FileFilter()
                filter.set_name("Filtro")

                for fil in filter_type:
                    filter.add_pattern(fil)

                self.add_filter(filter)

            elif mime:
                filter = Gtk.FileFilter()
                filter.set_name("Filtro")

                for m in mime:
                    filter.add_mime_type(m)

                self.add_filter(filter)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

        abrir = Gtk.Button("Abrir")
        salir = Gtk.Button("Salir")

        hbox.pack_end(salir, True, True, 5)
        hbox.pack_end(abrir, True, True, 5)

        self.set_extra_widget(hbox)

        salir.connect("clicked", self.__salir)
        abrir.connect("clicked", self.__abrir)

        self.show_all()

        #self.connect("file-activated", self.__file_activated)

    #def __file_activated(self, widget):
    #    """
    #    Cuando se hace doble click sobre un archivo.
    #    """

    #    self.__abrir(None)

    def __abrir(self, widget):
        """
        Emite el path del archivo seleccionado.
        """

        files = self.get_filenames()

        if self.action == Gtk.FileChooserAction.SELECT_FOLDER:
            self.emit('load', files)

        elif self.action == Gtk.FileChooserAction.OPEN:
            if not files:
                self.__salir(None)
                return

            archivos = []
            for file in files:
                direccion = str(file).replace("//", "/")

                if os.path.exists(direccion) and os.path.isfile(direccion):
                    archivos.append(direccion)

            self.emit('load', archivos)

        self.__salir()

    def __salir(self, widget=None):

        self.destroy()

class Lista(Gtk.TreeView):
    """
    Lista generica.
    """

    __gsignals__ = {
    "nueva-seleccion": (GObject.SIGNAL_RUN_FIRST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT, ))}

    def __init__(self):

        Gtk.TreeView.__init__(self)

        self.set_property("rules-hint", True)
        self.set_headers_clickable(True)
        self.set_headers_visible(True)

        self.permitir_select = True
        self.valor_select = None

        self.modelo = Gtk.ListStore(
            GdkPixbuf.Pixbuf,
            GObject.TYPE_STRING,
            GObject.TYPE_STRING)

        self.__setear_columnas()

        self.treeselection = self.get_selection()
        self.treeselection.set_select_function(
            self.__selecciones, self.modelo)

        self.set_model(self.modelo)
        self.show_all()

    def __selecciones(self, treeselection,
        model, path, is_selected, listore):
        """
        Cuando se selecciona un item en la lista.
        """

        if not self.permitir_select:
            return True

        # model y listore son ==
        _iter = model.get_iter(path)
        valor = model.get_value(_iter, 2)

        if not is_selected and self.valor_select != valor:
            self.scroll_to_cell(model.get_path(_iter))
            self.valor_select = valor
            self.emit('nueva-seleccion', self.valor_select)

        return True

    def __setear_columnas(self):

        self.append_column(self.__construir_columa_icono('', 0, True))
        self.append_column(self.__construir_columa('Nombre', 1, True))
        self.append_column(self.__construir_columa('', 2, False))

    def __construir_columa(self, text, index, visible):

        render = Gtk.CellRendererText()

        columna = Gtk.TreeViewColumn(text, render, text=index)
        columna.set_sort_column_id(index)
        columna.set_property('visible', visible)
        columna.set_property('resizable', False)
        columna.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)

        return columna

    def __construir_columa_icono(self, text, index, visible):

        render = Gtk.CellRendererPixbuf()

        columna = Gtk.TreeViewColumn(text, render, pixbuf=index)
        columna.set_property('visible', visible)
        columna.set_property('resizable', False)
        columna.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)

        return columna

    def limpiar(self):

        self.permitir_select = False
        self.modelo.clear()
        self.permitir_select = True

    def agregar_items(self, elementos):
        """
        Recibe lista de: [texto para mostrar, path oculto] y
        Comienza secuencia de agregado a la lista.
        """

        self.get_toplevel().set_sensitive(False)
        self.permitir_select = False

        GLib.idle_add(self.__ejecutar_agregar_elemento, elementos)

    def __ejecutar_agregar_elemento(self, elementos):
        """
        Agrega los items a la lista, uno a uno, actualizando.
        """

        if not elementos:
            self.permitir_select = True
            self.seleccionar_primero()
            self.get_toplevel().set_sensitive(True)
            return False

        texto, path = elementos[0]

        descripcion = describe_uri(path)

        icono = None
        if descripcion:
            if descripcion[2]:
                # Es un Archivo
                tipo = describe_archivo(path)

                if 'video' in tipo or 'application/ogg' in tipo or \
                    'application/octet-stream' in tipo:
                    icono = os.path.join(BASEPATH,
                        "Iconos", "video.svg")

                elif 'audio' in tipo:
                    icono = os.path.join(BASEPATH,
                        "Iconos", "sonido.svg")

                else:
                    icono = os.path.join(BASEPATH,
                        "Iconos", "sonido.svg")

        else:
            icono = os.path.join(BASEPATH,
                "Iconos", "sonido.svg")

        try:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icono,
                get_pixels(0.8), -1)
            self.modelo.append([pixbuf, texto, path])

        except:
            pass

        elementos.remove(elementos[0])

        GLib.idle_add(self.__ejecutar_agregar_elemento, elementos)

        return False

    def seleccionar_siguiente(self, widget=None):

        modelo, _iter = self.treeselection.get_selected()

        try:
            self.treeselection.select_iter(modelo.iter_next(_iter))

        except:
            self.seleccionar_primero()

        return False

    def seleccionar_anterior(self, widget=None):

        modelo, _iter = self.treeselection.get_selected()

        try:
            self.treeselection.select_iter(modelo.iter_previous(_iter))

        except:
            self.seleccionar_ultimo()

        return False

    def seleccionar_primero(self, widget=None):

        self.treeselection.select_path(0)

    def seleccionar_ultimo(self, widget=None):

        model = self.get_model()
        item = model.get_iter_first()

        _iter = None

        while item:
            _iter = item
            item = model.iter_next(item)

        if _iter:
            self.treeselection.select_iter(_iter)
            #path = model.get_path(iter)


"""
@version: 1.1
@author: yipeihuai
@mail: yiph@ihep.ac.cn
@file: simu_so.py
@time: 2018/10/15 10:58
@modify time: 2018/11/27
"""

import numpy as np
import ctypes
import random
import sys
import logging
from decimal import Decimal
from ctypes import *

# extract cdf_sampling_wrapper function pointer in the shared object simu_so.so
def get_cdf_sampling_wrapper():
    dll = ctypes.CDLL('./simu_so.so', mode=ctypes.RTLD_GLOBAL)
    func = dll.CDF_Sampling_wrapper
    func.argtypes = [POINTER(c_double), POINTER(c_double), POINTER(c_double), POINTER(c_int), c_int, c_int, c_int, c_int]
    func.restype = c_float
    return func

def get_cdf_sampling_wrapper_C():
    dll = ctypes.CDLL('./simu_c.so', mode=ctypes.RTLD_GLOBAL)
    func = dll.CDF_Sampling_wrapper
    func.argtypes = [POINTER(c_double), POINTER(c_double), POINTER(c_double), c_int, c_int, c_int]
    func.restype = c_double
    return func

# create __cuda_cdf_sampling function with get_cdf_sampling_wrapper()
__cuda_cdf_sampling   = get_cdf_sampling_wrapper()
__cuda_cdf_sampling_C = get_cdf_sampling_wrapper_C()
# convenient python wrapper for __cuda_cdf_sampling
# it does all job with types convertation
# from python ones to C++ ones
def cuda_cdf_sampling(a, b, c, seed, total_num, size, n, time):
    a_p = a.ctypes.data_as(POINTER(c_double))
    b_p = b.ctypes.data_as(POINTER(c_double))
    c_p = c.ctypes.data_as(POINTER(c_double))
    seed_p = seed.ctypes.data_as(POINTER(c_int))

    return __cuda_cdf_sampling(a_p, b_p, c_p, seed_p, total_num, size, n, time)

def cuda_cdf_sampling_C(a, b, c, total_num, n, time):
    a_p = a.ctypes.data_as(POINTER(c_double))
    b_p = b.ctypes.data_as(POINTER(c_double))
    c_p = c.ctypes.data_as(POINTER(c_double))

    return __cuda_cdf_sampling_C(a_p, b_p, c_p, total_num, n, time)

def create_npe_histo(max_n):
    # max_n 最大光子数量
    _range = Decimal('100')
    prob = []
    for item in range(max_n-1):
        _prob = Decimal(random.randrange(0,_range,1))
        prob.append(Decimal(_prob/100))
        _range = _range - _prob
    prob.append(1-sum(prob))
    histo = [str(i) for i in prob]
    return histo

def create_hittime_histo(max_time):
    # max_time 最大击中时间
    _range = Decimal('100')
    prob_list = []
    prob = []
    for item in range(max_time-1):
        _prob = Decimal(random.randrange(0,_range,1))
        prob.append(Decimal(_prob/100))
        _range = _range - _prob
    prob.append(1-sum(prob))
    histo = [str(i) for i in prob]
    return histo

def create_seed():
    _seed =random.randint(0,100000000)
    return _seed

def run(pmt_num,loc_num):
    #数据生成
    # pmt_num = 500
    # loc_num = 100
    max_n = 10
    max_time = 10
    total = str(pmt_num * loc_num)
    count = 1
    temp_list_npe = []
    temp_list_hit = []
    temp_list_seed = []
    for i in range(loc_num):
        for j in range(pmt_num):
            temp_list_npe.append([0,0,0,0,0.5,0,0.2,0,0.3,0])
            temp_list_hit.append([0,0.2,0.2,0.1,0,0,0,0.2,0.2,0.1])
            temp_list_seed.append(create_seed())
            # print("当前进度" + str(count) + "/" + total)
            count += 1
    temp_np_npe = np.array(temp_list_npe).astype('double')
    temp_np_hit = np.array(temp_list_hit).astype('double')
    temp_np_seed = np.array(temp_list_seed).astype('int')
    # print("数据生成任务已完成")

    #假定两种表内存一致大小，即：max_n= max_time
    # 8 :sizeof(double) = 8
    size=int(pmt_num * loc_num *max_n*8 )

    result = np.zeros(size/8).astype('double')

    gputime = cuda_cdf_sampling(temp_np_npe, temp_np_hit, result, temp_np_seed, pmt_num*loc_num, size, max_n, max_time)
    # print("GPU执行结束")
    # logging.info(gputime)
    # print("CPU开始执行")
    cputime = cuda_cdf_sampling_C(temp_np_npe, temp_np_hit, result, pmt_num*loc_num,max_n,max_time)
    # print("CPU执行结束")
    # logging.info(cputime)

    return gputime,cputime
    #强制打印numpy数组全部元素
    # np.set_printoptions(threshold=np.nan)
    # for index in range(0,len(result),max_n):
    #     logging.info(result[index:index+max_n])

if __name__ == '__main__':
    logging.basicConfig(filename='logger.log', filemode='w', level=logging.INFO)
    if len(sys.argv)>3:
        begin = int(sys.argv[1])
        end = int(sys.argv[2])
        step = int(sys.argv[3])
        gpu_time_list = []
        cpu_time_list = []

        for i in range(begin,end+step,step):
            print("total num:"+str(i))
            logging.info("total num:"+str(i))
            gpu_time_list = []
            cpu_time_list = []
            for j in range(10):
                gpu,cpu = run(int(i/10),10)
                gpu_time_list.append(gpu)
                cpu_time_list.append(cpu)
            print("gpu time:"+str(sum(gpu_time_list)/10.0))
            logging.info("gpu time:"+str(sum(gpu_time_list)/10.0))
            print("cpu time:"+str(sum(cpu_time_list)/10.0))
            logging.info("cpu time:"+str(sum(cpu_time_list)/10.0))
            # print("gpu time:"+str(gpu))
            # logging.info("gpu time:"+str(gpu))
            # print("cpu time:"+str(cpu))
            # logging.info("cpu time:"+str(cpu))
    elif len(sys.argv)>2:
        pmt_num = int(sys.argv[1])
        loc_num = int(sys.argv[2])
        run(pmt_num,loc_num)
    else:
        pmt_num = 10
        loc_num = 2
        run(pmt_num,loc_num)


from model.task import Task
from config.config import storage_path, test_suits_folder_path
from model.task_status import TaskStatus
from model.response import Response
from dateutil import parser
from datetime import timedelta
import simplejson
import re

"""
This controller for json storage of Task entities

Provide all methods to manage with Task entities
"""

class DataManager:
    def __init__(self, next_id: int, tasks: [Task], file_path=None):
        """
        Constructor for DataManager object

        :param next_id: id of task, that will be created next
        :param tasks: list of Task entity;
        """
        self.next_id = next_id
        self.tasks = tasks
        self.storage_path = file_path if file_path is str else storage_path

    def __json__(self):
        """
        JSON serialization for DataManager object

        :return: dict with all inner fields
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> a.__json__().__str__() == open(test_suits_folder_path + "/test_suit1.txt", 'r').read()
        True
        """
        return {
            'next_id': self.next_id,
            'tasks': DataManager.tasks_to_json(self.tasks),
        }

    for_json = __json__  # supported by simplejson

    @classmethod
    def from_json(cls, json_obj):
        """
        Creating DataManager object from JSON object

        :param json_obj: JSON object (dict)
        :return: instance of DataManager
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> b = DataManager.from_json(a.__json__())
        >>> b.next_id
        2
        >>> b.tasks.__len__()
        1
        """
        return cls(json_obj['next_id'], DataManager.tasks_from_json(json_obj['tasks']))

    @staticmethod
    def tasks_from_json(storage_obj):
        """
        Deserialize Tasks from JSON object

        :param storage_obj: JSON serialized Tasks
        :return: list of Task entities
        >>> data_str = open(test_suits_folder_path + "/test_suit1.json", 'r').read()
        >>> storage_obj = simplejson.loads(data_str)['tasks']
        >>> a = DataManager.tasks_from_json(storage_obj)[0].__json__()
        >>> a['name']
        'Name'
        >>> a['date_start']
        '1999-08-28T21:03:05'
        """
        return list(map(lambda x: Task.from_json(x), storage_obj))

    @staticmethod
    def tasks_to_json(tasks):
        """
        Deserialize Tasks from JSON object

        :param tasks: JSON serialized Tasks
        :return: list of Task entities
        >>> arr = [Task(1, "Name", "nothing", parser.parse("1999-08-28T21:03:05"), \
            parser.parse("1999-08-28T05:55:23"), True)]
        >>> a = DataManager.tasks_to_json(arr)[0]
        >>> a['name']
        'Name'
        >>> a['date_start']
        '1999-08-28T21:03:05'
        >>> a['id']
        1
        """
        return list(map(lambda x: x.__json__(), tasks))

    @classmethod
    def load_from_file(cls, file_path: str):
        """
        Create DataManager instance from file with JSON data

        :param file_path: set explicitly file path of data storage
        :return: instance of DataManager
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> a.next_id
        2
        >>> a.tasks.__len__()
        1
        """
        data_str = open(file_path, 'r').read()
        storage_obj = simplejson.loads(data_str)
        instance = DataManager.from_json(storage_obj)
        instance.storage_path = file_path
        return instance

    def set_storage_path(self, file_path: str):
        """
        Set storage path for data

        :param file_path: file path to json file
        :return: None
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> prev = a.storage_path
        >>> a.set_storage_path("/path")
        >>> a.storage_path == "/path" and a.storage_path != prev
        True
        """
        self.storage_path = file_path

    def save_to_file(self, file_path: str):
        """
        Save changes(tasks ans next_id) in data file

        :param file_path: set explicitly file path of data storage
        :return: None
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> a.save_to_file(test_suits_folder_path + "/test_suit2.json")
        >>> b = open(test_suits_folder_path + "/test_suit1.json", 'r').read()
        >>> c = open(test_suits_folder_path + "/test_suit2.json", 'r').read()
        >>> b == c
        True
        """
        with open(file_path, 'w') as outfile:
            simplejson.dump(self, outfile, indent=4, for_json=True)

    def update_from_file(self, file_path: str):
        """
        Get changes( tasks ans next_id) from data file

        :param file_path: set explicitly file path of data storage
        :return: None
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> a.save_to_file(test_suits_folder_path + "/test_suit2.json")
        >>> a.change_task_status(1, False)
        True
        >>> b = DataManager.load_from_file(test_suits_folder_path + "/test_suit2.json")
        >>> a.tasks[0] != b.tasks[0]
        True
        >>> a.update_from_file(test_suits_folder_path + "/test_suit2.json")
        >>> a.tasks[0] == b.tasks[0]
        True
        >>> a.change_task_status(1, True)
        True
        >>> a.save_to_file(test_suits_folder_path + "/test_suit1.json")
        """
        data_str = open(file_path, 'r').read()
        storage_obj = simplejson.loads(data_str)
        self.next_id = storage_obj['next_id']
        self.tasks = DataManager.tasks_from_json(storage_obj['tasks'])

    def get_all(self, offset=0, limit=None, query=None):
        """
        Get all filtered and paginated tasks

        :param offset: count of skipping tasks
        :param limit: count of tasks in page
        :param query: search parameter
        :return: list of Task entities
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> f = a.get_all().tasks
        >>> task = Task(1, "Name", "nothing", parser.parse("1999-08-28T21:03:05"), \
            parser.parse("1999-08-28T05:55:23"), True)
        >>> f[0] == task
        True
        >>> a.get_all(offset=1).tasks.__len__()
        0
        """
        self.update_from_file(self.storage_path)
        return DataManager.__paginate_and_search__(self.tasks, offset, limit, query)

    def get_with_status(self, status: TaskStatus, offset=0, limit=None, query=None):
        """
        Get all filtered and paginated tasks with selected status

        :param status: selected status of Task
        :param offset: count of skipping tasks
        :param limit: count of tasks in page
        :param query: search parameter
        :return: list of Task entities
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> a.get_with_status(status=TaskStatus.UNCOMPLETED).tasks
        []
        >>> a.get_with_status(status=TaskStatus.COMPLETED).tasks.__len__()
        1
        """
        self.update_from_file(self.storage_path)
        filter_function = {
            TaskStatus.COMPLETED: lambda x: x.completed(),
            TaskStatus.UNCOMPLETED: lambda x: not x.completed(),
            TaskStatus.PLANNED: lambda x: not x.is_finished() and not x.is_running(),
            TaskStatus.RUNNING: lambda x: x.is_running(),
            TaskStatus.FINISHED: lambda x: x.is_finished(),
        }[status]
        result = list(filter(filter_function, self.tasks))
        return DataManager.__paginate_and_search__(result, offset, limit, query, status)

    def get_by_id(self, task_id: int):
        """
        Get Task entity with selected id

        :param task_id: id of searched Task
        :return: Task entity if task with selected id exists else False
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> task = Task(1, "Name", "nothing", parser.parse("1999-08-28T21:03:05"), \
            parser.parse("1999-08-28T05:55:23"), True)
        >>> a.get_by_id(1) == task
        True
        >>> a.get_by_id(-1)
        False
        """
        self.update_from_file(self.storage_path)
        for i, task in enumerate(self.tasks):
            if task.task_id == task_id:
                return self.tasks[i]
        return False

    def update_task(self, task_id: int, new_task: Task):
        """
        Update Task by id

        :param task_id: id of updating Task
        :param new_task: Task entity with updating info
        :return: True if entity was updated else
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit3.json")
        >>> task = Task(1, "TEST val", "nothing", parser.parse("1999-08-28T21:03:05"), \
            parser.parse("1999-08-28T05:55:23"), True)
        >>> a.update_task(1, task)
        True
        >>> a.get_by_id(1) == task
        True
        >>> task.name = "TEst N2"
        >>> a.update_task(1, task)
        True
        >>> a.get_by_id(1) == task
        True
        """
        self.update_from_file(self.storage_path)
        new_task.task_id = task_id
        for i, task in enumerate(self.tasks):
            if task.task_id == task_id:
                self.tasks[i] = new_task
                self.save_to_file(self.storage_path)
                return True
        return False

    def change_task_status(self, task_id: int, is_completed: bool):
        """
        Change Task status

        :param task_id: id of updating Task
        :param is_completed: Task status
        :return:True if entity was updated else False
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit3.json")
        >>> a.change_task_status(1, False)
        True
        >>> a.get_by_id(1).completed()
        False
        >>> a.change_task_status(1, True)
        True
        >>> a.get_by_id(1).completed()
        True
        >>> a.change_task_status(-23, True)
        False
        """
        self.update_from_file(self.storage_path)
        for i, task in enumerate(self.tasks):
            if task.task_id == task_id:
                self.tasks[i].is_completed = is_completed
                self.save_to_file(self.storage_path)
                return True
        return False

    def update_task_info(self, task_id: int, name: str, description: str, date_start: str, duration: int):
        """
        Update Task info

        :param task_id: id of updating Task
        :param name: updated name of Task
        :param description: updated description of Task
        :param date_start: updated start date of Task
        :param duration: updated duration of Task
        :return: True if entity was updated else False
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit4.json")
        >>> temp = a.get_by_id(1)
        >>> a.update_task_info(1, "tEsT", "-.-", "2018", 3600)
        True
        >>> task = Task(1, "tEsT", "-.-", parser.parse("2018-06-03T00:00:00"), \
            parser.parse("2018-06-03T01:00:00"), True)
        >>> a.get_by_id(1) == task
        True
        >>> a.update_task(1, temp)
        True
        >>> a.update_task(144, temp)
        False
        >>> a.update_task_info(-1, "tEsT", "-.-", "2018", 3600)
        False
        """
        self.update_from_file(self.storage_path)
        for i, task in enumerate(self.tasks):
            if task.task_id == task_id:
                self.tasks[i].name = name
                self.tasks[i].description = description
                self.tasks[i].date_start = parser.parse(date_start)
                self.tasks[i].date_end = parser.parse(date_start) + timedelta(seconds=duration)
                self.save_to_file(self.storage_path)
                return True
        return False

    def delete_task(self, task_id: int):
        """
        Delete Task

        :param task_id: id of deleted Task
        :return: True if entity was deleted else False
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit4.json")
        >>> temp = open(test_suits_folder_path + "/test_suit4.json", 'r').read()
        >>> a.delete_task(2)
        False
        >>> a.delete_task(1)
        True
        >>> file = open(test_suits_folder_path + "/test_suit4.json", "w")
        >>> file.write(temp)
        281
        >>> file.close()
        """
        self.update_from_file(self.storage_path)
        length = self.tasks.__len__()
        self.tasks = list(filter(lambda task: task.task_id != task_id, self.tasks))
        self.save_to_file(self.storage_path)
        return length != self.tasks.__len__()

    def add_task(self, task: Task):
        """
        Add new Task to storage

        :param task: new Task entity
        :return: added Task entity
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit5.json")
        >>> task = Task(1, "quest", "desc", parser.parse("2018"), parser.parse("2019"), False)
        >>> a.add_task(task) == task
        True
        """
        self.update_from_file(self.storage_path)
        task.task_id = self.next_id
        self.tasks.append(task)
        self.next_id += 1
        self.save_to_file(self.storage_path)
        return task

    def create_task(self, name: str, description: str, date_start: str, duration: int):
        """
        Create new Task

        :param name: name of new Task
        :param description: description of new Task
        :param date_start: start date of new Task
        :param duration: duration of new Task
        :return: created Task entity
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit5.json")
        >>> task = Task(1, "quest", "desc", parser.parse("2018"), parser.parse("2018-06-03T00:00:34"), False)
        >>> b = a.create_task("quest", "desc", "2018", 34)
        >>> task.task_id = a.next_id - 1
        >>> b == task
        True
        """
        self.update_from_file(self.storage_path)
        task = Task.create(self.next_id, name, description, date_start, duration)
        self.tasks.append(task)
        self.next_id += 1
        self.save_to_file(self.storage_path)
        return task

    @staticmethod
    def __paginate_array__(array, offset=0, limit=None):
        """
        Paginate list of Tasks

        :param array: list of Tasks
        :param offset: count of skipping tasks
        :param limit: count of Tasks in page
        :return: paginated list of Tasks
        >>> print(DataManager.__paginate_array__([1,2,3,4,5]))
        [1, 2, 3, 4, 5]
        >>> print(DataManager.__paginate_array__([1,2,3,4,5], offset=2))
        [3, 4, 5]
        >>> print(DataManager.__paginate_array__([1,2,3,4,5], offset=1, limit=3))
        [2, 3, 4]
        >>> print(DataManager.__paginate_array__([1,2,3,4,5], offset=-5, limit=0))
        [1, 2, 3, 4, 5]
        """
        if isinstance(offset, int) and offset >= 0 and ((isinstance(limit, int) and limit > 0) or limit is None):
            return array[offset:(limit + offset if limit is not None else None)]
        return array

    @staticmethod
    def __search_filter__(array: [Task], query=None):
        """
        Search Tasks in list

        :param array: list of Tasks
        :param query: searched parameter
        :return: list of Tasks that consist searched parameter
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> DataManager.__search_filter__(a.tasks).__len__()
        1
        >>> DataManager.__search_filter__(a.tasks, query="Just").__len__()
        0
        >>> DataManager.__search_filter__(a.tasks, query="Name").__len__()
        1
        >>> DataManager.__search_filter__(a.tasks, query="nam").__len__()
        1
        """
        if not isinstance(query, str) or query.__len__() == 0:
            return array
        return list(filter(lambda x: re.search(query, x.name + x.description, re.IGNORECASE), array))

    @staticmethod
    def __paginate_and_search__(array, offset=0, limit=None, query=None, status_filter=None):
        """
        Paginate list of Tasks and Search Tasks in it

        :param array: list of Tasks
        :param offset: count of skipping tasks
        :param limit: count of Tasks in page
        :param query: searched parameter
        :param status_filter: searched Task status
        :return: paginated list of Tasks that consist searched parameter
        >>> a = DataManager.load_from_file(test_suits_folder_path + "/test_suit1.json")
        >>> DataManager.__paginate_and_search__(a.tasks).tasks.__len__()
        1
        >>> DataManager.__paginate_and_search__(a.tasks, offset=3).tasks.__len__()
        0
        >>> DataManager.__paginate_and_search__(a.tasks, query="nothing").tasks.__len__()
        1
        """
        filtered = DataManager.__search_filter__(array, query)
        length = filtered.__len__()
        tasks_result = DataManager.__paginate_array__(filtered, offset, limit)
        return Response(tasks_result, offset, limit, query, length, status_filter)


import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
import d3dshot
import time
import pyautogui
from pynput import mouse

flag=False
score=0
limit=600

def on_click(x, y, button, pressed):
    global flag
    if button == mouse.Button.left:
        print('clicked')
        flag=True
        return False

def listener():
    listener = mouse.Listener(on_click=on_click)
    listener.start()
    listener.join()

def process(img):
    global score
    img2=img[150:670,300:680,:]
    h,w,_=img2.shape
    iw=w//4
    indxs=[(i*iw)+iw//2 for i in range(4)]
    cs=[0,1,2,3]
    points=[]
    for c in cs:
        mn=256
        slc=img2[:,indxs[c]-5:indxs[c]+5]
        slc=cv.cvtColor(slc,cv.COLOR_BGR2GRAY)
        slc=np.sum(slc,1)/10
        for i in range(h-1,-1,-1):
            if slc[i]<40 and slc[i]<mn:
                score+=1
                mn=slc[i]
                y=indxs[c]+300
                x=i+150
                if score>limit:
                    m=(score-limit)//10
                    y-=m
                points.append((y,x))
                break
    return points

d = d3dshot.create(capture_output="numpy")
listener()
while 1:
    if flag:
        img=d.screenshot()
        points=process(img)
        points.sort(key=lambda x:x[1],reverse=True)
        if len(points):
            print('location= ',points[0],
                  'score= ',score)
            pyautogui.click(points[0][0],points[0][1])
            if score<600:
                time.sleep(0.1)


from datetime import datetime, timedelta
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator

DAG_ID = "task_1"
defaults = {
    "catchup": False,
    "depends_on_past": False,
    "mutex_keys": None,
    "start_date": datetime.now() - timedelta(minutes=10)
}

def create_dag(dag):
    start = DummyOperator(dag=dag, task_id="start")
    t1 = BashOperator(bash_command="echo 'task_1 sleep'; sleep 1", task_id="task_1", dag=dag)
    t2 = BashOperator(bash_command="echo 'task_2 sleep'; sleep 1", task_id="task_2", dag=dag)
    t3 = BashOperator(bash_command="echo 'task_3 sleep'; sleep 1", task_id="task_3", dag=dag)

    start >> t1
    start >> t2
    start >> t3

# Dag is returned by a factory method
def sub_dag(parent_dag_name, child_dag_name, start_date, schedule_interval):
    dag = DAG(
        '%s.%s' % (parent_dag_name, child_dag_name),
        schedule_interval=schedule_interval,
        start_date=start_date,
    )

    create_dag(dag)
    return dag

dag = DAG(
    dag_id=DAG_ID,
    default_args=defaults,
    schedule_interval=None,
)

create_dag(dag)