import json
import math

class Pose:
    HOLD = "HOLD"
    TURN_RIGHT = "TURN_RIGHT"
    TURN_LEFT = "TURN_LEFT"
    THROTTLE_UP = "THROTTLE_UP"
    THROTTLE_DOWN = "THROTTLE_DOWN"
    FORWARD = "FORWARD"

class Point:
    def __init__(self, x, y, acc, index, desc):
        self.x = x
        self.y = y
        self.acc = acc
        self.index = index
        self.desc = desc

    def __str__(self):
        return "[x: " + str(self.x) + ", y: " + str(self.y) + ", acc: " + str(
            int(self.acc * 100)) + "%" + ", index: " + str(self.index) + ", desc: " + str(self.desc) + "]"

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

    def distance_to(self, point):
        return math.sqrt(((self.x - point.x) ** 2) + ((self.y - point.y) ** 2))

    def gradient_to(self, point):
        return (self.y - point.y) / (self.x - point.x)

class Skeleton:
    def __init__(self, frame, keypoints, required_accuracy):
        self.frame = frame
        self.keypoints = keypoints
        self.required_accuracy = required_accuracy

    def check_for_important_keypoints(self):
        if 8 not in self.keypoints:
            print("Hip not detected at frame " + str(self.frame) + "!")
            return False
        elif 8 in self.keypoints and self.keypoints[8].acc < self.required_accuracy:
            print("Detected hip with low accuracy of ~" + str(int(self.keypoints[8].acc * 100)) + "% at frame " +
                  str(self.frame) + "!")
            return False
        elif 2 not in self.keypoints:
            print("Right shoulder not detected at frame " + str(self.frame) + "!")
            return False
        elif 2 in self.keypoints and self.keypoints[2].acc < self.required_accuracy:
            print("Detected right shoulder with low accuracy of ~" +
                  str(int(self.keypoints[2].acc * 100)) + "% at frame " + str(self.frame) + "!")
            return False
        elif 5 not in self.keypoints:
            print("Left shoulder not detected at frame " + str(self.frame) + "!")
            return False
        elif 5 in self.keypoints and self.keypoints[5].acc < self.required_accuracy:
            print("Detected left shoulder with low accuracy of ~" +
                  str(int(self.keypoints[5].acc * 100)) + "% at frame " + str(self.frame) + "!")
            return False
        elif 4 not in self.keypoints:
            print("Right hand not detected at frame " + str(self.frame) + "!")
            return False
        elif 4 in self.keypoints and self.keypoints[4].acc < self.required_accuracy:
            print("Detected right hand with low accuracy of ~" +
                  str(int(self.keypoints[4].acc * 100)) + "% at frame " + str(self.frame) + "!")
            return False
        elif 7 not in self.keypoints:
            print("Left hand not detected at frame " + str(self.frame) + "!")
            return False
        elif 7 in self.keypoints and self.keypoints[7].acc < self.required_accuracy:
            print("Detected left hand with low accuracy of ~" +
                  str(int(self.keypoints[7].acc * 100)) + "% at frame " + str(self.frame) + "!")
            return False
        return True

    def transform_points(self):
        for key in self.keypoints:
            if self.keypoints[key].index is not 8:
                self.keypoints[key].x = self.keypoints[key].x - self.keypoints[8].x
                self.keypoints[key].y = self.keypoints[8].y - self.keypoints[key].y
        self.keypoints[8].x = 0
        self.keypoints[8].y = 0

class PoseDetector:
    def __init__(self, skeleton):
        self.skeleton = skeleton
        self.handGradient = None
        self.handDistance = None
        self.shoulderDistance = None
        self.handToShoulderDistance = None

    def calc_hand_gradient(self):
        if self.skeleton.keypoints[4] is not None and self.skeleton.keypoints[7] is not None:
            self.handGradient = self.skeleton.keypoints[7].gradient_to(self.skeleton.keypoints[4])
        else:
            self.handGradient = None

    def get_hand_gradient(self):
        if self.handGradient is None:
            self.calc_hand_gradient()
        return self.handGradient

    def calc_hand_distance(self):
        if self.skeleton.keypoints[4] is not None and self.skeleton.keypoints[7] is not None:
            self.handDistance = self.skeleton.keypoints[7].distance_to(self.skeleton.keypoints[4])
        else:
            self.handDistance = None

    def get_hand_distance(self):
        if self.handDistance is None:
            self.calc_hand_distance()
        return self.handDistance

    def calc_shoulder_distance(self):
        if self.skeleton.keypoints[5] is not None and self.skeleton.keypoints[2] is not None:
            self.shoulderDistance = self.skeleton.keypoints[5].distance_to(self.skeleton.keypoints[2])
        else:
            self.shoulderDistance = None

    def get_shoulder_distance(self):
        if self.shoulderDistance is None:
            self.calc_shoulder_distance()
        return self.shoulderDistance

    def calc_hand_to_shoulder_distance(self):
        if self.skeleton.keypoints[5] is not None and self.skeleton.keypoints[2] is not None and \
                self.skeleton.keypoints[7] is not None and self.skeleton.keypoints[4] is not None:
            right_distance = self.skeleton.keypoints[2].y - self.skeleton.keypoints[4].y
            left_distance = self.skeleton.keypoints[5].y - self.skeleton.keypoints[7].y
            self.handToShoulderDistance = (right_distance + left_distance) / 2
        else:
            self.handToShoulderDistance = None

    def get_hand_to_shoulder_distance(self):
        if self.handToShoulderDistance is None:
            self.calc_hand_to_shoulder_distance()
        return self.handToShoulderDistance

    def detect_pose(self):
        # Right turn: left hand up, right hand down
        #  => high hand gradient
        if self.get_hand_gradient() > 0.7:
            return Pose.TURN_RIGHT
        # Left turn: left hand down, right hand up
        #  => high negative hand gradient
        elif self.get_hand_gradient() < -0.7:
            return Pose.TURN_LEFT
        # Forward: both hands at shoulder height, close distance
        #  => low hand gradient, hands distance is close to shoulder distance, hands height is close to shoulders height
        elif -0.2 <= self.get_hand_gradient() <= 0.2 \
                and self.get_shoulder_distance() - 100 <= self.get_hand_distance() <= self.get_shoulder_distance() + 100 \
                and -50 <= self.get_hand_to_shoulder_distance() <= 50:
            return Pose.FORWARD
        # Throttle up: both hands high, close distance
        #  => hands height is higher than shoulder height, low hand gradient, low hands distance
        elif self.get_hand_to_shoulder_distance() <= -100 \
                and -0.2 <= self.get_hand_gradient() <= 0.2 \
                and self.get_shoulder_distance() - 50 <= self.get_hand_distance() <= self.get_shoulder_distance() + 250:
            return Pose.THROTTLE_UP
        # Throttle down: both hands low, close distance
        #  => hands height is lower than shoulder height, low hand gradient, low hands distance
        elif self.get_hand_to_shoulder_distance() > 0 \
                and -0.2 <= self.get_hand_gradient() <= 0.2 \
                and self.get_shoulder_distance() - 50 <= self.get_hand_distance() <= self.get_shoulder_distance() + 250:
            return Pose.THROTTLE_DOWN
        # Default:
        # => hold position
        else:
            return Pose.HOLD

def return_number(number):
    if number is None:
        return "None"
    else:
        return str(round(number, 2))

log_file = open("../data/OpenPose Demo#1/log_minified", "r")
content = log_file.read()
parsed_data = json.loads(content)
total_scanned = 0
missing_keypoints = 0
for frame_set in parsed_data:
    points = {}
    for keypoint in frame_set["points"]:
        points[keypoint["part"]] = Point(keypoint["x"], keypoint["y"], keypoint["accuracy"], keypoint["part"],
                                         keypoint["description"])
    skeleton = Skeleton(frame_set["index"], points, 0.4)
    total_scanned += 1
    if skeleton.check_for_important_keypoints():
        skeleton.transform_points()
        detector = PoseDetector(skeleton)
        pose = detector.detect_pose()
        print(pose + " (frame " + str(skeleton.frame) + " with HD " +
              return_number(detector.handDistance) + "/HG " + return_number(detector.handGradient) + "/SD " +
              return_number(detector.shoulderDistance) + "/HSD " + return_number(detector.handToShoulderDistance) + ")")
    else:
        missing_keypoints += 1

print("Detection rate: " + str(100 - round(100 * (missing_keypoints * 1.0 / total_scanned), 2)) + "% (" + str(
    total_scanned - missing_keypoints) + "/" + str(total_scanned) + ")")


""" Variational Approach for conformation dynamics (VAC)

This package contains basis sets, estimators and solvers for the variational approach for
conformation dynamics, a theory that has been proposed in [1] and was further developed in
[2] and [3]. The variational approach is analogous to the Ritz method [4] that is
employed in computational quantum chemistry. It differs in the way how the involved
matrices are computed and in the meaning of the involved operators, eigenfunctions and
eigenvalues - see [3] for a comparison.

Roughly, the idea of the VAC is as follows: Given a (classical)
molecular dynamics trajectory with configurations {x_1, ..., x_T}, and a
set of basis functions defined on the space of configurations {chi_1(x), ..., chi_n(x)},
we compute the two correlation matrices:

c_ij (0)   = < chi_i(x_t) chi_j(x_t) >_t
c_ij (tau) = < chi_i(x_t) chi_j(x_t+tau) >_t

where < . >_t is average over time t. Of course this can be generalized to many trajectories.
Then we solve the generalized eigenvalue problem

C(tau) r = C(0) r l(tau)

where the eigenvalues l(tau) approximate the dominant eigenvalues of the Markov propagator
or Markov backward propagator of the underlying dynamics. The corresponding eigenfunction
of the backward propagator is approximated by

psi(x) = sum_i r_i chi_i(x)

Package functionalities
-----------------------

This package aims at providing code to help addressing a number of key problems:

1. Basis sets for molecular dynamics (MD), and in particular protein dynamics. See [5] for a
   first approach in this direction.

2. Estimators for the corration matrices C(0), C(tau). The trivial time-average that is usually
   employed has a number of problems especially for many short simulation trajectories that are
   initiated far from the equilibrium distribution (the usual case!).

3. Solvers for accurately solving the eigenvalue problem above, even for huge basis sets.

At this time only a few of the above functionalities are implemented and we will go step by step.
This package will undergo heavy development and there is currently no date for an official
release, so don't be surprised if the API (the look + feel of functions and classes) change.
At the moment this package is purely intended for development purposes, so use it at your own
risk.

Applications
------------
1. The time-lagged independent component analysis (TICA) method originally developed in [6] and
   proposed as an optimal data transformation method for building Markov state models of MD
   in [3,7] is a VAC with mean-free basis functions. Therefore you can easily implement TICA with
   this package.

2. By transforming the internal coordinates such as torsion angles or interatomic distances into
   suitable basis functions, you can approximate experimentally-measurable relaxation timescales
   and determine the corresponding structural rearrangements for peptides and proteins [2,5]

3. ... more will follow.

References
----------
[1] Noe, F. and Nueske, F. (2013): A variational approach to modeling slow processes
    in stochastic dynamical systems. SIAM Multiscale Model. Simul. 11, 635-655.

[2] Nueske, F., Keller, B., Perez-Hernandez, G., Mey, A.S.J.S. and Noe, F. (2014)
    Variational Approach to Molecular Kinetics. J. Chem. Theory Comput. 10, 1739-1752.

[3] Perez-Hernandez, G., Paul, F., Giorgino, T., De Fabritiis, G. and Noe, F. (2013)
    Identification of slow molecular order parameters for Markov model construction.
    J. Chem. Phys. 139, 015102.

[4] Ritz, W. (1909): Ueber eine neue Methode zur Loesung gewisser
    Variationsprobleme der mathematischen Physik. J. Reine Angew. Math., 135, 1-61.

[5] Vitalini, F., Noe, F. and Keller, B. (2015): A basis set for peptides for the
    variational approach to conformational kinetics. (In review).

[6] Molgedey, L. and Schuster H. G. (1994): Phys. Rev. Lett. 72, 3634.

[7] Schwantes, C. R. and Pande, V. S. (2000): J. Chem. Theory Comput. 9, 2000

"""
from __future__ import print_function
import os
from setuptools import setup
from os.path import relpath, join
import subprocess

DOCLINES = __doc__.split("\n")

########################
VERSION = "0.0.0"
ISRELEASED = False
__version__ = VERSION
########################
CLASSIFIERS = """\
Development Status :: 3 - Alpha
Intended Audience :: Science/Research
Intended Audience :: Developers
License :: OSI Approved :: Open BSD clause 2 (OpenBSD)
Programming Language :: Python
Topic :: Scientific/Engineering :: Bio-Informatics
Topic :: Scientific/Engineering :: Chemistry
Topic :: Scientific/Engineering :: Physics
Operating System :: Microsoft :: Windows
Operating System :: POSIX
Operating System :: Unix
Operating System :: MacOS
"""

################################################################################
# Writing version control information to the module
################################################################################

def git_version():
    # Return the git revision as a string
    # copied from numpy setup.py
    def _minimal_ext_cmd(cmd):
        # construct minimal environment
        env = {}
        for k in ['SYSTEMROOT', 'PATH']:
            v = os.environ.get(k)
            if v is not None:
                env[k] = v
        # LANGUAGE is used on win32
        env['LANGUAGE'] = 'C'
        env['LANG'] = 'C'
        env['LC_ALL'] = 'C'
        out = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, env=env).communicate()[0]
        return out

    try:
        out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
        GIT_REVISION = out.strip().decode('ascii')
    except OSError:
        GIT_REVISION = 'Unknown'

    return GIT_REVISION

def write_version_py(filename='variational/version.py'):
    cnt = """
# This file is automatically generated by setup.py
short_version = '%(version)s'
version = '%(version)s'
full_version = '%(full_version)s'
git_revision = '%(git_revision)s'
release = %(isrelease)s

if not release:
    version = full_version
"""
    # Adding the git rev number needs to be done inside write_version_py(),
    # otherwise the import of numpy.version messes up the build under Python 3.
    FULLVERSION = VERSION
    if os.path.exists('.git'):
        GIT_REVISION = git_version()
    else:
        GIT_REVISION = 'Unknown'

    if not ISRELEASED:
        FULLVERSION += '.dev-' + GIT_REVISION[:7]

    a = open(filename, 'w')
    try:
        a.write(cnt % {'version': VERSION,
                       'full_version': FULLVERSION,
                       'git_revision': GIT_REVISION,
                       'isrelease': str(ISRELEASED)})
    finally:
        a.close()

################################################################################
# USEFUL SUBROUTINES
################################################################################

def find_package_data(data_root, package_root):
    files = []
    for root, dirnames, filenames in os.walk(data_root):
        for fn in filenames:
            files.append(relpath(join(root, fn), package_root))
    return files

################################################################################
# SETUP
################################################################################

write_version_py()

setup(
    name = 'variational',
    author = 'Frank Noe, Fabian Paul and Feliks Nueske',
    author_email = 'frank.noe@fu-berlin.de',
    description = DOCLINES[0],
    long_description = "\n".join(DOCLINES[2:]),
    version=__version__,
    license='OpenBSD',
    url='https://github.com/markovmodel/variational',
    platforms=['Linux', 'Mac OS-X', 'Unix', 'Windows'],
    classifiers=CLASSIFIERS.splitlines(),
    package_dir={'variational': 'variational'},
    packages=['variational', 'variational.basissets', 'variational.estimators', 'variational.solvers'],
    zip_safe=False,
    install_requires=[
        'numpy',
        'scipy',
        ],
    )


#coding: UTF-8
from requests_oauthlib import OAuth1Session
import os
import json
import codecs
import ConfigParser
import lxml.html
from bs4 import BeautifulSoup

def unescape(s):
  s = s.replace("<", "<")
  s = s.replace(">", ">")
  # this has to be last:
  s = s.replace("&", "&")
  return s

def readContent(path):
  f = open(path)
  content = f.read()
  f.close()
  return content

CONFIG_FILE = '/home/ec2-user/tweet_collect/add_setting.ini'
conf = ConfigParser.SafeConfigParser()
conf.read(CONFIG_FILE)
CK = conf.get('key','CK')
CS = conf.get('key','CS')
AT = conf.get('key','AT')
AS = conf.get('key','AS')
dirs = conf.get('url','dir')
template = conf.get('url','template')

twitter = OAuth1Session(CK,CS,AT,AS)

print("tset")
params = {'count':'200'}
req = twitter.get("https://api.twitter.com/1.1/statuses/home_timeline.json", params = params)
#req = twitter.get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=KirbyTrain&count=2",params=params)
timeline = json.loads(req.text)
html = readContent(template)
html = html.decode('utf-8')
soup = BeautifulSoup(html, "lxml")
#print(timeline)
#exit()

string =""
#thead_string = str(soup.find('thead', id="data1"))
#tbody_string = str(soup.find('tbody', id="data2"))
#exit()
for tweet in timeline:
    if tweet != "errors":
        string += "

"
        string += ""+ str(tweet["id"]) +  "
"
        string += "" + tweet["user"]["name"]+" / "+tweet["user"]["screen_name"] + "
"
        string += "" + tweet["created_at"] + "	"
        string += "	"
        string += "

"
        #tbody_string += ""
        string += "
" + tweet["text"] + "
"
        string += "

"
    else:
        print('error')
        os.system('python timeline.py')
        exit()

string = string + unicode(soup.find('table'))
soup.find('table').string = string
f = open(dirs,'w+')
f.write(unescape(str(soup)))

print('Completed write'+ dirs)
f.close()


# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# Define a class to receive the characteristics of each line detection
class Lane():
    def __init__(self):
        # 当前的图像
        self.current_warped_binary = None
        # 当前图片的尺寸
        self.current_warped_binary_shape = []

        # 检测到的车道线像素的横坐标 x values for detected line pixels
        self.allx = None

        # 检测到的车道线像素的纵坐标 y values for detected line pixels
        self.ally = None

        # 以纵坐标为自变量，取值空间
        self.ploty = None

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        # 是否检测到车道线 was the line detected in the last iteration?
        self.detected = False

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        # 保存的数据量
        self.n = 5

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        # 最近n个帧的拟合曲线 x values of the last n fits of the line
        self.recent_fitted_xs = []

        # 最近n个帧的平均拟合曲线 average x values of the fitted line over the last n iterations
        self.average_fitted_x = []

        # 当前帧的拟合曲线
        self.current_fitted_x = []

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        # 最近n个帧的拟合函数
        self.recent_fits = []

        # 最近n个帧的拟合函数 polynomial coefficients averaged over the last n iterations
        self.average_fit = []

        # 当前帧的拟合函数 polynomial coefficients for the most recent fit
        self.current_fit = [np.array([False])]

        # 拟合函数的误差 difference in fit coefficients between last and new fits
        self.diffs = np.array([0, 0, 0], dtype='float')

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        # 半径 radius of curvature of the line in some units
        self.radius_of_curvature = []

        # 车辆在车道线之间距离 distance in meters of vehicle center from the line
        self.line_base_pos = None

    # 对全新的帧进行车道线像素检测
    def find_lane_pixels(self, binary_warped, location):
        self.current_warped_binary = binary_warped
        self.current_warped_binary_shape = binary_warped.shape
        self.ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0])

        # Take a histogram of the bottom half of the image
        histogram = np.sum(binary_warped[binary_warped.shape[0] // 2:, :], axis=0)
        # Create an output image to draw on and visualize the result
        # out_img = np.dstack((binary_warped, binary_warped, binary_warped))
        # Find the peak of the left and right halves of the histogram
        # These will be the starting point for the left and right lines
        midpoint = np.int(histogram.shape[0] // 2)

        if location == "left":
            base = np.argmax(histogram[:midpoint])
        elif location == "right":
            base = np.argmax(histogram[midpoint:]) + midpoint

        # HYPERPARAMETERS
        # Choose the number of sliding windows
        nwindows = 9
        # Set the width of the windows +/- margin
        margin = 80
        # Set minimum number of pixels found to recenter window
        minpix = 50

        # Set height of windows - based on nwindows above and image shape
        window_height = np.int(binary_warped.shape[0] // nwindows)
        # Identify the x and y positions of all nonzero pixels in the image
        nonzero = binary_warped.nonzero()  # 扁平化后非零值点的列表
        nonzeroy = np.array(nonzero[0])
        nonzerox = np.array(nonzero[1])
        # Current positions to be updated later for each window in nwindows

        current = base

        # Create empty lists to receive left and right lane pixel indices
        lane_inds = []
        # right_lane_inds = []

        # Step through the windows one by one
        for window in range(nwindows):
            # Identify window boundaries in x and y (and right and left)
            win_y_low = binary_warped.shape[0] - (window + 1) * window_height
            win_y_high = binary_warped.shape[0] - window * window_height
            win_x_low = current - margin
            win_x_high = current + margin

            # # Draw the windows on the visualization image
            # cv2.rectangle(out_img, (win_xleft_low, win_y_low),
            #               (win_xleft_high, win_y_high), (0, 255, 0), 2)
            # cv2.rectangle(out_img, (win_xright_low, win_y_low),
            #               (win_xright_high, win_y_high), (0, 255, 0), 2)

            # 形成对每个像素的bool值
            # Identify the nonzero pixels in x and y within the window #
            good_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
                         (nonzerox >= win_x_low) & (nonzerox < win_x_high)).nonzero()[0]

            # Append these indices to the lists
            lane_inds.append(good_inds)

            # If you found > minpix pixels, recenter next window on their mean position
            if len(good_inds) > minpix:
                current = np.int(np.mean(nonzerox[good_inds]))

        # Concatenate the arrays of indices (previously was a list of lists of pixels)
        try:
            lane_inds = np.concatenate(lane_inds)
        except ValueError:
            # Avoids an error if the above is not implemented fully
            pass

        # Extract left and right line pixel positions
        x = nonzerox[lane_inds]
        y = nonzeroy[lane_inds]

        self.allx = x
        self.ally = y

        return x, y

    # 在之前的plot基础上找车道线
    def search_pixel_around_poly(self, binary_warped):

        self.current_warped_binary = binary_warped
        self.current_warped_binary_shape = binary_warped.shape
        self.ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0])

        # HYPERPARAMETER
        # Choose the width of the margin around the previous polynomial to search
        # The quiz grader expects 100 here, but feel free to tune on your own!
        margin = 80

        # Grab activated pixels
        nonzero = binary_warped.nonzero()
        nonzeroy = np.array(nonzero[0])
        nonzerox = np.array(nonzero[1])

        fit = self.recent_fits[-1]

        ### TO-DO: Set the area of search based on activated x-values ###
        ### within the +/- margin of our polynomial function ###
        ### Hint: consider the window areas for the similarly named variables ###
        ### in the previous quiz, but change the windows to our new search area ###
        lane_inds = ((nonzerox > (fit[0] * (nonzeroy ** 2) + fit[1] * nonzeroy + fit[2] - margin)) & (
                    nonzerox < (fit[0] * (nonzeroy ** 2) + fit[1] * nonzeroy + fit[2] + margin)))

        # Again, extract left and right line pixel positions
        x = nonzerox[lane_inds]
        y = nonzeroy[lane_inds]

        self.allx = x
        self.ally = y

        return x, y

    def fit_polynomial(self):
        ploty = self.ploty

        # Fit a second order polynomial to each using `np.polyfit`
        fit = np.polyfit(self.ally, self.allx, 2)

        # 存储当前结果
        self.current_fit = fit

        # 计算误差
        if len(self.recent_fits) == 0:
            self.diffs = [0,0,0]
        else:
            new = np.array(self.current_fit)
            old = np.array(self.recent_fits[-1])
            self.diffs = new - old

        # 存储为历史结果
        if len(self.recent_fits) < self.n:
            self.recent_fits.append(self.current_fit)
        elif len(self.recent_fits) == self.n:
            self.recent_fits.pop(0)
            self.recent_fits.append(self.current_fit)
        else:
            self.recent_fits.append(self.current_fit)
            self.recent_fits = self.recent_fits[-self.n:]  # 后面n个

        # 计算当前平均
        self.average_fit = np.array(self.recent_fits).mean(axis=0)

        try:
            x_fitted = self.average_fit[0] * ploty ** 2 + self.average_fit[1] * ploty + self.average_fit[2]
        except TypeError:
            # Avoids an error if `left` and `right_fit` are still none or incorrect
            print('The function failed to fit a line!')
            x_fitted = 1 * ploty ** 2 + 1 * ploty
            self.detected = False
        else:
            self.detected = True

        self.current_fitted_x = x_fitted

        # 存储为历史结果
        if len(self.recent_fitted_xs) < self.n:
            self.recent_fitted_xs.append(self.current_fitted_x)
        elif len(self.recent_fitted_xs) == self.n:
            self.recent_fitted_xs.pop(0)
            self.recent_fitted_xs.append(self.current_fitted_x)
        else:
            self.recent_fitted_xs.append(self.current_fitted_x)
            self.recent_fitted_xs = self.recent_fitted_xs[-self.n:]  # 后面n个

        self.average_fitted_x = np.array(self.recent_fitted_xs).mean(axis=0)

        return self.average_fitted_x

    def fit(self, binary_warped,location,sequence=True):
        if sequence:
            if not self.detected:
                # 没有检测到，重新开始检测
                self.find_lane_pixels(binary_warped,location)
            else:
                # 从上一次周围开始检测
                self.search_pixel_around_poly(binary_warped)
                # TODO 如果两次检测的误差较大怎么办？
                # TODO 是否存在

            self.fit_polynomial()
            # if np.abs(self.diffs).sum() > 20:
            #     self.current_fit = np.array(self.recent_fits[:-1]).mean(axis=0)
            #     self.recent_fits[-1] = self.current_fit
            #     self.average_fit = np.array(self.recent_fits).mean(axis=0)
            #
            #     self.current_fitted_x = np.array(self.recent_fitted_xs[:-1]).mean(axis=0)
            #     self.recent_fitted_xs[-1] = self.current_fitted_x
            #     self.average_fitted_x = np.array(self.recent_fitted_xs).mean(axis=0)
        else:
            self.find_lane_pixels(binary_warped, location)
            self.fit_polynomial()

    def measure_curvature_real(self,ploty, x, y):
        '''
        Calculates the curvature of polynomial functions in meters.
        '''
        # Define conversions in x and y from pixels space to meters
        ym_per_pix = 30 / 720  # meters per pixel in y dimension
        xm_per_pix = 3.7 / 700  # meters per pixel in x dimension

        fit_cr = np.polyfit(y * ym_per_pix, x * xm_per_pix, 2)

        # Define y-value where we want radius of curvature
        # We'll choose the maximum y-value, corresponding to the bottom of the image
        y_eval = np.max(ploty)

        # Calculation of R_curve (radius of curvature)
        curverad = ((1 + (2 * fit_cr[0] * y_eval * ym_per_pix + fit_cr[1]) ** 2) ** 1.5) / np.absolute(2 * fit_cr[0])

        self.radius_of_curvature = curverad

        return curverad

if __name__ == "__main__":
    from lane.perspective import perspective,src,dst
    from lane.gaussian_blur import gaussian_blur
    from lane.combined_threshold import combined_threshold
    from lane.measure_vehicle_pos import measure_vehicle_pos
    from lane.draw_lane import draw_lane

    image = mpimg.imread('../output_images/undistorted/straight_lines1-undistorted.jpg')

    image = gaussian_blur(image, 3)
    combined = combined_threshold(image, ksize=3,
                                  th=[[20, 100], [25, 254], [100, 250], [0.6, 1.2], [180, 254], [250, 0]])
    combined = gaussian_blur(combined, 3)
    perspectived_img = perspective(combined,src,dst)

    # plt.imshow(perspectived_img,cmap="gray")
    # plt.show()

    left_lane = Lane()
    left_lane.fit(perspectived_img,"left")

    right_lane = Lane()
    right_lane.fit(perspectived_img, "right")

    result = left_lane.visual(perspectived_img,"left")
    plt.imshow(result)
    result = right_lane.visual(perspectived_img, "right")
    plt.imshow(result)
    plt.show()
    # # 计算曲率
    # left_r = left_lane.measure_curvature_real(left_lane.ploty, left_lane.average_fitted_x, left_lane.ploty)
    # right_r = left_lane.measure_curvature_real(right_lane.ploty, right_lane.average_fitted_x, right_lane.ploty)
    #
    # # 计算偏移值
    # v = measure_vehicle_pos(left_lane.average_fitted_x, right_lane.average_fitted_x,left_lane.current_warped_binary_shape[1])
    #
    # # 绘制车道线
    # img = draw_lane(image, combined, dst, src,left_lane.current_fitted_x, right_lane.current_fitted_x, right_lane.ploty)

    # plt.imshow(img)

    # # 打印文字
    # plt.text(0,60,"Radius of Curvature = %d(m)"%int(r),fontdict={'size': 20, 'color': 'w'})
    # plt.text(0,120, "Vehicle is %.2f(m) left of center" % v, fontdict={'size': 20, 'color': 'w'})
    # plt.show()


import tkinter as tk
from tkinter import *
import re
import os
from tkinter.messagebox import *
from tkinter.filedialog import *
from tksheet import Sheet
from tkinter import ttk

import NIRA_Lexer as lexical
import NIRA_Parser as parser
import NIRA_Main as Main

class Notepad:
    __root = Tk()

    __thisWidth = 300
    __thisHeight = 300
    __thisTextArea = Text(__root)
    __thisMenuBar = Menu(__root)
    __thisFileMenu = Menu(__thisMenuBar, tearoff=0)
    __thisCompilerButton = Menu(__thisMenuBar,tearoff=0)
    __thisEditMenu = Menu(__thisMenuBar, tearoff=0)
    __thisHelpMenu = Menu(__thisMenuBar, tearoff=0)

    __thisScrollBar = Scrollbar(__thisTextArea)
    __file = None

    def __init__(self, **kwargs):

        try:
            self.__root.wm_iconbitmap("Notepad.ico")
        except:
            pass

        try:
            self.__thisWidth = kwargs['width']
        except KeyError:
            pass

        try:
            self.__thisHeight = kwargs['height']
        except KeyError:
            pass

        self.__root.title("Untitled")

        screenWidth = self.__root.winfo_screenwidth()
        screenHeight = self.__root.winfo_screenheight()

        left = (screenWidth / 2) - (self.__thisWidth / 2)

        top = (screenHeight / 2) - (self.__thisHeight / 2)

        self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth,
                                              self.__thisHeight,
                                              left, top))

        self.__root.grid_rowconfigure(0, weight=1)
        self.__root.grid_columnconfigure(0, weight=1)

        self.__thisTextArea.grid(sticky=N + E + S + W)

        self.__thisFileMenu.add_command(label="Novo",
                                        command=self.__newFile)

        self.__thisFileMenu.add_command(label="Abrir",
                                        command=self.__openFile)

        self.__thisFileMenu.add_command(label="Salvar",
                                        command=self.__saveFile)
        self.__thisFileMenu.add_separator()
        self.__thisFileMenu.add_command(label="Compilar",
                                       command=self.__compilar)
        self.__thisFileMenu.add_command(label="Tokens",
                                        command=self.__open_windows)

        self.__thisFileMenu.add_separator()
        self.__thisFileMenu.add_command(label="Fechar",
                                        command=self.__quitApplication)
        self.__thisMenuBar.add_cascade(label="Arquivo",
                                       menu=self.__thisFileMenu)

        self.__thisEditMenu.add_command(label="Cortar",
                                        command=self.__cut)

        self.__thisEditMenu.add_command(label="Copiar",
                                        command=self.__copy)

        self.__thisEditMenu.add_command(label="Colar",
                                        command=self.__paste)

        self.__thisMenuBar.add_cascade(label="Editar",
                                       menu=self.__thisEditMenu)

        self.__thisHelpMenu.add_command(label="Ajuda",
        command=self.__ajuda)
        self.__thisMenuBar.add_cascade(label="Ajuda",
                                       menu=self.__thisHelpMenu)

        self.__root.config(menu=self.__thisMenuBar)

        self.__thisScrollBar.pack(side=RIGHT, fill=Y)

        self.__thisScrollBar.config(command=self.__thisTextArea.yview)
        self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set)

    def __quitApplication(self):
        self.__root.destroy()
        exit()
    def __ajuda(self):
        root = Tk();
        root.geometry('250x300')

        text = Text(root)
        text.insert(INSERT,"Eu também preciso de ajuda!")
        text.pack()

    def __compilar(self):
        content = ""
        content2 = ""
        linhao = 0
        self.__file = askopenfilename(defaultextension=".txt",
                                      filetypes=[("All Files", "*.*"),
                                                 ("Text Documents", "*.txt")])
        y = os.path.basename(self.__file)

        with open(y,'r+', encoding="utf8") as file:
            for linha in file:
                linhao += 1
            content = file.readlines()

        x = Main.main(y)

        content2 = str(x).replace(",","\n").replace("[","").replace("]","").replace("'","").replace('"',"").replace("\\","/")

        with open("result.txt",'w+', encoding="utf8") as file1:
            file1.writelines("")
            content = file1.writelines(content2)

        self.__root.title(y)

        file = open("result.txt", "r+", encoding="utf8")

        root = Tk();
        root.geometry('500x600')

        text = Text(root,height=30,width=40)

        text.insert(INSERT,file.read())
        text.pack()

        file.close()

    def __openFile(self):

        self.__file = askopenfilename(defaultextension=".txt",
                                      filetypes=[("All Files", "*.*"),
                                                 ("Text Documents", "*.txt")])

        if self.__file == "":

            self.__file = None
        else:
            self.__root.title(os.path.basename(self.__file) + " - Notepad")
            self.__thisTextArea.delete(1.0, END)

            file = open(self.__file, "r", encoding="utf8")

            self.__thisTextArea.insert(1.0, file.read())

            file.close()

    def __newFile(self):
        self.__root.title("Untitled - Notepad")
        self.__file = None
        self.__thisTextArea.delete(1.0, END)

    def __open_windows(self):
        root = Tk();
        root.geometry("700x500")
        root.title("Tokens");

        treeview = ttk.Treeview(root)
        treeview.pack(fill="x");

        style = ttk.Style(root)
        style.configure("Treeview", rowheight=50)
        treeview.configure(style="Treeview")

        treeview.insert('',0,'1',text="Token (Palavra Reservada)")

        treeview.insert('',1,'2',text="Token (Identificador)")

        treeview.insert('',2,'3',text="Token (Double)")

        treeview.insert('',3,'4',text="Token (Inteiro)")

        treeview.insert('',4,'5',text="Token (Atribuição)")

        treeview.insert('',5,'6',text="Token (Agrupadores)")

        treeview.insert('',6,'7',text="Token (Comparador)")

        treeview.insert('',7,"8",text="Token (Comentário)")

        for i in lexical.reservadas:
            treeview.insert("1","end",text="PR : "+i)

        treeview.insert("2","end",text="Qualquer palavra que não contenha : @!#$%¨¨&*()")

        treeview.insert("5","end",text="=")

        treeview.insert("8","end",text="Comentário : #")

    def __saveFile(self):

        if self.__file == None:
            self.__file = asksaveasfilename(initialfile='Untitled.txt',
                                            defaultextension=".txt",
                                            filetypes=[("All Files", "*.*"),
                                                       ("Text Documents", "*.txt")])

            if self.__file == "":
                self.__file = None
            else:

                file = open(self.__file, "w", encoding="utf8")
                file.write(self.__thisTextArea.get(1.0, END))
                file.close()

                self.__root.title(os.path.basename(self.__file) + " - Notepad")

        else:
            file = open(self.__file, "w")
            file.write(self.__thisTextArea.get(1.0, END))
            file.close()

    def __cut(self):
        self.__thisTextArea.event_generate("<<Cut>>")

    def __copy(self):
        self.__thisTextArea.event_generate("<<Copy>>")

    def __paste(self):
        self.__thisTextArea.event_generate("<<Paste>>")

    def run(self):

        self.__root.mainloop()

def doidao1():
    notepad = Notepad(width=800, height=600)
    notepad.run()


import numpy as np
import cv2
from scipy.io import loadmat
from os import listdir
from os.path import isfile, join
import os
from collections import namedtuple
import tensorflow as tf
import sys

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('colored_labels',
                           './gta/labels/',
                           'Path to directory where colored labels exist.')

tf.app.flags.DEFINE_string(
    'trainID_dir',
    './gta/trainIDs/',
    'Path to save train id files.')

#--------------------------------------------------------------------------------
# Definitions
#--------------------------------------------------------------------------------

# a label and all meta information
Label = namedtuple( 'Label' , [

    'name'        , # The identifier of this label, e.g. 'car', 'person', ... .
                    # We use them to uniquely name a class

    'id'          , # An integer ID that is associated with this label.
                    # The IDs are used to represent the label in ground truth images
                    # An ID of -1 means that this label does not have an ID and thus
                    # is ignored when creating ground truth images (e.g. license plate).

    'trainId'     , # An integer ID that overwrites the ID above, when creating ground truth
                    # images for training.
                    # For training, multiple labels might have the same ID. Then, these labels
                    # are mapped to the same class in the ground truth images. For the inverse
                    # mapping, we use the label that is defined first in the list below.
                    # For example, mapping all void-type classes to the same ID in training,
                    # might make sense for some approaches.

    'category'    , # The name of the category that this label belongs to

    'categoryId'  , # The ID of this category. Used to create ground truth images
                    # on category level.

    'hasInstances', # Whether this label distinguishes between single instances or not

    'ignoreInEval', # Whether pixels having this class as ground truth label are ignored
                    # during evaluations or not

    'color'       , # The color of this label
    ] )

#--------------------------------------------------------------------------------
# A list of all labels
#--------------------------------------------------------------------------------

# Please adapt the train IDs as appropriate for you approach.
# Note that you might want to ignore labels with ID 255 during training.
# Make sure to provide your results using the original IDs and not the training IDs.
# Note that many IDs are ignored in evaluation and thus you never need to predict these!

labels = [
    #       name                     id    trainId   category            catId     hasInstances   ignoreInEval   color
    Label(  'unlabeled'            ,  0 ,      255 , 'void'            , 0       , False        , True         , (  0,  0,  0) ),
    Label(  'ego vehicle'          ,  1 ,      255 , 'void'            , 0       , False        , True         , (  0,  0,  0) ),
    Label(  'rectification border' ,  2 ,      255 , 'void'            , 0       , False        , True         , (  0,  0,  0) ),
    Label(  'out of roi'           ,  3 ,      255 , 'void'            , 0       , False        , True         , (  0,  0,  0) ),
    # Label(  'static'               ,  4 ,      255 , 'void'            , 0       , False        , True         , (  0,  0,  0) ),
    Label(  'static'               ,  4 ,      255 , 'void'            , 0       , False        , True         , (  20,  20,  20) ), # in gta dataset it has a different color
    Label(  'dynamic'              ,  5 ,      255 , 'void'            , 0       , False        , True         , (111, 74,  0) ),
    Label(  'ground'               ,  6 ,      255 , 'void'            , 0       , False        , True         , ( 81,  0, 81) ),
    Label(  'road'                 ,  7 ,        0 , 'ground'          , 1       , False        , False        , (128, 64,128) ),
    Label(  'sidewalk'             ,  8 ,        1 , 'ground'          , 1       , False        , False        , (244, 35,232) ),
    Label(  'parking'              ,  9 ,      255 , 'ground'          , 1       , False        , True         , (250,170,160) ),
    Label(  'rail track'           , 10 ,      255 , 'ground'          , 1       , False        , True         , (230,150,140) ),
    Label(  'building'             , 11 ,        2 , 'construction'    , 2       , False        , False        , ( 70, 70, 70) ),
    Label(  'wall'                 , 12 ,        3 , 'construction'    , 2       , False        , False        , (102,102,156) ),
    Label(  'fence'                , 13 ,        4 , 'construction'    , 2       , False        , False        , (190,153,153) ),
    Label(  'guard rail'           , 14 ,      255 , 'construction'    , 2       , False        , True         , (180,165,180) ),
    Label(  'bridge'               , 15 ,      255 , 'construction'    , 2       , False        , True         , (150,100,100) ),
    Label(  'tunnel'               , 16 ,      255 , 'construction'    , 2       , False        , True         , (150,120, 90) ),
    Label(  'pole'                 , 17 ,        5 , 'object'          , 3       , False        , False        , (153,153,153) ),
    Label(  'polegroup'            , 18 ,      255 , 'object'          , 3       , False        , True         , (153,153,153) ),
    Label(  'traffic light'        , 19 ,        6 , 'object'          , 3       , False        , False        , (250,170, 30) ),
    Label(  'traffic sign'         , 20 ,        7 , 'object'          , 3       , False        , False        , (220,220,  0) ),
    Label(  'vegetation'           , 21 ,        8 , 'nature'          , 4       , False        , False        , (107,142, 35) ),
    Label(  'terrain'              , 22 ,        9 , 'nature'          , 4       , False        , False        , (152,251,152) ),
    Label(  'sky'                  , 23 ,       10 , 'sky'             , 5       , False        , False        , ( 70,130,180) ),
    Label(  'person'               , 24 ,       11 , 'human'           , 6       , True         , False        , (220, 20, 60) ),
    Label(  'rider'                , 25 ,       12 , 'human'           , 6       , True         , False        , (255,  0,  0) ),
    Label(  'car'                  , 26 ,       13 , 'vehicle'         , 7       , True         , False        , (  0,  0,142) ),
    Label(  'truck'                , 27 ,       14 , 'vehicle'         , 7       , True         , False        , (  0,  0, 70) ),
    Label(  'bus'                  , 28 ,       15 , 'vehicle'         , 7       , True         , False        , (  0, 60,100) ),
    Label(  'caravan'              , 29 ,      255 , 'vehicle'         , 7       , True         , True         , (  0,  0, 90) ),
    Label(  'trailer'              , 30 ,      255 , 'vehicle'         , 7       , True         , True         , (  0,  0,110) ),
    Label(  'train'                , 31 ,       16 , 'vehicle'         , 7       , True         , False        , (  0, 80,100) ),
    Label(  'motorcycle'           , 32 ,       17 , 'vehicle'         , 7       , True         , False        , (  0,  0,230) ),
    Label(  'bicycle'              , 33 ,       18 , 'vehicle'         , 7       , True         , False        , (119, 11, 32) ),
    Label(  'license plate'        , -1 ,       -1 , 'vehicle'         , 7       , False        , True         , (  0,  0,142) ),
]

# annotation_folder = '/home/mumu01/Downloads/datasets/gta/labels/'

def create_folders(fol_name):
    if not (os.path.exists(fol_name)):
        os.makedirs(fol_name)

annotation_folder = FLAGS.colored_labels
trainID_folder = FLAGS.trainID_dir

create_folders(trainID_folder)

considered_labels = [label for label in labels if not (label.ignoreInEval)]
not_considered_labels = [label for label in labels if (label.ignoreInEval)]

def get_file_names(dir_name):
    only_files = [f for f in listdir(dir_name) if isfile(join(dir_name, f))]
    only_files.sort()
    return only_files

def get_gtFine_labelTrainIds(color_annotated_img):
    # create similar size image as annotation

    r, c, _ = np.shape(color_annotated_img)
    labelTrainIds_img = np.zeros((r, c), dtype=np.uint8)

    # handle objects which needs to be considered by giving them same id as Train Id
    for l in considered_labels:
        lookup_color = l.color
        lookup_color = (
            lookup_color[2], lookup_color[1],
            lookup_color[0])  # switching blue and red channel of color to synchronize.

        lower = np.array(lookup_color, dtype="uint8")
        upper = np.array(lookup_color, dtype="uint8")

        # find the colors within the specified boundaries and apply
        # the mask
        mask = cv2.inRange(color_annotated_img, lower, upper)
        labelTrainIds_img[mask != 0] = l.trainId

    # handle objects which are not getting considered and assign them a different id
    for l in not_considered_labels:
        if not (l.name == 'license plate') and not (l.name == 'polegroup'):
            lookup_color = l.color
            lookup_color = (
                lookup_color[2], lookup_color[1],
                lookup_color[0])  # switching blue and red channel of color to synchronize.

            lower = np.array(lookup_color, dtype="uint8")
            upper = np.array(lookup_color, dtype="uint8")

            # find the colors within the specified boundaries and apply
            # the mask
            mask = cv2.inRange(color_annotated_img, lower, upper)
            labelTrainIds_img[mask != 0] = 255  # trainID for unlabeled

    return labelTrainIds_img

# depreciated
def get_colored_img_from_gtFine_labelTrainIds(labelTrainIds_img):
    r, c = np.shape(labelTrainIds_img)
    colored_img = np.zeros((r, c, 3), dtype=np.uint8)#, 255
    for l in labels:
        if not (l.trainId == 255) and not ((l.trainId == -1)):
            lookup_trainID = l.trainId
            lower = np.array(lookup_trainID, dtype="uint8")
            upper = np.array(lookup_trainID, dtype="uint8")

            mask = cv2.inRange(labelTrainIds_img, lower, upper)

            assign_color = l.color
            assign_color = (
                assign_color[2], assign_color[1],
                assign_color[0])  # switching blue and red channel of color to synchronize.

            colored_img[np.where(mask == [255])] = assign_color

    return colored_img

# annotation_folder = FLAGS.colored_labels

# server = "/run/user/477015036/gvfs/sftp:host=sonic.sb.dfki.de,user=mumu01"
# annotation_folder = server+ "/home/mumu01/models/deeplab/datasets/gta/labels"
#
# trainID_folder = server+ "/home/mumu01/models/deeplab/datasets/gta/trainIDs"
# create_folders(trainID_folder)

file_names = get_file_names(annotation_folder)

# file_names = ['00002.png', '00003.png', '00005.png']
num_files = len(file_names)

sample_annotation_files = file_names[:num_files]

for idx, s in enumerate(sample_annotation_files):
    print("\rImages Processed: {}".format(idx + 1), end=' ')
    sys.stdout.flush()

    full_path = join(annotation_folder, s)
    color_annotated_img = cv2.imread(full_path, -1)

    labelTrainIds_img = get_gtFine_labelTrainIds(color_annotated_img)

    # colored_back = get_colored_img_from_gtFine_labelTrainIds(labelTrainIds_img)
    #
    # cv2.imshow("original", color_annotated_img)
    # cv2.imshow("temp", colored_back)
    # cv2.waitKey()
    cv2.imwrite(join(trainID_folder, s),
                labelTrainIds_img, [cv2.IMWRITE_PNG_COMPRESSION, 9])  # need to make sure if saving doesn't effect the values in side


#    Copyright 2016 Stefan Steidl
#    Friedrich-Alexander-Universität Erlangen-Nürnberg
#    Lehrstuhl für Informatik 5 (Mustererkennung)
#    Martensstraße 3, 91058 Erlangen, GERMANY
#    stefan.steidl@fau.de

#    This file is part of the Python Classification Toolbox.
#
#    The Python Classification Toolbox 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 3 of the License, or (at your option) any later version.
#
#    The Python Classification Toolbox 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 the Python Classification Toolbox.
#    If not, see <http://www.gnu.org/licenses/>.

import cvxopt
import numpy

class KernelSVM(object):

	def __init__(self, C = 1.0, gamma = 0.5):
		self.C = C
		self.gamma = gamma
		self.__initialized = False

	def fit(self, X, y):
		y = y.astype(numpy.int)
		classes = list(set(y))

		self.__initialized = False
		assert len(classes) == 2, "This Kernel SVM implementation can handle only 2-class problems!"

		self.__mapping = {}
		self.__mapping[-1] = classes[0]
		self.__mapping[+1] = classes[1]
		y[y == classes[0]] = -1
		y[y != -1] = +1

		m, d = X.shape
		self.a = numpy.zeros(d + 1 + m)

		# set up Gram matrix
		# K = numpy.empty((m, m))
		# for i in range(m):
		#	 K[i, :] = self.__kernel(X[i], X)
		K = self.GaussianRBFKernelMatrix(X, X)

		# quadratic optimization problem: 0.5 * x^T P x + q^T x
		P = cvxopt.matrix(numpy.outer(y, y) * K)
		q = cvxopt.matrix(-1 * numpy.ones(m))

		# lower and upper bounds written as inequality constraints: Gx <= h
		G1 = numpy.diag(-1 * numpy.ones(m))
		G2 = numpy.diag(numpy.ones(m))
		G = cvxopt.matrix(numpy.vstack((G1, G2)))
		h1 = numpy.zeros(m)
		#only need the C constraint if doing soft margin
		h2 = self.C * numpy.ones(m)
		h = cvxopt.matrix(numpy.append(h1, h2))

		# equality constraints: Ax = b
		A = cvxopt.matrix(1.0*y, (1, m))
		b = cvxopt.matrix(0.0)

		# solve quadratic programming problem
		solution = cvxopt.solvers.qp(P, q, G, h, A, b)

		# Lagrange multipliers
		lagrange_multipliers = numpy.ravel(solution['x'])

		# support vectors
		sv_indices = lagrange_multipliers > 0.001 * self.C
		self.support_multipliers = lagrange_multipliers[sv_indices]
		self.support_vectors = X[sv_indices]
		self.support_vector_labels = y[sv_indices]
		print("Kernel SVM: {0} support vectors".format(len(self.support_vector_labels)))

		# bias computation
		self.__initialized = True
		self.__bias = 0.0
		self.__bias = numpy.mean(self.support_vector_labels - self.predict(self.support_vectors, mapping = False))

	def GaussianRBFKernelMatrix(self, X1, X2):
		# exp(- gamma * |x-y|^2)
		# |x-y|^2 = (x - y)^T (x - y) = - 2 * x^T y + x^T x + y^T y
		d1 = numpy.square(X1).sum(axis = 1)
		d2 = numpy.square(X2).sum(axis = 1)
		K = numpy.dot(X1, X2.T)
		K *= -2
		K += d1.reshape(-1, 1)
		K += d2
		K *= - self.gamma
		return numpy.exp(K, K) # in-place exponentiation

	def predict(self, X, mapping = True):
		if not self.__initialized:
			return None

		Z = self.GaussianRBFKernelMatrix(X, self.support_vectors)
		Z *= self.support_multipliers * self.support_vector_labels
		Z = Z.sum(axis = 1)
		Z += self.__bias

		if mapping:
			Z[Z > 0] = self.__mapping[+1]
			Z[Z <= 0] = self.__mapping[-1]

		return Z


import sys
import re
import requests

# The purpose of main() is to test the functions
def main(args):
    with open(args[1]) as infile:
        #print('\n'.join(getTableHeaders(infile.read())))
        tables = get_tables(infile.read(),{'summary':'Results'})
        print(len(tables))
        filterCrit = {'Type':['10-Q','EX-10.1'],
                'Description':'Complete submission text file'}
        for t in tables:
            headers = get_th(t)
            print(headers)
            filter_table(t,cols=filterCrit)

def filter_table(table,cols={},row={}):
    table = table_to_dicts(table)
    res = []
    for row in table:
        for col in iter(cols):
            if row[col] in cols[col]:
                res.append(row)
    return res

def decode_table(table, removeTH=False):
    res = re.findall(r'
',table,flags=re.DOTALL)
    for i in range(0,len(res)):
        res[i] = [re.sub(r'|	','',x,flags=re.DOTALL).strip()
                for x in re.findall(r'',res[i],flags=re.DOTALL)]
    # this is unsafe! needs to be fixed
    if removeTH:
        return res[1:]
    return res

def decode_tr(row):
    res = get_td(row)

def table_to_dicts(table):
    headers = get_th(table)
    res = re.findall(r'
',table,flags=re.DOTALL)[1:]
    for i in range(len(res)):
        res[i] = dict(zip(headers, [re.sub(r'|	','',x,flags=re.DOTALL).strip()
                for x in re.findall(r'',res[i],flags=re.DOTALL)]))
    return res

def get_td(table):
    res = re.findall(r'',table,flags=re.DOTALL)
    for i in range(0,len(res)):
        res[i] = re.sub(r'|	','',res[i],flags=re.DOTALL)
    return res

def get_tr(table):
    res = []
    for row in re.findall(r'
',table,flags=re.DOTALL):
        res.append(re.sub(r'
|
','',row,flags=re.DOTALL))
    return res

def iter_tr(table):
    for rowMatch in re.finditer(r'
',table,flags=re.DOTALL):
        yield re.sub(r'
|
','',table[rowMatch.start():rowMatch.end()],flags=re.DOTALL)

def get_th(s):
    res = re.findall(r'<th.*?	',s,flags=re.DOTALL)
    for i in range(0,len(res)):
        res[i] = re.sub(r'<.*?>','',res[i],flags=re.DOTALL)
    return res

def get_tables(s, tags={}):
    if not tags:
        return re.findall(r'<table.*?
',s,flags=re.DOTALL)
    res = []
    for table in re.findall(r'<table.*?
',s,flags=re.DOTALL):
        if check_tags(table,tags):
            res.append(table)
    return res

def iter_tables(s, tags={}):
    for tableMatch in re.finditer(r''.format(key,tags[key])
        pat=re.compile(tmp,flags=re.DOTALL)
        if not re.search(pat,s):
            # print('tags not found')
            return False
    return True

def is_link(s):
    if re.match(r'<a.*?',s):
        return True
    return False

def get_link_text(s):
    res = re.findall(r'',s,flags=re.DOTALL)
    for i in range(0,len(res)):
        res[i] = re.sub(r'|','',res[i],flags=re.DOTALL)
    return res

def get_ul_as_list(s):
    res = re.findall(r'
',s,flags=re.DOTALL)
    for i in range(0,len(res)):
        res[i] = re.sub(r'
|
','',res[i],flags=re.DOTALL)
    return res

def get_link(s):
    m = re.search(r'href=".*?"',s)
    if m:
        return s[m.start()+6:m.end()-1]

def strip_tags(s):
    return re.sub('<.*?>|</.*?>| ','',s,flags=re.DOTALL)

if __name__ == '__main__':
    main(sys.argv)


from flask import Flask
import json
import requests
app = Flask(__name__)

@app.route("/")
def hello():
    url = 'https://private-endpoint-dot-net.azurewebsites.net'
    r = requests.get(url = url)
    data = r.text
    print(r.status_code)
    print(data)
    output = "Hello, World with {} & {} from {}!".format(r.text, r.status_code, url)
    return output


''' Faça um programa que leia o peso de cinco pessoas. No final, mostre qual foi o maior e o menor peso lidos. '''

maior = 0
menor = 0
for c in range(1, 6):
    peso = int(input(f'Digite o {c}º peso: '))
    if c == 1:
        maior = peso
        menor = peso
    else:
        if peso > maior:
            maior = peso
        if peso < menor:
            menor = peso
print(f'{maior} for o maior peso')
print(f'{menor} foi o menor peso')


# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
import os,sys
import subprocess
import shutil

__project__="QgsGisAnalyzer"
__author__="Kai Moriguchi"
__version__="0.0.0"

APIDOC_PATH = r"C:\Python27\Scripts\sphinx-apidoc.exe"
BUILD_PATH="C:\Python27\Scripts\sphinx-build.exe"
if __name__ == '__main__':
    _src = os.path.split(sys.argv[0])[0]
    _sphinx = _src.replace('src','sphinx')
    _doc = _src.replace('src','doc')
    print (subprocess.check_output([APIDOC_PATH, '-f', '-F','-H',__project__,'-A',__author__, '-V', __version__, '-R',__version__, '-o', _sphinx, _src]))
    print(subprocess.check_output([BUILD_PATH, '-a','-b', 'html', _sphinx, _doc]))
    shutil.rmtree(_sphinx)


"""
scouter ip 插件
下载dbip的mmdb数据
mmdb的控制由autotask控制
create by judy 2019/08/28
新增直接使用GeoIPLoc by judy 2020/04/23
"""
import traceback

from datacontract import EClientBusiness
from ....config_client import basic_client_config

clientbusiness = eval(basic_client_config.clientbusiness)
if (
    EClientBusiness.ALL.value in clientbusiness
    or EClientBusiness.IScanTask.value in clientbusiness
):
    from geoiploc.geoiploc import GeoIPLoc

from commonbaby.countrycodes import ALL_COUNTRIES, CountryCode
from commonbaby.mslog import MsLogger, MsLogManager

from ....clientdatafeedback.scoutdatafeedback import GeoInfo

class DbipMmdb(object):
    def __init__(self):
        self._logger: MsLogger = MsLogManager.get_logger("DBIP-mmdb")

    def format_string(self, instr):
        """
        有些国家的中文出现了带有分号的情况，
        这里统一处理下
        20200927 目前只有国家出现了这个数据
        """
        res = None
        try:

            if instr is None or instr == "":
                return
            res_strip: str = instr.strip()
            if res_strip.endswith(";"):
                res = res_strip[: res_strip.index(";")]
            else:
                res = res_strip
        except:
            self._logger.error(
                f"Format dbipinfo {instr} error, err:{traceback.format_exc()}"
            )
        return res

    def get_ip_mmdbinfo(self, level: int, ip: str):
        """
        根据ip去查询所在的地址
        :param level:
        :param ip:
        :return:
        """
        res: GeoInfo = None
        org = None
        isp = None
        res_dict = None
        try:
            res_dict = GeoIPLoc.get_ip_location(ip)
            # 新镇获取isp和org信息
            traits_info = res_dict.get("traits", {})
            org = traits_info.get("organization", None)
            isp = traits_info.get("isp", None)
            # autonomous_system_number
            asn = traits_info.get("autonomous_system_number", None)
            # autonomous_system_organization
            aso = traits_info.get("autonomous_system_organization", None)
            # connection_type
            ct = traits_info.get("connection_type", None)
            # cityinfo
            data_city_info = res_dict.get("city", {})
            data_city_geoid = data_city_info.get("geoname_id", None)
            data_city_en = data_city_info.get("names", {}).get("en", None)
            data_city_cn = data_city_info.get("names", {}).get("zh-CN", None)
            # provinceinfo
            data_state_en = None
            data_state_cn = None
            data_state_geoid = None
            data_state_isocode = None
            subdivisions = res_dict.get("subdivisions", [])
            if len(subdivisions) > 0:
                data_state_info = subdivisions[0]
                data_state_geoid = data_state_info.get("geoname_id", None)
                data_state_isocode = data_state_info.get("iso_code", None)
                data_state_en = data_state_info.get("names", {}).get("en", None)
                data_state_cn = self.format_string(
                    data_state_info.get("names", {}).get("zh-CN", None)
                )
            # countryinfo
            data_country_info = res_dict.get("country", {})
            data_country_geoid = data_country_info.get("geoname_id", None)
            data_country_code = data_country_info.get("iso_code", None)
            data_country_is_in_eu_union = data_country_info.get(
                "is_in_european_union", False
            )
            data_country_en = data_country_info.get("names", {}).get("en")
            data_country_cn = self.format_string(
                data_country_info.get("names", {}).get("zh-CN")
            )
            # location info
            location = res_dict.get("location", {})
            lng = location.get("longitude", None)
            lat = location.get("latitude", None)
            # time_zone = location.get('time_zone', None)
            weather_code = location.get("weather_code", None)
            # 这里表示国家不属于主权国家，所以直接返回None，写注释是一个非常好的习惯
            if (
                not ALL_COUNTRIES.__contains__(data_country_code)
                or lat is None
                or lng is None
            ):
                return res, org, isp
            geoinfo: GeoInfo = GeoInfo(level, lng, lat)
            geoinfo.set_country(
                data_country_code,
                data_country_geoid,
                data_country_is_in_eu_union,
                {"zh-CN": data_country_cn, "en": data_country_en},
            )
            geoinfo.set_province(
                {"zh-CN": data_state_cn, "en": data_state_en},
                code=data_state_isocode,
                geoid=data_state_geoid,
            )
            geoinfo.set_city(
                {"zh-CN": data_city_cn, "en": data_city_en}, geoid=data_city_geoid
            )
            # 大洲
            ctinfo = res_dict.get("continent", {})
            ctcode = ctinfo.get("code")
            ctgeoid = ctinfo.get("geoname_id")
            ctname_en = ctinfo.get("names", {}).get("en")
            ctname_cn = ctinfo.get("names", {}).get("zh-CN")
            geoinfo.set_continent(
                ctcode, ctgeoid, {"zh-CN": ctname_cn, "en": ctname_en}
            )
            exrtinfo: CountryCode = ALL_COUNTRIES.get(data_country_code)
            geoinfo.globaltelcode = exrtinfo.countrycode
            geoinfo.timezone = exrtinfo.timediffer
            # 新增字段
            geoinfo.weather_code = weather_code
            geoinfo.asn = asn
            geoinfo.aso = aso
            geoinfo.ct = ct
            res = geoinfo
        except Exception:
            if res_dict is not None:
                self._logger.error(
                    "Get geoinfo error: ip={}, error: {}".format(
                        ip, traceback.format_exc()
                    )
                )
        return res, org, isp


#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : main.py
# Create date : 2019-08-09 13:14
# Modified date : 2019-08-10 21:58
# Author : DARREN
# Describe : not set
# Email : lzygzh@126.com
#####################################
from __future__ import division
from __future__ import print_function

from kb import SemanticKB
from hyper_extract import HyponomyExtraction

from baike import SemanticBaike

def run_one_kb(word):
    handler = SemanticKB()
    handler.walk_concept_chain(word)

def kb_run():
    word = '孔子'
    run_one_kb(word)
    word = '苹果'
    run_one_kb(word)
    word = '中国'
    run_one_kb(word)
    word = '小麦'
    run_one_kb(word)
    word = '哆啦A梦'
    run_one_kb(word)
    word = '平壤'
    run_one_kb(word)

def run_hyper():
    content = '''油加醋，或者去涂脂抹粉“打造”它。历史是不需要加工的。无形的音乐是一种灵魂，而肺癌是癌症的一种'''
    handler = HyponomyExtraction()
    handler.process_candis2()

def baike_run():
    handler = SemanticBaike()
    word = '孔子'
    handler.extract_main(word)
    word = '苹果'
    handler.extract_main(word)
    word = '中国'
    handler.extract_main(word)
    word = '小麦'
    handler.extract_main(word)
    word = '哆啦A梦'
    handler.extract_main(word)
    word = '平壤'
    handler.extract_main(word)

def run():
    kb_run()
    #run_hyper()
    baike_run()

run()


def main():
    from sys import stdin, stdout
    result = 0
    column = int(stdin.readline())
    operation = stdin.readline().strip()
    for i in range(12):
        for j in range(12):
            tmp = float(stdin.readline())
            if j == column:
                result += tmp
    stdout.write('{:.1f}\n'.format(result if operation == 'S' else result / 12.0))
main()


class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0:
            return ""
        result = ""
        minlen = len(strs[0])
        flag = False
        for i in range(len(strs)):
            if minlen > len(strs[i]):
                minlen = len(strs[i])
            if len(strs[i]) > 0:
                flag = True
        if flag and minlen == 0:
            return ""

        for i in range(minlen):
            s = strs[0][i]
            for j in range(1,len(strs)):
                if strs[j][i] != s:
                    return result
            result += s
        return result


import unittest
import warnings
import json
import node
import sess
from eosf import *

CONTRACT_NAME = "@CONTRACT_NAME@/build"
set_verbose(False)

class Test1(unittest.TestCase):

    def run(self, result=None):
        """ Stop after first error """
        if not result.failures:
            super().run(result)

    @classmethod
    def setUpClass(cls):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            node.reset()
        sess.init()
        contract = Contract(CONTRACT_NAME)
        contract.deploy()

    def setUp(self):
        self.assertTrue(node.is_running(), "testnet failure")
        self.contract = Contract(CONTRACT_NAME)
        self.assertTrue(self.contract.is_created(), "contract failure")
        self.assertTrue(self.contract.is_deployed(), "deployment failure")

    def test_01(self):
        self.assertTrue(
            self.contract.push_action(
            "create",
            '{"issuer":"eosio", "maximum_supply":"1000000000.0000 EOS", \
                "can_freeze":0, "can_recall":0, "can_whitelist":0}'),
            "push_action create")

        self.assertTrue(
            self.contract.push_action(
            "issue",
            '{"to":"alice", "quantity":"100.0000 EOS", \
                "memo":"issue 100.0000 EOS"}',
            sess.eosio),
            "push_action issue")

    def test_02(self):
        self.assertTrue(
            self.contract.push_action(
            "transfer",
            '{"from":"alice", "to":"carol", "quantity":"25.0000 EOS", \
                "memo":"transfer 25.0000 EOS"}',
            sess.alice),
            "push_action transfer")

        self.assertTrue(
            self.contract.push_action(
            "transfer",
            '{"from":"carol", "to":"bob", "quantity":"13.0000 EOS", \
                "memo":"transfer 13.0000 EOS"}',
            sess.carol),
            "push_action transfer")

        self.assertTrue(
            self.contract.push_action(
            "transfer",
            '{"from":"bob", "to":"alice", "quantity":"2.0000 EOS", \
                "memo":"transfer 2.0000 EOS"}',
            sess.bob),
            "push_action transfer")

    def test_03(self):
        t1 = self.contract.get_table("accounts", sess.alice)
        self.assertFalse(t1.error, "get_table alice")

        t2 = self.contract.get_table("accounts", sess.bob)
        self.assertFalse(t2.error, "get_table bob")

        t3 = self.contract.get_table("accounts", sess.carol)
        self.assertFalse(t2.error, "get_table carol")

        self.assertEqual(
            t1.json["rows"][0]["balance"], "77.0000 EOS")

        self.assertEqual(
            t2.json["rows"][0]["balance"], "11.0000 EOS")

        self.assertEqual(
            t3.json["rows"][0]["balance"], "12.0000 EOS")

    def tearDown(self):
        pass

    @classmethod
    def tearDownClass(cls):
        node.stop()

if __name__ == "__main__":
    unittest.main()


'''
파티 출처다국어분류
시간 제한	메모리 제한	제출	정답	맞은 사람	정답 비율
1 초	128 MB	16739	7798	5123	44.939%
문제
N개의 숫자로 구분된 각각의 마을에 한 명의 학생이 살고 있다.

어느 날 이 N명의 학생이 X (1 ≤ X ≤ N)번 마을에 모여서 파티를 벌이기로 했다. 이 마을 사이에는 총 M개의 단방향 도로들이 있고 i번째 길을 지나는데 Ti(1 ≤ Ti ≤ 100)의 시간을 소비한다.

각각의 학생들은 파티에 참석하기 위해 걸어가서 다시 그들의 마을로 돌아와야 한다. 하지만 이 학생들은 워낙 게을러서 최단 시간에 오고 가기를 원한다.

이 도로들은 단방향이기 때문에 아마 그들이 오고 가는 길이 다를지도 모른다. N명의 학생들 중 오고 가는데 가장 많은 시간을 소비하는 학생은 누구일지 구하여라.

입력
첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어온다. 시작점과 끝점이 같은 도로는 없으며, 시작점과 한 도시 A에서 다른 도시 B로 가는 도로의 개수는 최대 1개이다.

모든 학생들은 집에서 X에 갈수 있고, X에서 집으로 돌아올 수 있는 데이터만 입력으로 주어진다.

출력
첫 번째 줄에 N명의 학생들 중 오고 가는데 가장 오래 걸리는 학생의 소요시간을 출력한다.

예제 입력 1
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
예제 출력 1
10
출처
Olympiad > USA Computing Olympiad > 2006-2007 Season > USACO February 2007 Contest > Silver 3번

문제를 번역한 사람: author6
빠진 조건을 찾은 사람: his130
링크
PKU Judge Online
TJU Online Judge

'''
from sys import stdin
from heapq import heappop, heappush

def readline(): return map(int, stdin.readline().split())

def search(graph, x):
    D = [float('inf') for _ in range(len(graph))]
    hq = [(0, x)]

    D[x] = 0

    while hq:
        cost, cur = heappop(hq)

        for nxt, nxt_cost in graph[cur]:
            total_cost = cost + nxt_cost

            if D[nxt] > total_cost:
                heappush(hq, (total_cost, nxt))
                D[nxt] = total_cost

    return D

if __name__ == '__main__':
    N, M, X = readline()

    forward_graph = [[] for _ in range(N+1)]
    backward_graph = [[] for _ in range(N+1)]

    for _ in range(M):
        s, e, t = readline()
        forward_graph[s].append((e, t))
        backward_graph[e].append((s, t))

    foward = search(forward_graph, X)
    backward = search(backward_graph, X)

    print(max(map(sum, zip(foward[1:], backward[1:]))))


"""
Databases models of the LeanCrawler. All paths in database are relative.
"""

import os
import shutil
from datetime import datetime
from pathlib import Path
from peewee import (SqliteDatabase, Model,
                    CharField, IntegerField, ForeignKeyField, DateTimeField)

from leancrawler.python_storage import LeanLib, LeanFile, LeanItem, logger

db = SqliteDatabase(None)

class BaseModel(Model):
    class Meta:
        database = db

class LeanLibModel(BaseModel):
    name = CharField(unique=True)

    @classmethod
    def from_path(cls, name: str, path: Path):
        """Get a LeanLibModel object from its path, fetching files either from
           database or source."""
        try:
            lean_lib_m = cls.get(name=name)
        except cls.DoesNotExist:
            lean_lib_m = cls.create(name=name)

        # Delete non-existing files from db
        for lean_file_m in lean_lib_m.files:
            if not (path/lean_file_m.path).exists():
                lean_file_m.delete().execute()
        for dep in Path(__file__).parent.glob('deps.*'):
            logger.debug(f"Will copy {dep} to {path}")
            shutil.copy(dep, path)

        for file_path in path.glob('**/*.lean'):
            if file_path.name == 'deps.lean':
                continue
            mtime = os.path.getmtime(file_path)
            rel_path = str(file_path.relative_to(path))

            # A file may need update either because it's not in the database or
            # because it has been modified since last visit.
            needs_update = False
            try:
                lean_file_m = LeanFileModel.get(path=rel_path)
                if lean_file_m.visited.timestamp() < mtime:
                    needs_update = True
            except LeanFileModel.DoesNotExist:
                lean_file_m = LeanFileModel.create(lib=lean_lib_m,
                                                   path=rel_path)
                needs_update = True

            if needs_update:
                lean_file_m.read(path)
            else:
                logger.debug(f"No need to update {rel_path}")
        for dep in Path(__file__).parent.glob('deps.*'):
            (path/dep.name).unlink()
        return lean_lib_m

    def to_py(self, path: Path) -> LeanLib:
        lib = LeanLib(name=self.name, path=path)
        for lf_m in self.files:
            logger.debug(f"Importing {lf_m.path}")
            lib.files[Path(lf_m.path)] = lf_m.to_py()
        return lib

class LeanFileModel(BaseModel):
    lib = ForeignKeyField(LeanLibModel, backref='files', null=True)
    path = CharField(unique=True)
    visited = DateTimeField(default=datetime.now)
    nb_lines = IntegerField(default=0)

    def read(self, path: Path) -> None:
        logger.debug(f"LeanFileModel reading {path}")
        LeanItemModel.delete().where(LeanItemModel.leanfile == self)
        ImportModel.delete().where(ImportModel.importer == self)
        lf = LeanFile.from_path(path/self.path, root=path)
        for imported in lf.imports:
            imp_path = imported.replace('.', '/') + '.lean'
            other_file_m = LeanFileModel.get_or_create(path=imp_path)[0]
            ImportModel.create(importer=self, imported=other_file_m)
        for key, val in lf.items.items():
            LeanItemModel.from_py(self, val)

        self.nb_lines = lf.nb_lines
        self.save()

    def to_py(self) -> LeanFile:
        return LeanFile(Path(path=self.path),
                        visited=self.visited,
                        nb_lines=self.nb_lines,
                        imports=[f.imported.path for f in self.imports],
                        items={i.name: i.to_py() for i in self.items})

class ImportModel(BaseModel):
    importer = ForeignKeyField(LeanFileModel, backref='imports')
    imported = ForeignKeyField(LeanFileModel, backref='imported_by')

class NameSpaceModel(BaseModel):
    fullname = CharField(unique=True)

class LeanItemModel(BaseModel):
    kind = CharField(max_length=10, default='unknown')
    name = CharField(unique=True)
    #  null ⇔ root namespace
    namespace = ForeignKeyField(NameSpaceModel, null=True)
    leanfile = ForeignKeyField(LeanFileModel, backref='items', null=True)
    line_nb = IntegerField(default=0)
    size = IntegerField(default=0)
    proof_size = IntegerField(default=0)

    def to_py(self) -> LeanItem:
        def_deps = []
        pf_deps = []
        for item in self.deps:
            if item.kind == 'def':
                def_deps.append(item.used.name)
            else:
                pf_deps.append(item.used.name)
        return LeanItem(self.kind,
                        name=self.name,
                        size=self.size,
                        line_nb=self.line_nb,
                        def_depends=def_deps,
                        proof_size=self.proof_size,
                        proof_depends=pf_deps)

    @staticmethod
    def from_py(lf_m: LeanFileModel, item: LeanItem) -> 'LeanItemModel':
        li_m, _ = LeanItemModel.get_or_create(name=item.name)

        li_m.kind = item.kind
        li_m.name = item.name
        li_m.namespace = NameSpaceModel.get_or_create(
            fullname='.'.join(item.namespace))[0]
        li_m.leanfile = lf_m
        li_m.line_nb = item.line_nb or 0
        li_m.size = item.size
        li_m.proof_size = item.proof_size
        li_m.save()

        for dep in item.def_depends:
            try:
                used_m = LeanItemModel.get(name=dep)
            except LeanItemModel.DoesNotExist:
                used_m = LeanItemModel.create(name=dep)
            DependanceModel.create(kind='def',
                                   user=li_m,
                                   used=used_m)
        for dep in item.proof_depends:
            try:
                used_m = LeanItemModel.get(name=dep)
            except LeanItemModel.DoesNotExist:
                used_m = LeanItemModel.create(kind='unknown', name=dep)
            DependanceModel.create(kind='proof',
                                   user=li_m,
                                   used=used_m)
        for field in item.fields:
            StructureFieldModel.create(name=field, parent=li_m)
        if item.kind == 'instance':
            try:
                class_m = LeanItemModel.get(name=item.instance_target)
            except LeanItemModel.DoesNotExist:
                class_m = LeanItemModel.create(name=item.instance_target)
            InstanceModel.create(instance=li_m, target=class_m)
        return li_m

class StructureFieldModel(BaseModel):
    name = CharField()
    parent = ForeignKeyField(LeanItemModel, backref='fields')

class DependanceModel(BaseModel):
    kind = CharField(max_length=5)  # 'def' or 'proof'
    user = ForeignKeyField(LeanItemModel, backref='deps')
    used = ForeignKeyField(LeanItemModel, backref='used_by')

class InstanceModel(BaseModel):
    instance = ForeignKeyField(LeanItemModel, backref='classes')
    target = ForeignKeyField(LeanItemModel, backref='instances')

def create_db(name: str):
    db.init(name)
    db.create_tables([LeanLibModel, LeanFileModel, LeanItemModel, ImportModel,
                      NameSpaceModel, StructureFieldModel, DependanceModel,
                      InstanceModel])

def use_db(name: str):
    db.init(name)


total_eth_qty: public(wei_value)
total_token_qty: public(currency_value)
# Constant set in `initiate` that's used to calculate
# the amount of ether/tokens that are exchanged
invariant: public(num(wei * currency))
token_address: address(ERC20)
owner: public(address)

# Sets the on chain market maker with its owner, intial token quantity,
# and initial ether quantity
@public
@payable
def initiate(token_addr: address, token_quantity: currency_value):
    assert self.invariant == 0
    self.token_address = token_addr
    self.token_address.transferFrom(msg.sender, self, as_num256(token_quantity))
    self.owner = msg.sender
    self.total_eth_qty = msg.value
    self.total_token_qty = token_quantity
    self.invariant = msg.value * token_quantity
    assert self.invariant > 0

# Sells ether to the contract in exchange for tokens (minus a fee)
@public
@payable
def eth_to_tokens():
    fee = msg.value / 500
    eth_in_purchase = msg.value - fee
    new_total_eth = self.total_eth_qty + eth_in_purchase
    new_total_tokens = self.invariant / new_total_eth
    self.token_address.transfer(msg.sender,
                                as_num256(self.total_token_qty - new_total_tokens))
    self.total_eth_qty = new_total_eth
    self.total_token_qty = new_total_tokens

# Sells tokens to the contract in exchange for ether
@public
def tokens_to_eth(sell_quantity: currency_value):
    self.token_address.transferFrom(msg.sender, self, as_num256(sell_quantity))
    new_total_tokens = self.total_token_qty + sell_quantity
    new_total_eth = self.invariant / new_total_tokens
    eth_to_send = self.total_eth_qty - new_total_eth
    send(msg.sender, eth_to_send)
    self.total_eth_qty = new_total_eth
    self.total_token_qty = new_total_tokens

# Owner can withdraw their funds and destroy the market maker
@public
def owner_withdraw():
    assert self.owner == msg.sender
    self.token_address.transfer(self.owner, as_num256(self.total_token_qty))
    selfdestruct(self.owner)


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import sklearn.metrics as skl
from tensorflow.examples.tutorials.mnist import input_data

def plot_confusion_matrix(cm, classes,normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    #print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.

    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            plt.text(j, i, format(cm[i, j], fmt),horizontalalignment="center",color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

if __name__ == '__main__':
    mnist = input_data.read_data_sets("./MNIST_data", one_hot=True)
    x_data = mnist.test.images
    y_data = mnist.test.labels
    x_data = x_data.reshape((-1, 28, 28))
    x_data = np.expand_dims(x_data, axis=-1)

    (n_data, h, w, n_channels) = x_data.shape
    (_, n_classes) = y_data.shape

    # *************************************************************
    #               Model building
    # *************************************************************

    # Layer 1 (Input): L1 (28x28x1)
    X = tf.placeholder(tf.float32, shape=(None, w, h, 1), name="input")   # (None means 'any number of rows')
    Y = tf.placeholder(tf.float32, shape=(None, n_classes), name="output")

    # Layer 2 (Convolution), W1 (5x5x1x3):  L1 (28x28x1) --> L2 (24x24x3)
    W1 = tf.Variable(tf.random_uniform([5, 5, 1, 3], -0.01, 0.01), name="W1")
    v1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='VALID')
    y1 = tf.nn.relu(v1)

    # Layer 3 (Convolution), W2 (5x5x3x20): L2 (24x24x3) --> L3 (20x20x20)
    W2 = tf.Variable(tf.random_uniform([5, 5, 3, 20], -0.01, 0.01), name="W2")
    v2 = tf.nn.conv2d(y1, W2, strides=[1, 1, 1, 1], padding='VALID')
    y2 = tf.nn.relu(v2)

    # Layer 4 (Pooling): L3 (20x20x20) --> L4 (10x10x20)
    y3 = tf.nn.avg_pool(y2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

    # Layer 5 (Flatten): L4 (10x10x20) --> L5 (2000)
    y4 = tf.reshape(y3, [-1, 10*10*20])

    # Layer 6 (Fully-connected), W3 (2000x300): L5 (2000) --> L6 (300)
    W3 = tf.Variable(tf.random_uniform([2000, 300], -0.01, 0.01), name="W3")
    v5 = tf.matmul(y4, W3)
    y5 = tf.nn.relu(v5)

    # Layer 7 (Fully-connected for Output), Wo (300x10): L6 (300) --> L7 (10)
    Wo = tf.Variable(tf.random_uniform([300, n_classes], -0.01, 0.01), name="Wo")
    Yhat = tf.matmul(y5, Wo)

    # Session opening
    # saver = tf.train.Saver()
    saver = tf.train.Saver()
    sess = tf.Session()
    saver.restore(sess, './result/model.ckpt')

    pred, target = sess.run([Yhat, Y], feed_dict={X: x_data, Y: y_data})

    ylable = np.argmax(target,axis=1)
    yhatlable = np.argmax(pred,axis=1)

    # Compute confusion matrix
    cnf_matrix = skl.confusion_matrix(ylable, yhatlable)
    np.set_printoptions(precision=2)

    # Plot non-normalized confusion matrix
    class_names = ['0','1','2','3','4','5','6','7','8','9']
    plt.figure()
    plot_confusion_matrix(cnf_matrix, classes=class_names,
                      title='Confusion matrix, without normalization')

    # Plot normalized confusion matrix
    plt.figure()
    plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')

    plt.show()


### This is created as a separate file from one of the data challenges to the purposes of code review.

# import and load the libraries

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn import preprocessing, metrics

# load the data csv file

data = pd.read_csv('vgsales.csv')
#data.head()     #shows the top 5 lines of the dataframe

print('Number of games in the dataset is {}\nNumber of columns is {}'.format(data.shape[0], data.shape[1]))
if data.isnull().values.any():
    print('There are null values in the data.')

for column in data.columns:
    if data[column].isnull().any():
        print('Column "{}" has null values'.format(column))

# Grab lists of unique values for publishers, genres, and platforms
publishers = data['Publisher'].unique()
genres = data['Genre'].unique()
platforms = data['Platform'].unique()

regions = ['NA_Sales', 'EU_Sales', 'JP_Sales']

max_sales_in_market = []
for region in regions:
    max_sales_in_market.append(data[region].max())

max_sales = max(max_sales_in_market)
print('The max sales for a single video game in a single region is {}'.format(max(max_sales_in_market)))

sales_data = {}
for region in regions:
    sales_list = []
    for sales_bin in range(1, int(max_sales)):
        sales_list.append(data[data[region] > sales_bin].shape[0])
    sales_data[region] = sales_list

## train and test the data for linear regression

#drop the null values in the 4 columns - year, publisher, platform, genres
vg_data = data.dropna(subset=['Year', 'Publisher', 'Platform', 'Genre'], axis=0)

# convert categorical variable into dummy/indicator variables

platforms_encoded = pd.get_dummies(vg_data['Platform'].values)
platforms_encoded = platforms_encoded.reset_index().drop(labels='index', axis=1)

genres_encoded = pd.get_dummies(vg_data['Genre'].values)
genres_encoded = genres_encoded.reset_index().drop(labels='index', axis=1)

publishers_encoded = pd.get_dummies(vg_data['Publisher'].values)
publishers_encoded = publishers_encoded.reset_index().drop(labels='index', axis=1)

year = vg_data['Year']/2020
year = year.reset_index().drop(labels='index', axis=1)

model_data = pd.concat([platforms_encoded, genres_encoded, publishers_encoded, year], axis=1)
target = vg_data['Global_Sales']

X_train, X_test, y_train, y_test = train_test_split(model_data, target, test_size=0.30, random_state=0)
#X_train.shape

# train test a linear regression model

clf = LinearRegression()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
#y_pred

print(clf.score(X_test, y_test))   # prints the accuracy score for the linear regression model

# train a random forest regressor next

clf = RandomForestRegressor()
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))

n_estimators = [10, 25, 50, 100] # number of trees
min_samples_split = [2, 10, 25, 50] # minimum number of samples needed to split a node

hp_list = []
for estimator in n_estimators:
    for sample in min_samples_split:
        clf = RandomForestRegressor(n_estimators=estimator, min_samples_split=sample)
        clf.fit(X_train, y_train)
        hp_list.append([estimator, sample, clf.score(X_test, y_test)])
        print('done')

print(sorted(hp_list, key=lambda x: x[2], reverse=True))   # largest accuracies shown first

### The best R^2 score is achieved for n_estimators = 10, min_samples_split = 25, with R^2=0.17.
### This is much better than linear regression, suggesting that the underlying function may not be linear.
### Refit the best model and see what the top predictors are

clf = RandomForestRegressor(n_estimators=10, min_samples_split=25)
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))

## get the top predictors
top_predictors = (-clf.feature_importances_).argsort()[:20]
X_train.iloc[:, top_predictors].columns

## prints the top predictors

for name in X_train.iloc[:, top_predictors].columns:
    if name in publishers:
        print('publisher')
    elif name in genres:
        print('genres')
    elif name in platforms:
        print('platform')
    else:
        print('year')

## plot the important features

plt.figure(figsize=(8,8))
plt.bar(x=range(20), height=clf.feature_importances_[top_predictors],\
        tick_label=X_train.iloc[:, top_predictors].columns)
plt.xticks(rotation=90)
plt.title('Feature Importance (Random Forest)');
plt.savefig('rf_featureimportance.png')


# /* backend leaker */
# /* made by nano */ - cybersecurity at it's finest

from time import sleep
from time import process_time
from modules.banner import *
from modules.dns import *
from modules.get import *
import shodan
import ctypes

def handleurl():
    ctypes.windll.kernel32.SetConsoleTitleW("[ rx leaker ] - Waiting for target")

    url = input(f"      {white}Url {gray}►{white} ")

    # url handling
    if "https" in url or "http" in url:
        pass
    else:
        url = "https://" + url

    # testing url
    ctypes.windll.kernel32.SetConsoleTitleW(f"[ rx leaker ] - testing {url} connection")
    # connection speed
    t1_start = process_time()
    testconnect(url)
    t1_stop = process_time()
    sleep(3)
    print(f"      {white}Url {gray}► {green}ONLINE{white}")
    sleep(2)
    print(f"      Status {gray}► {green}200")
    print(f"      Speed {gray}► {yellow}{t1_stop - t1_start}")
    sleep(4)
    system('cls;clear')
    banner()
    ctypes.windll.kernel32.SetConsoleTitleW(f"[ rx leaker ] - Scanning {url}")

    print(f"    {gray}► {white}Starting backend enum...\n")
    rawip = GetDomainIP(url)

    # check ip ranges if cloudflare
    if "172." in rawip['ip'] or "103." in rawip['ip'] or "108." in rawip['ip'] or "104." in rawip['ip']:
        print(f"    {gray}► {white}RAW IP {yellow}{rawip['ip']} {rawip['cf']}")
    else:
        print(f"    {gray}► {white}RAW IP {yellow}{rawip['ip']}")
    # headers
    head = redd(url)
    if not head['red']:
        print(f"    {gray}► {white}redirects = {yellow}False")
    else:
        print(f"    {gray}► {white}redirect {green}{head['red']}")
    # backend / leakage
    mail = mailman(url)
    if not mail['backend']:
        print(f"    {gray}► {white}SMP backend {yellow}not found")
    else:
        print(f"    {gray}► {white}FOUND SMP backend {green}{mail['backend']}")
        io = GetDomainIP(mail['backend'])
        print(f"    {gray}► {white}FOUND SMP IP {green}{io['ip']}")
    # uses the cpanel module
    cpan = cpanel(url)
    if not cpan['cpanel']:
        print(f"    {gray}► {white}cpanel {yellow}not found")
    else:
        print(f"    {gray}► {white}FOUND cpanel {green}{cpan['cpanel']}")
    # uses the DnsDumpster module
    print(f"\n    {gray}► {white}Scanning DnsRecords...\n")
    dump = urlparse(url).netloc
    dnsdumpster(dump)
    # uses the viurstotal module
    try:
        print(f"\n    {gray}► {white}Scanning Subdomains...\n")
        total(url)
    except Exception as e:
        print(f"    {gray}► {white}Could not find any subdomains")

        pass
    print(f"\n    {gray}► {white}Scanning Shodan...\n")
    # shodan module
    shodan = sho(url)
    try:
        if not shodan['shodan']:
            print(f"    {gray}► {white}No results on shodan")
            pass
        else:
            print(f"    {gray}► {white}Found {shodan['res']} {shodan['shodan']}")

    except TypeError:
        print(f"    {gray}► {white}No results on shodan")
        pass

    # did not include hackertarget / formatting bad does not match rx theme
    # hackertarget(url)
    # choices
    print(f"\n    {gray}► {white}would u like to scan another target? (y/n) \n")
    option = input(f"    {gray}► {white}")
    if option == "y":
        banner()
        handleurl()
    elif option == "n":
        exit(0)
    else:
        banner()
        handleurl()

if __name__ == '__main__':
    banner()
    handleurl()


# -*- coding: utf-8 -*-
# @Time    : 2018-01-20 16:35
# @Author  : Storm
# @File    : my_xgb01_localtest.py

'''
xgboost:
export PYTHONPATH=/stone/pythonPackage/xgboost/python-package
'''

import pandas as pd
import numpy as np
import time
import xgboost as xgb
from xgboost.sklearn import XGBRegressor
from scipy.stats import pearsonr
import matplotlib.pyplot as plt

# 读取数据
from sklearn.model_selection import train_test_split

print('1.读取数据中...')
data_path = 'resultAend/'
train = pd.read_csv(data_path + 'dataP_train03_poly.csv')
test = pd.read_csv(data_path + 'dataP_test03_poly.csv')

# 构造训练集、标签、测试集
feature_columns = [x for x in train.columns if x not in ['cnt'] and train[x].dtype != object]
X_train, y = train[feature_columns], train['cnt']
X_test = test[feature_columns]

# 计算特征权重
corr = {}
for f in X_train.columns:
    data = X_train[f]
    corr[f] = pearsonr(data.values, y.values)[0]
feature = []
feature_v = []
for k, v in corr.items():
    if abs(v) >= 0:
        feature.append(k)
        feature_v.append(v)

# 保存特征和权重
feature_all = pd.DataFrame()
feature_all['特征'] = feature
feature_all['权重'] = feature_v
# 按权重排序

feature_all_sorted = feature_all.sort_values(by=['权重'], ascending=False)
# feature_all_sorted.plot(x='特征', y='权重', kind='bar')
# plt.show()
feature_all_sorted.to_csv('feature/feature_xgb_20180225.csv',
                          index=None,
                          encoding='utf-8')
''''''

# 保留权重大于0.1的特征
X_train = train[feature_columns]
X_test = test[feature_columns]

split_num = 1034
trainX_split = X_train.iloc[:split_num, :]
testX_split = X_train.iloc[split_num:, :]
trainY_split = y[:split_num]
testY_split = y[split_num:]

# 构造XGBRegressor模型

'''
params = {'booster': 'gbtree',
          'objective': 'reg:linear',
          'eval_metric': 'rmse',
          'gamma': 0.1,
          'min_child_weight': 2,
          'max_depth': 5,
          'lambda': 10,
          'alpha': 2.5,
          'silent': True,
          'subsample ': 0.8,
          'colsample_bytree': 0.5,
          'colsample_bylevel': 0.5,
          'eta': 0.01,
          # 'tree_method': 'gpu_exact',
          # 'gpu_id': 0,
          'seed': 50,
          # 'scale_pos_weight':10,
          'nthread': -1
          }

xgb1 = XGBRegressor(
    learning_rate=0.01,
    n_estimators=1000,
    max_depth=5,
    min_child_weight=2,
    gamma=0.1,
    reg_alpha=2.5,
    reg_lambda=5,
    subsample=0.8,
    colsample_bytree=0.5,
    objective='reg:linear',
    nthread=-1,
    scale_pos_weight=1,
    silent=True,
    # tree_method='gpu_exact',
    # gpu_id=0,
    seed=0)

result = xgb.cv(params, xgb.DMatrix(X_train, label=y), num_boost_round=1000, nfold=8, stratified=False, folds=None,
                maximize=False, early_stopping_rounds=10, as_pandas=True, verbose_eval=None, show_stdv=True,
                seed=0, callbacks=None, shuffle=True)
xgb1.set_params(n_estimators=result.shape[0])

print('2.开始训练...')
xgb1.fit(trainX_split, trainY_split)

print('3.开始预测...')
y_predict = xgb1.predict(testX_split)

print('4.评测...')
# 对训练集本身评价
compare2 = pd.DataFrame()
compare2['true'] = testY_split
compare2['pred'] = y_predict
f2 = (1 / (len(compare2))) * sum((compare2['pred'] - compare2['true']) * (compare2['pred'] - compare2['true']))
print('评价结果：', f2)

# 预测
xgb1.fit(X_train, y)
y_predict_test = xgb1.predict(X_test)

# 画图
print('==============画图============')
# plt.plot(X_train.date, y, 'b')
plt.plot(testX_split.date, testY_split, 'b')
plt.title('All Count Law')
plt.xlabel('Date')
plt.ylabel('Cnt')
plt.plot(testX_split.date, y_predict, 'r')
plt.plot(X_test.date, y_predict_test, 'g')
plt.show()

'''
# model = xgb.XGBRegressor(n_estimators=10000, learning_rate=0.08, gamma=0, subsample=0.7,
#                          colsample_bytree=0.7, max_depth=8)
model = xgb.XGBRegressor(max_depth=10,
                         learning_rate=0.01,
                         n_estimators=1000,
                         early_stopping_rounds=100,
                         silent=True,
                         objective='reg:linear',
                         nthread=-1,
                         gamma=0,
                         min_child_weight=1,
                         max_delta_step=0,
                         subsample=0.85,
                         colsample_bytree=0.7,
                         colsample_bylevel=1,
                         reg_alpha=0,
                         reg_lambda=1,
                         scale_pos_weight=1,
                         seed=1440,
                         missing=None,
                         )

print('2.开始训练...')
model.fit(trainX_split, trainY_split)

print('3.开始预测...')
y_predict = model.predict(testX_split)

print('4.评测...')
# 对训练集本身评价
compare2 = pd.DataFrame()
compare2['true'] = testY_split
compare2['pred'] = y_predict
f2 = (1 / (len(compare2))) * sum((compare2['pred'] - compare2['true']) * (compare2['pred'] - compare2['true']))
print('评价结果：', f2)

# 预测
model.fit(X_train, y)
y_predict_test = model.predict(X_test)

# 画图
print('==============画图============')
# plt.plot(X_train.date, y, 'b')
plt.plot(testX_split.date, testY_split, 'b')
plt.title('All Count Law')
plt.xlabel('Date')
plt.ylabel('Cnt')
plt.plot(testX_split.date, y_predict, 'r')
plt.plot(X_test.date, y_predict_test, 'g')
plt.show()