from cmd import Cmd
from file_to_data import FileToData
import os.path
from os import path
import webbrowser
# import matplotlib.pyplot as plt
# import matplotlib.image as mpimg
# import numpy as np
from DiagramCreator import MyCreator
from PickleMaker import MyPickle
from SQLDatabase import MyDatabase
import os
import cmd
import subprocess
import shlex
import sys
import os.path
import shelve_create

class MyCli(Cmd):
    """Command line interpreter for generating UML class diagram"""

    # Harry's work
    def __init__(self, my_name=">"):
        Cmd.__init__(self, my_name)
        self.my_name = my_name
        self.prompt = ">>" + my_name + ">> "
        self.file_to_data = FileToData()

    # Matt's work
    def do_exit(self):  # delete
        # exit the application.
        print('Exiting Program...')
        return True

    # Matt's work
    def help_exit(self):
        print('Exit the application.')

    # Matt's work
    def do_diagram(self, inp):
        image_name = input('Image name/type:')
        MyCreator(image_name, inp).create_diagram()

    # Matt's work
    def help_diagram(self):
        print('Create a class diagram. Enter file location of py/dot file, then enter name/type of image.')

    # Matt's work
    def do_deletediagram(self, inp):
        MyCreator(inp, 'pass').delete_diagram()

    # Matt's work
    def help_deletediagram(self):
        print('Deletes a diagram')

    # Matt's work
    def do_pickle(self, inp):
        MyPickle(inp, input('name of pickle file: ')).make_pickle()

    # Matt's work
    def help_pickle(self):
        print('pickle [filename], enter file to pickle then the name of the pickle file')

    # Matt's work
    def do_unpickle(self, inp):
        MyPickle('a', inp).make_pickle()

    # Matt's work
    def help_unpickle(self):
        print('unpickle [picklefilename], enter the name of a text file that has been pickled')

    # Matt's work
    def do_deletepickle(self, inp):
        MyPickle('pass', inp).delete_pickle()

    # Matt's work
    def help_deletepickle(self):
        print('Deletes a pickle file')

    # Matt's work
    def do_createtable(self, inp):
        MyDatabase().create_table(inp)

    # Matt's work
    def help_createtable(self):
        print('createtable [TABLE_NAME], creates a table with: file_number INTEGER PRIMARY KEY, file_name VARCHAR(30),'
              'file_content VARCHAR(999) ')

    # Matt's work
    def do_addtotable(self, inp):
        f_number = input('File number: ')
        try:
            val = int(f_number)
            MyDatabase().add_data(inp, val, input('File name: '), input('File content: '))
        except ValueError:
            print('Please input a integer!')

    # Matt's work
    def help_addtotable(self):
        print('addtotable [TABLE_NAME], adds data to specified table')

    # Matt's work
    def do_showtable(self, inp):
        MyDatabase().show_data(inp)

    # Matt's work
    def help_showtable(self):
        print('showtable [TABLE_NAME], shows data held within specified table')

    # Matt's work
    def do_deletetable(self, inp):
        MyDatabase().delete_table(inp)

    # Matt's work
    def help_deletetable(self):
        print('deletetable [TABLE_NAME], deletes specified table')

    # Matt's work
    def do_closedb(self):
        MyDatabase().close_database()

    # Matt's work
    def help_closedb(self):
        print('Closes current open database')

    # Matt's work
    def do_deletedb(self):
        MyDatabase().close_database()

    # Matt's work
    def help_deletedb(self):
        print('Deletes current database')

    # Matt's work
    def default(self, inp):
        print(inp + ' is an incorrect command. Type ? to list commands.')

	    #John's work
    def help_help(self):
        message = 'Commands help.'
        print(message)

    #John's work
    def do_help(self, args):
        """Get help on commands"""
        cmd.Cmd.do_help(self, args)

    #John's work
    def do_create(self, source_file):
        """Generate dot file."""
        try:
            if os.path.exists(source_file):
                args = shlex.split('-o dot -p result source_file.py')
                subprocess.call(['pyreverse'] + args)
                print('Dot File is ready please check folder.')
            else:
                message = "Process has failed."
                raise NameError(message)

        except NameError as e:
            print(e)

    def help_create(self):
        message = 'Make source py file into dot file. Example: create source_file.py'
        print(message)

    def do_check_dot(self, classes_result):
        """Checks if dot file exists"""
        try:
            if os.path.exists(classes_result):
                print(classes_result + ' is ready to be converted')
            else:
                message = "the dot file does not exist"
                raise NameError(message)

        except NameError as e:
            print(e)

    def help_check_dot(self):
        message = 'Checks if dot file exists. Example: check_dot classes_result.dot'
        print(message)

    def do_dot(self, classes_result):
        """Turn dot file into png file."""
        try:
            if os.path.exists(classes_result):
                args = shlex.split(' -Tpng -O classes_result.dot')
                subprocess.call(['dot'] + args)
                print('the conversion is done')

            else:
                message = "the dot file failed to be converted to png.."
                print(message)

        except Exception as err:
            print("Failed, the error is: ", err)

    def help_dot(self):
        message = 'Turn dot file into png file. Example: dot classes_result.dot'
        print(message)

    def do_make_shelve(self, inp):
        """Create shelf!"""
        shelve_create.Shelf(inp, input('name of file to shelf: ')).make_shelf()

    def help_make_shelve(self):
        message = 'make_shelve then press enter, then enter the name of the file shelved [example: cli.py]'
        print(message)

    def do_existing_shelve(self, inp):
        """Access data!"""
        shelve_create.Shelf(inp, input('name of shelf file to access: ')).existing_shelf()

    def help_existing_shelve(self):
        message = 'existing_shelve then press enter, then enter the name of the shelved file to access'
        print(message)

    def run_pyreverse(self):
        """run pyreverse"""
        from pylint.pyreverse.main import Run
        Run(sys.argv[1:])

    # Harry's work
    def do_pyr_class_diagram(self, file_names):
        """Generate and display a class diagram in png format from given [png_file_name_suffix py_file_name.py]"""
        self.file_names = file_names
        python_file_name = file_names[(file_names.find(" ") + 1):]
        png_file_name = 'classes_' + file_names[0:(file_names.find(" "))] + '.png'
        try:
            if path.exists(python_file_name):
                pyreverse_command = 'pyreverse -ASmn -o png -p ' + file_names
                subprocess.call(pyreverse_command)
                print(file_names + ' are done')

                if path.exists(png_file_name):
                    # show png image
                    img = mpimg.imread(png_file_name)
                    fig = plt.imshow(img)
                    fig.axes.get_xaxis().set_visible(False)
                    fig.axes.get_yaxis().set_visible(False)
                    plt.show()
                else:
                    print("The image of class diagram cannot be generate.")
                    print("Please check with your system administrators.")
            else:
                print("Your given python file does not exist in the current directory "
                      "or your input arguments were wrong. The input arguments "
                      "should be [png_file_name_suffix py_file_name.py]. "
                      "Please try again!")
        except Exception as err:
            print("Please try again! The exception is: ", err)

    # Harry's work
    def help_pyr_class_diagram(self):
        print("\n".join(['Generate and display a class diagram in png format from a given python file',
                         'Syntax: pyr_class_diagram [output png file name suffix] '
                         '[input source code file name.py])']))

    # Harry's work
    def do_read_source_file(self, file_name):
        """This function extract data from the given python file to be an ast node.
        The file name should be [py_file_name.py]. The node will display as an indication of extraction"""
        try:
            if path.exists(file_name):
                self.file_to_data.read_file(file_name)
                print("The ast nodes below has been read from the given python file, " + file_name + ":")
                self.file_to_data.show_ast_nodes()
            else:
                print("Your given python file does not exist in the current directory ")
                print("or your input arguments were wrong. The input arguments ")
                print("should be [py_file_name.py]. ")
                print("Please try again!")
        except Exception as err:
            print("Please try again! The exception is: ", err)

    # Harry's work
    def help_read_source_file(self):
        print("\n".join(['Extract data from the given python file to be an ast node',
                         'Syntax: read_source_file [input source code file name.py]']))

    # Harry's work
    def do_validate_class_contents(self, file_name):
        """Validate, list and display class names, function names and the total numbers of them
        in the given python file. Class and function names are displayed in command line.
        Total numbers of classes and functions are displayed in a bar graph.
        Syntax: validate_class_contents [input source code file name.py]"""

        # sample: validate_class_contents test.py
        num_of_classes = 0
        num_of_functions = 0
        try:
            if path.exists(file_name):
                self.file_to_data.read_file(file_name)
                num_of_classes = len(self.file_to_data.tree.body)
                print("---There are " + str(num_of_classes) + " classes.-------------------")
                print("-----The classes are: -------------------")
                for my_class in self.file_to_data.tree.body:
                    print("-------" + my_class.name + " class")
                for my_class in self.file_to_data.tree.body:
                    print("---------The " + my_class.name + " class has " + str(len(my_class.body)) + " functions")
                    num_of_functions += len(my_class.body)
                    print("-----------The functions in " + my_class.name + " class are ")
                    for my_function in my_class.body:
                        print("---------------" + my_function.name + " function")
                print("total number of classes is " + str(num_of_classes))
                print("total number of functions is " + str(num_of_functions))
                # for my_function in my_class.body:
                #     if my_function.name == "__init__":
                #         print("---------The " + my_class.name + " class has " + str(
                #             len(my_function.body)) + " attributes")
                #         print("-----------The attributes in " + my_class.name + " class are ")
                #         for my_attribute in my_function.body:
                #             print("---------------" + my_attribute.targets[0].attr + " attribute")

                # types_x = ["class", "function"]
                # num_y = [num_of_classes, num_of_functions]
                # plt.plot(types_x, num_y, '-b', label="A simple line")
                # plt.legend(loc='upper left')
                # plt.title("Total Numbers of classes and functions")
                # plt.xlabel('Types')
                # plt.ylabel('Total Numbers')
                # plt.show()
                types_x = ["class", "function"]
                x_pos = np.arange(len(types_x))
                num_y = [num_of_classes, num_of_functions]
                plt.bar(x_pos, num_y, align='center', alpha=0.5)
                plt.xticks(x_pos, types_x)
                plt.ylabel('Total Numbers')
                plt.title('Total Numbers of classes and functions')
                plt.show()
            else:
                print("Your given python file does not exist in the current directory ")
                print("or your input arguments were wrong. The input arguments ")
                print("should be [py_file_name.py]. ")
                print("Please try again!")
        except Exception as err:
            print("Please try again! The exception is: ", err)

    # Harry's work
    def help_validate_class_contents(self):
        print("\n".join(['Validate, list and display class names, function names and the total numbers of them'
                         ' in the given python file.',
                         'Class and function names are displayed in command line.',
                         'Total numbers of classes and functions are displayed in a bar graph.',
                         'Syntax: validate_class_contents [input source code file name.py].']))

    # Harry's work
    def do_dot_2_png(self, input_dot_file_name):
        """Generate and display png file from the given dot file.
        Syntax: dot_2_png [input dot file name.dot]"""
        try:
            if path.exists(input_dot_file_name):
                dot_command = 'dot -Tpng ' + input_dot_file_name + ' -o ' + input_dot_file_name + '.png'
                subprocess.call(dot_command)
                print(input_dot_file_name + '.png ' + ' are done')
                png_file_name = input_dot_file_name+".png"
                if path.exists(png_file_name):
                    # show png image
                    img = mpimg.imread(png_file_name)
                    fig = plt.imshow(img)
                    fig.axes.get_xaxis().set_visible(False)
                    fig.axes.get_yaxis().set_visible(False)
                    plt.show()
                else:
                    print("The image of class diagram cannot be generate.")
                    print("Please check with your system administrators.")
            else:
                print("Your given dot file does not exist in the current directory ")
                print("or your input arguments were wrong. The input arguments ")
                print("should be [dot_file_name.dot]. ")
                print("Please try again!")
        except Exception as err:
            print("Please try again! The exception is: ", err)

    # Harry's work
    def help_dot_2_png(self):
        """Help for dot_2_png command to generate and display png file from the given dot file.
                Syntax: dot_2_png [input dot file name.dot]"""
        print("\n".join(['Generate and display png file from the given dot file.',
                         'Syntax: dot_2_png [input dot file name.dot].']))

    # Harry's work
    def do_quit(self, line):
        """Exit this command line interpreter"""
        print("Quitting......")
        return True

    # Harry's work
    def help_quit(self):
        print("\n".join(['Quit from this CLI', ':return: True']))

# below is for manual testing only
if __name__ == '__main__':
    my_cli = MyCli()
    my_cli.cmdloop()


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

class Brush:
    def __init__(self, STATE, SIZE, X_POSITION, Y_POSITION, PAINT, CURSOR):
        self.state = False
        self.size = SIZE
        self.x = X_POSITION
        self.y = Y_POSITION
        self.paint = PAINT
        self.cursor = CURSOR

    def switch(self):
        self.state = not self.state

    def move(self, CANVAS, DIRECTION):
        if DIRECTION == "w":
            self.y -= 1
        elif DIRECTION == "s":
            self.y += 1
        elif DIRECTION == "d":
            self.x += 1
        elif DIRECTION == "a":
            self.x -= 1
        # if paint is on
        if self.state:
            self.draw(CANVAS)

    def draw(self, CANVAS):
        if self.state:
            for row in range(self.y, self.y + self.size):
                for col in range(self.x, self.x + self.size):
                    CANVAS.grid[row][col] = True
    def mapDraw(self, CANVAS, MOTION):
        # coming soon
        pass


from PyIRC.common.dispatch import PRIORITY_DEFAULT

def dispatch_account(client, line):
    """ Do account-notify stuff """
    if not line.hostmask:
        return

    nick = line.hostmask.nick
    if not nick:
        return

    account = line.params[-1]
    if account == '*':
        # Unset
        account = ''

    if nick == client.current_nick:
        return

    if nick not in client.users:
        # Not entirely sure why this would happen but ok :p.
        user = line.hostmask.user
        host = line.hostmask.host

        client.create_user(nick, user, host)

    client.users[nick].account = account

hooks_in = (
    ('ACCOUNT', PRIORITY_DEFAULT, dispatch_account),
)


import math
class Heap:
    a = []
    n = 0
    heap = []
    def __init__(self,a,n):
        self.n = n
        self.a = a
        self.heap = []
    def insert(self,k):#insert element k into the existing heap; maintain heap property
        self.heap.append(k)
        c = len(self.heap) - 1
        p = math.ceil(c/2) - 1
        while(p>=0 and c>0 and self.heap[p][1]>self.heap[c][1]):#bubble-up
            self.heap[p],self.heap[c] = self.heap[c],self.heap[p]
            c = p
            p = math.ceil(c/2) - 1
    def heapify(self):
        for i in range(self.n):
            self.insert(self.a[i])
        #self.displayHeap()
    def extractMin(self):
        return self.deleteAtIndex(0)
    def deleteAtIndex(self,k):#delete element at index k of the heap
        self.heap[k],self.heap[-1]=self.heap[-1],self.heap[k]
        item = self.heap.pop()
        #print("item popped=",item)
        lenHeap = len(self.heap)
        p = k
        lc = (2*p) + 1
        rc = (2*p) + 2
        while((lc<lenHeap and self.heap[p][1]>self.heap[lc][1]) or (rc<lenHeap and self.heap[p][1]>self.heap[rc][1])):#bubble-down
            if rc>=lenHeap or self.heap[lc][1]<self.heap[rc][1]:
                self.heap[lc],self.heap[p] = self.heap[p],self.heap[lc]
                p = lc
            else:
                self.heap[rc],self.heap[p]= self.heap[p],self.heap[rc]
                p = rc
            lc = (2*p) + 1
            rc = (2*p) + 2
        #self.displayHeap()
        return item
    def deleteElement(self,k):#delete element k from the heap
        self.heap.remove(k)
        self.a = self.heap
        self.heap = []
        self.heapify()
        self.displayHeap()
    def displayHeap(self):
        print(self.heap)

#The above Heap class is used for faster implementation of Dijkstra's Algorithm
nV = int(input())#no of vertices
a = []
V = []
for i in range(nV):#input graph
    a.append([])
    V.append(i)
    l = [y for y in input().split()]
    u = int(l[0]) - 1
    for i in range(1,len(l)):
        v,w = map(int,l[i].split(','))
        a[u].append([v-1,w])
#print("a=",a)
dist = [1000000]*nV#to store the single source shortest paths
s = 1#source vertex is say,vertex 1
X = []
X.append(s-1)#to keep track of vertices already visited
dist[s-1] = 0
dd = []#a double dimensional array each element is stroing the vertex number and it's key (that is, so far computed shortest distance)
for i in range(nV-1):
    dd.append([(i+1),1000000])
h = Heap(dd,nV-1)#using heap data structure for fast implementation
h.heapify()
while(len(X)<len(V)):
    #print("X=",X)
    v = X[-1]
    #print("v=",v)
    adjv = a[v]
    #print("adjv=",adjv)
    for adj in adjv:
        vertex = adj[0]
        weight = adj[1]
        if dist[vertex]>dist[v]+weight:
            dd[vertex-1][1] = min(dd[vertex-1][1],dist[v]+weight)#updating keys of the objects in heap
    h.a = dd
    h.heap = []
    h.heapify()
    item = h.extractMin()
    X.append(item[0])
    if dist[item[0]]> item[1]:
        dist[item[0]]=item[1]
    dd[item[0]-1]=[item[0],1000000]
    #print("dd=",dd)
print("dist=",dist)
#print("dd=",dd)
#h.displayHeap()


import contextlib
import ir_measures
from ir_measures import providers, measures, Metric
from ir_measures.providers.base import Any, Choices, NOT_PROVIDED

class JudgedProvider(providers.Provider):
    """
    python implementation of judgment rate
    """
    NAME = 'judged'
    SUPPORTED_MEASURES = [
        measures._Judged(cutoff=Any())
    ]

    def _evaluator(self, measures, qrels):
        cutoffs = []
        for measure in ir_measures.util.flatten_measures(measures):
            if measure.NAME == 'Judged':
                cutoffs.append((measure['cutoff'], measure))
            else:
                raise ValueError(f'unsupported measure {measure}')
        qrels = ir_measures.util.QrelsConverter(qrels).as_dict_of_dict()
        return JudgedEvaluator(measures, qrels, cutoffs)

class JudgedEvaluator(providers.Evaluator):
    def __init__(self, measures, qrels, cutoffs):
        super().__init__(measures, set(qrels.keys()))
        self.qrels = qrels
        self.cutoffs = cutoffs

    def _iter_calc(self, run):
        run = ir_measures.util.RunConverter(run).as_dict_of_dict()
        sorted_run = {q: list(sorted(run[q].items(), key=lambda x: (-x[1], x[0]))) for q in run}
        for qid in run:
            qid_qrels = self.qrels.get(qid)
            if qid_qrels:
                for cutoff, measure in self.cutoffs:
                    judged_c = sum((did in qid_qrels) for did, _ in sorted_run.get(qid, [])[:cutoff])
                    value = judged_c / cutoff
                    yield Metric(query_id=qid, measure=measure, value=value)

providers.register(JudgedProvider())


# -*- coding: utf-8 -*-
"""
Noise Fitting Brownian Motion Data

Ryan Dwyer

2013-11-22

This document will implement John Marohn's Brownian motion noise
fitting routine in Python. The class ``BrownianMotionFitter`` implements the
fitting routine. The rest of the module consists of helper functions for
specific portions of the fitting procedure and data input / output.

Dependencies:

- numpy
- scipy
- matplotlib
- pint
- uncertainties
- h5py

"""

from __future__ import division
import os
import errno

import numpy as np
import scipy
import scipy.stats
import six
sp = scipy
from scipy.optimize import curve_fit
import matplotlib as mpl
# Backend should be set correctly here.
from uncertainties import correlated_values, ufloat
import h5py
import pint

u = pint.UnitRegistry()

k_B = u.boltzmann_constant

class Cantilever(object):
    """Implement a simple unit cantilever, and require all inputs."""

    k_B = k_B

    def __init__(self, f_c, k_c, Q):
        """Initialize the cantilever."""
        self.f_c = f_c
        self.k_c = k_c
        self.Q = Q

    # Properties of the cantilver
    @property
    def omega_c(self):
        """Return the angular resonance frequency of the cantilever."""
        return self.f_c * 2 * pi

    @property
    def Gamma_i(self):
        """Return the cantilever's intrinsic dissipation."""
        return (self.k_c / (self.omega_c * self.Q))

    def F_min(self, T, bandwidth=1*u.Hz):
        """Return the thermally limited minimum detectable
        force (pN).

        The optional bandwidth parameter allows determining
        a miniumun force over a broader or narrower bandwidth
        than 1 Hz."""
        return ((4 * self.k_B * self.Gamma_i * T * bandwidth) ** 0.5)

    # Representations of the cantilever
    def __str__(self):
        """Write out the cantilever as its most important parameters:
        resonance frequency, spring constant, quality factor and
        intrinsic friction."""
        return "f_c = {self.f_c}, k_c = {self.k_c}, Q = {self.Q}\
Gamma_i = {self.Gamma_i}".format(self=self)

    def __repr__(self):
        """Return a representation of the cantilever. Rounds the cantilever
        to 9 digits, so eval(repr(cantilever)) is not necessarily equal to
        cantilever."""
        return "Cantilever(f_c = {self.f_c}, k_c = {self.k_c}, \
Q = {self.Q})".format(self=self)

class BrownianMotionFitter(object):
    """This object fits Brownian motion position fluctuation data to
    calculate the spring constant of the cantilever and the
    detector noise floor.
    """

    def __init__(self, f, PSD, PSD_ci, T, est_cant):
        """
        f
            frequency (Hz)

        PSD
            Power spectral density of position fluctuations (nm^2 / Hz)

        PSD_ci
            The confidence intervals corresponding to the PSD data (nm^2 / Hz)

        T
            temperature (K)

        est_cant
            A unitted cantilever with f_c, k_c and Q defined as best guesses.

        """
        self.f = f
        self.PSD_raw = PSD
        self.PSD_ci_raw = PSD_ci
        self.T = T * u.K
        self.est_cant = est_cant

        self.rcParams = {}

    def _guess_P_detector(self):
        """Guess a low, but reasonable number for the detector noise floor.
        This is used to scale the data for fitting."""
        try:
            self.mask
        except AttributeError:
            # Make a dummy mask of all trues if no mask has been set
            self.mask = np.isfinite(self.PSD_raw)

        self.P_detector0_raw = np.percentile(self.PSD_raw[self.mask], 25)

    def _scale_data(self):
        """Scale the raw data for fitting, so that floating point number errors
        are not an issue."""
        self.PSD = self.PSD_raw / self.P_detector0_raw
        self.P_detector0 = 1

    def calc_fit(self, f_min=None, f_max=None):
        """Fit the power spectrum data over the frequency range
        f_min to f_min, using a 3 pass approach.

        If no f_min and f_max are provided, it fits over the entire range
        of input frequencies."""

        # Set f_min, f_max
        if f_min is None:
            self.f_min = self.f.min()
        else:
            self.f_min = f_min
        if f_max is None:
            self.f_max = self.f.max()
        else:
            self.f_max = f_max

        # Store a mask to aid fitting and plotting
        self.mask = mask = make_mask(self.f, self.f_min, self.f_max)

        self._guess_P_detector()
        self._scale_data()

        f = self.fit_f = self.f[mask]
        PSD = self.fit_PSD = self.PSD[mask]
        PSD_ci = self.PSD_ci = self.PSD_ci_raw[mask]
        self.fit_PSD_raw = self.PSD_raw[mask]

        self._calc_initial_params()

        self._first_pass(f, PSD, PSD_ci)
        self._second_pass(f, PSD, PSD_ci)
        self._final_pass(f, PSD, PSD_ci)
        self._prepare_output()

    def _calc_initial_params(self):
        """Use the estimates to calculate initial parameters to use for
        the fitting functions."""
        f_c = self.est_cant.f_c.to(u.Hz)
        k_c = self.est_cant.k_c.to(u.N/u.m)
        Q = self.est_cant.Q.magnitude

        P_x0guess = (calc_P_x0(f_c, k_c, Q, self.T) /
                    (self.P_detector0_raw * u.nm ** 2 / u.Hz))

        self.initial_params = [P_x0guess.magnitude, f_c.magnitude, Q]

    def _first_pass(self, f, PSD, PSD_ci):
        """Fit the power spectrum data using initial guesses for the
        parameters and a fixed noise floor. Fitting outputs stored in
        self.popt1, self.pcov1.

        We use user input guesses for the three cantilever parameters,
        and fix the thermal noise floor to a low but reasonable guess.
        Then, we fit the remaining three parameters :math:`P_{x0}, f_c, Q`.
        """

        def Pf_fixed_P_detector(f, P_x0, f_c, Q):
            return Pf(f, P_x0, f_c, Q, self.P_detector0)

        self.popt1, self.pcov1 = curve_fit(Pf_fixed_P_detector,
                                           f, PSD,
                                           p0=self.initial_params,
                                           sigma=PSD_ci)

    def _second_pass(self, f, PSD, PSD_ci):
        """Fit the power spectrum holding the center frequency :math:`f_c`
        fixed. Fitting outputs stored in self.popt2, self.pcov2."""
        f_c = self.popt1[1]

        def Pf_fixed_f_c(f, P_x0, Q, P_detector):
            return Pf(f, P_x0, f_c, Q, P_detector)

        p0 = [self.popt1[0], self.popt1[2], self.P_detector0]

        self.popt2, self.pcov2 = curve_fit(Pf_fixed_f_c,
                                           f, PSD,
                                           p0=p0,
                                           sigma=PSD_ci)

    def _final_pass(self, f, PSD, PSD_ci):
        """Fit the power spectrum allowing all parameters to
        determined by the fitting algorithm. Fitting outputs stored to
        self.popt, self.pcov.

        The fit data and raw fit data is stored to self.PSD_fit,
        self.PSD_fit_raw."""
        popt1, popt2 = self.popt1, self.popt2
        f_c = popt1[1]
        p0 = [popt2[0], f_c, popt2[1], popt2[2]]
        self.popt, self.pcov = curve_fit(Pf,
                                         f, PSD,
                                         p0=p0,
                                         sigma=PSD_ci)
        self.PSD_fit = Pf(f, *self.popt)
        self.PSD_fit_raw = self.PSD_fit * self.P_detector0_raw

    def _prepare_output(self):
        f_c, k_c, Q, P_detector = translate_fit_parameters(
            self.popt, self.pcov, self.P_detector0_raw, self.T)
        self.residuals = (self.PSD_fit - self.PSD[self.mask])
        self.reduced_residuals = self.residuals / self.PSD_fit
        self.reduced_residuals_sorted = np.sort(self.reduced_residuals)
        self._fit_residuals()
        self.f_c, self.k_c, self.Q, self.P_detector = f_c, k_c, Q, P_detector

    def report(self):
        return u"""
    Input
    -----------------------------------------------
    Temperature           T: {self.T:P}

    Estimates
    -----------------------------------------------
    Spring constant     k_c: {self.est_cant.k_c:P}
    Resonance frequency f_c: {self.est_cant.f_c:P}
    Quality factor        Q: {self.est_cant.Q:P}

    Fitting
    -----------------------------------------------
    Fit frequency min f_min: {f_min:P}
    Fit frequency max f_max: {f_max:P}

    Results
    -----------------------------------------------
    Resonance frequency f_c: {self.f_c:.1uSP}
    Spring constant     k_c: {self.k_c:.1uSP}
    Quality Factor        Q: {self.Q:.1uSP}
    Detector Noise         : {self.P_detector:.1uSP}
            """.format(self=self, f_min=self.f_min*u.Hz, f_max=self.f_max*u.Hz)

    def rst_report(self):
        return u"""
Report::

    Input
    -----------------------------------------------
    Temperature           T: {self.T:P}

    Estimates
    -----------------------------------------------
    Spring constant     k_c: {self.est_cant.k_c:P}
    Resonance frequency f_c: {self.est_cant.f_c:P}
    Quality factor        Q: {self.est_cant.Q:P}

    Fitting
    -----------------------------------------------
    Fit frequency min f_min: {f_min:P}
    Fit frequency max f_max: {f_max:P}

    Results
    -----------------------------------------------
    Resonance frequency f_c: {self.f_c:.1uSP}
    Spring constant     k_c: {self.k_c:.1uSP}
    Quality Factor        Q: {self.Q:.1uSP}
    Detector Noise         : {self.P_detector:.1uSP}
            """.format(self=self, f_min=self.f_min*u.Hz, f_max=self.f_max*u.Hz)

    def print_output(self):
        print(self.report())

# These plotting functions could use a refactor
# Lots of duplicated code, logic fairly separate from the rest of the class
    def plot_fit(self, filename=None):
        """Plot the calculated fit."""
        f = self.fit_f

        mpl.rcParams.update(self.rcParams)
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots()

        ax.semilogy(f, self.fit_PSD_raw, f, self.PSD_fit_raw)

        ax.set_xlim(self.f_min, self.f_max)
        ax.set_xlabel('Frequency [Hz]')
        ax.set_ylabel(u'PSD [nm²/Hz]')

        fig.gca()
        fig.canvas.draw()
        fig.tight_layout(pad=0.25)

        if filename is not None:
            fig.savefig(filename)

        self.fit_fig, self.fit_ax = fig, ax
        return fig, ax

    def plot_residuals(self, filename=None):
        """Plot the residuals of the calculated fit."""

        mpl.rcParams.update(self.rcParams)
        import matplotlib.pyplot as plt

        fig, ax = plt.subplots()

        ax.plot(self.fit_f, self.residuals)

        ax.set_xlabel('Frequency [Hz]')
        ax.set_ylabel(u'Residual [nm²/Hz]')

        fig.gca()
        fig.canvas.draw()
        fig.tight_layout(pad=0.25)

        if filename is not None:
            fig.savefig(filename)
        return fig, ax

    def plot_reduced_residuals(self, filename=None):
        mpl.rcParams.update(self.rcParams)
        import matplotlib.pyplot as plt

        fig, ax = plt.subplots()

        ax.plot(self.fit_f, self.reduced_residuals)

        ax.set_xlabel('Frequency [Hz]')
        ax.set_ylabel('Normalized Residual')

        fig.gca()
        fig.canvas.draw()
        fig.tight_layout(pad=0.25)

        if filename is not None:
            fig.savefig(filename)
        return fig, ax

    def plot_cdf(self, filename=None):
        x = self.reduced_residuals_sorted
        size = x.size
        y = np.arange(1, 1 + size) / size
        popt = self.p_residuals

        mpl.rcParams.update(self.rcParams)
        import matplotlib.pyplot as plt

        fig, ax = plt.subplots()

        ax.plot(x, y, 'bo', x, cdf(x, *popt), 'g-')
        ax.set_xlabel("Reduced Residual")
        ax.set_ylabel("CDF")

        fig.gca()
        fig.canvas.draw()
        fig.tight_layout(pad=0.25)

        if filename is not None:
            fig.savefig(filename)
        return fig, ax

    def _fit_residuals(self):
        self.p_residuals = fit_residuals(self.reduced_residuals_sorted)
        print("""
Residuals
-------------------------------------
        Mean: {self.p_residuals[0]:.2e}
   Std. dev.: {self.p_residuals[1]:.2e}""".format(self=self))

def Pf(f, P_x0, f_c, Q, P_detector):
    """The equation to calculate :math:`P_x(f)` from the cantilever
    parameters and detector noise floor.

    f
        Frequency, the independent variable.

    P_x0
        The zero frequency power spectral density of position fluctuations.

    f_c
        The cantilever resonance frequency.

    Q
        The quality factor of the cantilever.

    P_detector
        The detector noise floor.
        """
    return (P_x0 * f_c**4 /
            ((f**2 - f_c**2)**2 + f**2 * f_c**2 / Q**2)
            + P_detector)

def make_mask(f, f_min, f_max):
    """Return a mask of the array f. The mask can be used
    to give only elements of f between fmin and fmax.

    Use the resulting mask to block off elements of f outside
    of the specified bounds, as shown below.

    >>> f = np.arange(10)
    >>> mask = make_mask(f, 1.5, 5.5)
    >>> f[mask]
    array([2, 3, 4, 5])
    """
    mask_low = f < f_max
    mask_high = f > f_min
    return np.logical_and(mask_high, mask_low)

def translate_fit_parameters(popt, pcov, P_detector0_raw, T=300*u.K):
    """Take the fit parameters and covariances, and converts them to
    SI values and errors for f_c, k_c, Q.

    Also makes sure all values are positive."""
    pvals = correlated_values(popt, pcov)
    punits = [u.nm**2 / u.Hz, u.Hz, u.dimensionless, u.nm**2 / u.Hz]
    scales = [P_detector0_raw, 1, 1, P_detector0_raw]

    def create_measurement(uncert_val, unit, scale):
        return (uncert_val.n * unit).plus_minus(uncert_val.s) * scale

    P_x0, f_c, Q, P_detector = [np.abs(
                                    create_measurement(uncert_val, unit, scale)
                                ) for
                                uncert_val, unit, scale in
                                zip(pvals, punits, scales)]

    k_c = np.abs(calc_k_c(f_c, Q, P_x0, T))
    return f_c, k_c, Q, P_detector

def calc_k_c(f_c, Q, P_x0, T=(300, 1)*u.K):
    """Calculate the spring constant, returning a pint quantity containing
    a value with uncertainty, if uncertainty is used on the input (ufloat)."""
    return ((2 * k_B * T) / (np.pi * f_c * Q * P_x0)).to(u.N / u.m)

def calc_P_x0(f_c, Q, k_c, T):
    """Calculate the thermal noise floor, returning a pint quantity in
    units of nm^2 / Hz."""
    return ((2 * k_B * T) / (np.pi * f_c * Q * k_c)).to(u.nm ** 2 / u.Hz)

def iterate_fit(func, x, y, p0, sigma_i, wgt):
    popt = [[]]
    pcov = [[]]
    popt[0], pcov[0] = curve_fit(func, x, y, p0=p0, sigma=sigma_i)
    for i in xrange(10):
        new_sigma = wgt * func(x, *popt[-1])
        opt, cov = curve_fit(func, x, y, p0=popt[-1], sigma=new_sigma)
        popt.append(opt)
        pcov.append(cov)
    PSD_fit = func(x, *popt[-1])
    return popt[-1], pcov[-1], PSD_fit

def avg_ci_data(data, axis=0):
    """Average the data along the given dimension, and calculate the 95
    percent confidence interval. Return the avg, ci arrays."""
    data_avg = data.mean(axis=axis)
    data_std = data.std(axis=axis, ddof=1)
    data_ci = data_std / data.shape[axis]**0.5 * 1.96
    return data_avg, data_ci

def get_data(filename_or_fh):
    """Extract power spectrum data from an HDF5 file.
    Return f, PSD_mean, PSD_conf_int.

    The HDF file should have the following structure:

    x
        A table containing the frequencies of each point in the power
        spectrum.

    y
        Contains power spectra PSD_0(f) through PSD_(n-1)(f), arranged
            as follows, with each power spectrum occupying 1 column.
        PSD_0(0 Hz) PSD_1(O Hz)
        PSD_0(1 Hz) PSD_1(1 Hz) ...

    y_std
        Contains standard deviations for the data points in y

    See http://h5labview.sourceforge.net and http://www.h5py.org for
    more information."""
    is_filename = isinstance(filename_or_fh, six.string_types)
    if is_filename:
        fh = h5py.File(filename_or_fh, 'r')
    else:
        fh = filename_or_fh

    # Put some checks about old-style files here.
    f = fh['x'].value
    PSD_mean = fh['y'].value
    PSD_ci = fh['y_std'].value * 1.96 / fh['y'].attrs['n_avg']**0.5

    if is_filename:
        fh.close()

    return f, PSD_mean, PSD_ci

def cdf(x, loc, scale):
    """The cumulative probability function of the normal distribution."""
    return sp.stats.norm.cdf(x, loc=loc, scale=scale)

def fit_residuals(sorted_residuals):
    """Return the result of fitting the residuals to the normal cdf.
    """
    size = sorted_residuals.size
    y = np.arange(1, 1 + size) / size

    popt, _ = curve_fit(cdf, sorted_residuals, y)

    return popt

def convert_data(oldfile, newfile, fileformat='v1'):
    """Converts data from the v1 format to the v2 format"""
    oldf = h5py.File(oldfile, 'r')
    if oldf.get('PSD', getclass=True) is not h5py.Group:
        oldf.close()
        raise ValueError("It appears you have inputted a new style file.\
            The file will not be converted.")
    else:
        newf = h5py.File(newfile, 'w')
        newf['f'] = oldf['f'][:]
        # Include any dataset that starts with a small d
        # This should catch the default d001, d0002, d003,
        data_list = [value for key, value in oldf['PSD'].items()
                     if key.startswith('d')]
        newf['PSD'] = np.empty((oldf['f'].value.size, len(data_list)))

        for i, val in enumerate(data_list):
            newf['PSD'][:, i] = val[:]
        oldf.close()
        newf.close()

def silentremove(filename):
    """If a file exists, delete it. Otherwise, return nothing.
       See http://stackoverflow.com/q/10840533/2823213"""
    try:
        os.remove(filename)
    except OSError as e:  # this would be "except OSError, e:" before Python 2.6
        if e.errno != errno.ENOENT:  # errno.ENOENT = no such file or directory
            raise  # re-raise exception if a different error occured

from ._version import get_versions
__version__ = get_versions()['version']
del get_versions


from multiprocessing import Process
import subprocess
import socket
import os
import time

Django_Server_Web_Port = 8000
Java_Sock_Port = 8001
Django_Sock_Port = 8005

JAVA_CMD = ['java','-cp','/home/jelix/example-zookeeper.jar','cn.itcast.zk.TestZKClient']
DJANGO_CMD = ['/home/jelix/HelloWorld/manage.py','runserver','0.0.0.0:8000']

def run_django_server(name):
    django_proc = subprocess.call(DJANGO_CMD,stdout=open('./test_3.out','w'),stderr=open('./test_4.out','w'))

# 子进程要执行的代码
def run_java_sock_proc(name):
	print('Run child process %s (%s)...' % (name, os.getpid()))
	server = socket.socket()#声明socket类型，并且生成socket连接对象
	server.bind(('localhost',Java_Sock_Port))#把服务器绑定到localhost的6969端口上
	server.listen(5)#开始监听
	print("等待连接中……")
	django_proc_flag = False
	django_proc = Process(target=run_django_server, args=('test_django',))
	while True:
		conn,addr = server.accept()#接收连接
		print("***连接成功***")
		data = bytes.decode(conn.recv(4096)).split() #接收客户发来的数据

		me_flag = data[-1]==data[-2]
		if me_flag and (not django_proc_flag):
			django_proc.start()
			print("Django Server Started")
			django_proc_flag = True

		if (not me_flag) and django_proc_flag:
			django_proc.terminate()
			print("Django Server Stopped")
			django_proc_flag = False

		print("接收到的命令为：",data[-1]==data[-2])
	server.close()

def run_java(name):
    java_proc = subprocess.call(JAVA_CMD,stdout=open('./test_1.out','w'),stderr=open('./test_2.out','w'))

def run_django_sock(name):
	print('Run child process %s (%s)...' % (name, os.getpid()))
	server = socket.socket()#声明socket类型，并且生成socket连接对象
	server.bind(('localhost',Django_Sock_Port))#把服务器绑定到localhost的6969端口上
	server.listen(5)#开始监听
	print("等待连接中……")
	while True:
		conn,addr = server.accept()#接收连接
		print("***连接成功***")
		data = conn.recv(4096) #接收客户发来的数据
		print("接收到的命令为：",data)

	server.close()

if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    # s = Process(target=run_django_sock, args=('test_django_sock',))
    # s.start()
    # time.sleep(1)
    # p = Process(target=run_java_sock_proc, args=('test_java_sock',))
    # print('Child process will start.')
    # p.start()
    # time.sleep(1)
    # r = Process(target=run_java, args=('test_java',))
    # r.start()
    # s.join()
    # r.join()
    # p.join()
    # print('Child process end.')
    s = Process(target=run_django_sock, args=('test_django_sock',))
    s.start()
    s.join()


from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image

class SwitchButton(Button):
    def __init__(self, manager, direction, screen, **kwargs):
        super(SwitchButton, self).__init__(**kwargs)
        self.manager = manager
        self.screen = screen
        self.size_hint_y = 0.2
        self.size_hint_x = 0.1

        if( direction == 'left'):
            self.text = "<<"
            self.pos_hint = {'x': 0.0, 'y': 0.4}
            self.direction = 'right'
        elif( direction == 'right' ):
            self.text = ">>"
            self.pos_hint = {'x': 0.9, 'y': 0.4}
            self.direction = 'left'

    def on_press(self):
        self.manager.transition.direction = self.direction
        self.manager.current = self.screen

class ImageButton(ButtonBehavior, Image):
    pass


#!/usr/bin/env python
# ROS IMPORTS
import roslib; roslib.load_manifest('simple_ur_driver')
import rospy
import tf; import tf_conversions as tf_c
import PyKDL
# URDF
from urdf_parser_py.urdf import URDF
from pykdl_utils.kdl_kinematics import KDLKinematics
# MSGS and SERVICES
from simple_ur_msgs.srv import *
from sensor_msgs.msg import JointState
from geometry_msgs.msg import PoseStamped
from predicator_msgs.msg import *
from std_msgs.msg import *
import time
import threading
import socket
# URX Universal Robot Driver
import urx
# OTHER
import logging
import numpy as np
from pid import PID

class URDriver():
    MAX_ACC = .5
    MAX_VEL = 1.8
    JOINT_NAMES = ['shoulder_pan_joint', 'shoulder_lift_joint', 'elbow_joint',
               'wrist_1_joint', 'wrist_2_joint', 'wrist_3_joint']

    def __init__(self):
        rospy.init_node('ur_simulation',anonymous=True)
        rospy.logwarn('SIMPLE_UR SIMULATION DRIVER LOADING')
        # TF
        self.broadcaster_ = tf.TransformBroadcaster()
        self.listener_ = tf.TransformListener()
        # SERVICES
        self.servo_to_pose_service = rospy.Service('simple_ur_msgs/ServoToPose', ServoToPose, self.servo_to_pose_call)
        self.set_stop_service = rospy.Service('simple_ur_msgs/SetStop', SetStop, self.set_stop_call)
        self.set_teach_mode_service = rospy.Service('simple_ur_msgs/SetTeachMode', SetTeachMode, self.set_teach_mode_call)
        self.set_servo_mode_service = rospy.Service('simple_ur_msgs/SetServoMode', SetServoMode, self.set_servo_mode_call)
        # PUBLISHERS AND SUBSCRIBERS
        self.driver_status_publisher = rospy.Publisher('/ur_robot/driver_status',String)
        self.robot_state_publisher = rospy.Publisher('/ur_robot/robot_state',String)
        self.joint_state_publisher = rospy.Publisher('joint_states',JointState)
        # self.follow_pose_subscriber = rospy.Subscriber('/ur_robot/follow_goal',PoseStamped,self.follow_goal_cb)
        # Predicator
        self.pub_list = rospy.Publisher('/predicator/input', PredicateList)
        self.pub_valid = rospy.Publisher('/predicator/valid_input', ValidPredicates)
        rospy.sleep(1)
        pval = ValidPredicates()
        pval.pheader.source = rospy.get_name()
        pval.predicates = ['moving', 'stopped', 'running']
        pval.assignments = ['robot']
        self.pub_valid.publish(pval)
        # Rate
        self.run_rate = rospy.Rate(100)
        self.run_rate.sleep()
        ### Set Up Simulated Robot ###
        self.driver_status = 'IDLE'
        self.robot_state = 'POWER OFF'
        robot = URDF.from_parameter_server()
        self.kdl_kin = KDLKinematics(robot, 'base_link', 'ee_link')
        # self.q = self.kdl_kin.random_joint_angles()
        self.q = [-1.5707,-.785,-3.1415+.785,-1.5707-.785,-1.5707,0] # Start Pose?
        self.start_pose = self.kdl_kin.forward(self.q)
        self.F_start = tf_c.fromMatrix(self.start_pose)
        # rospy.logwarn(self.start_pose)
        # rospy.logwarn(type(self.start_pose))
        # pose = self.kdl_kin.forward(q)
        # joint_positions = self.kdl_kin.inverse(pose, q+0.3) # inverse kinematics
        # if joint_positions is not None:
        #     pose_sol = self.kdl_kin.forward(joint_positions) # should equal pose
        # J = self.kdl_kin.jacobian(q)
        # rospy.logwarn('q:'+str(q))
        # rospy.logwarn('joint_positions:'+str(joint_positions))
        # rospy.logwarn('pose:'+str(pose))
        # if joint_positions is not None:
        #     rospy.logwarn('pose_sol:'+str(pose_sol))
        # rospy.logwarn('J:'+str(J))

        ### START LOOP ###
        while not rospy.is_shutdown():
            if self.driver_status == 'TEACH':
                self.update_from_marker()

            # if self.driver_status == 'SERVO':
            #     self.update_follow()

            # Publish and Sleep
            self.publish_status()
            self.send_command()
            self.run_rate.sleep()

        # Finish
        rospy.logwarn('SIMPLE UR - Simulation Finished')

    def update_from_marker(self):
        try:
            F_target_world = tf_c.fromTf(self.listener_.lookupTransform('/world','/endpoint_interact',rospy.Time(0)))
            F_target_base = tf_c.fromTf(self.listener_.lookupTransform('/base_link','/endpoint_interact',rospy.Time(0)))
            F_base_world = tf_c.fromTf(self.listener_.lookupTransform('/world','/base_link',rospy.Time(0)))
            self.F_command = F_base_world.Inverse()*F_target_world
            M_command = tf_c.toMatrix(self.F_command)

            joint_positions = self.kdl_kin.inverse(M_command, self.q) # inverse kinematics
            if joint_positions is not None:
                pose_sol = self.kdl_kin.forward(joint_positions) # should equal pose
                self.q = joint_positions
            else:
                rospy.logwarn('no solution found')

            # self.send_command()

        except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException) as e:
            rospy.logwarn(str(e))

    def send_command(self):
        self.current_joint_positions = self.q
        msg = JointState()
        msg.header.stamp = rospy.get_rostime()
        msg.header.frame_id = "simuated_data"
        msg.name = self.JOINT_NAMES
        msg.position = self.current_joint_positions
        msg.velocity = [0]*6
        msg.effort = [0]*6
        self.joint_state_publisher.publish(msg)

        pose = self.kdl_kin.forward(self.q)
        F = tf_c.fromMatrix(pose)
        # F = self.F_command
        self.current_tcp_pose = tf_c.toMsg(F)
        self.current_tcp_frame = F
        self.broadcaster_.sendTransform(tuple(F.p),tuple(F.M.GetQuaternion()),rospy.Time.now(), '/endpoint','/base_link')

    def set_teach_mode_call(self,req):
        if req.enable == True:
            # self.rob.set_freedrive(True)
            self.driver_status = 'TEACH'
            return 'SUCCESS - teach mode enabled'
        else:
            # self.rob.set_freedrive(False)
            self.driver_status = 'IDLE'
            return 'SUCCESS - teach mode disabled'

    def set_servo_mode_call(self,req):
        if req.mode == 'SERVO':
            self.driver_status = 'SERVO'
            return 'SUCCESS - servo mode enabled'
        elif req.mode == 'DISABLE':
            self.driver_status = 'IDLE'
            return 'SUCCESS - servo mode disabled'

    def set_stop_call(self,req):
        rospy.logwarn('SIMPLE UR - STOPPING ROBOT')
        self.rob.stop()
        return 'SUCCESS - stopped robot'

    def servo_to_pose_call(self,req):
        if self.driver_status == 'SERVO':
            rospy.logwarn(req)
            self.F_command = tf_c.fromMsg(req.target)
            M_command = tf_c.toMatrix(self.F_command)
            # Calculate IK
            joint_positions = self.kdl_kin.inverse(M_command, self.q) # inverse kinematics
            if joint_positions is not None:
                pose_sol = self.kdl_kin.forward(joint_positions) # should equal pose
                self.q = joint_positions
            else:
                rospy.logwarn('no solution found')
            # self.send_command(F_command)
            return 'SUCCESS - moved to pose'
        else:
            rospy.logerr('SIMPLE UR -- Not in servo mode')
            return 'FAILED - not in servo mode'

    def publish_status(self):
        self.driver_status_publisher.publish(String(self.driver_status))
        self.robot_state_publisher.publish(String(self.robot_state))

        ps = PredicateList()
        ps.pheader.source = rospy.get_name()
        ps.statements = []

        statement = PredicateStatement( predicate='moving',
                                        confidence=1,
                                        value=PredicateStatement.FALSE,
                                        num_params=1,
                                        params=['robot', '', ''])
        ps.statements += [statement]
        statement = PredicateStatement( predicate='stopped',
                                        confidence=1,
                                        value=PredicateStatement.TRUE,
                                        num_params=1,
                                        params=['robot', '', ''])
        ps.statements += [statement]
        statement = PredicateStatement( predicate='running',
                                        confidence=1,
                                        value=PredicateStatement.TRUE,
                                        num_params=1,
                                        params=['robot', '', ''])
        ps.statements += [statement]
        self.pub_list.publish(ps)

    def check_robot_state(self):
        self.robot_state == 'RUNNING SIMULATION'
        self.driver_status = 'IDLE - SIMULATION'

if __name__ == "__main__":
    robot_driver = URDriver()


import dropbox
import csv

mode='H'

accessToken='g4xbkHKkKwAAAAAAAAACqwCjEAwcxfUzjS_kyjInXWmBR609R6AlRASjsWNNnogx'

dbx = dropbox.Dropbox(accessToken)
dbx.users_get_current_account()
list=[]

n1=0
n2=0
mode=['0','D','G','P','H']
for item in mode:
  for entry in dbx.files_list_folder('/style library/'+item+' Styles/White Background/').entries:
    #print(entry.path_lower)
    link=dbx.sharing_create_shared_link(entry.path_lower)
    URL=link.url
    filename=entry.name
    style=filename[0:filename.find(' ')]
    if '_'in filename:
      color=filename[filename.find(' '):filename.find('_')]
    else:
      color=filename[filename.find(' '):filename.find('.jpg')]

    if filename.count('_')>0:#if back pic
      s=filename.find('_')
      #print (s+1,filename.find('_wm'))
      index=filename[s+1:filename.find('_wm')]
    else:
      index='0'
    print (style,color,index,URL)
    sublist=[]
    sublist.append(style)
    sublist.append(color)
    sublist.append(URL.replace('https://www.dropbox.com','https://dl.dropboxusercontent.com'))
    sublist.append(index)
    list.append(sublist)

fo= open('All styles url.csv',"w",newline='')
fieldnames=['style','color','url','index']
writer=csv.DictWriter(fo,fieldnames=fieldnames)

writer.writeheader()

for info in list:
    writer.writerow({'style':info[0],'color':info[1], 'url':info[2],'index':info[3]})
    print ("Report: ",info[0],info[1],info[2])


#coding=utf-8

from definition import *
import matplotlib.pyplot as plt
from planar_utils import plot_decision_boundary, load_planar_dataset

#加载数据
X, Y = load_planar_dataset()

shape_X = X.shape
shape_Y = Y.shape
m = shape_X[1]  # training set size
print ('The shape of X is: ' + str(shape_X))
print ('The shape of Y is: ' + str(shape_Y))
print ('I have m = %d training examples!' % (m))

# Build a model with a n_h-dimensional hidden layer
parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)

# Plot the decision boundary
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(4))
plt.show()

#预测准确率
predictions = predict(parameters, X)
print ('Accuracy: %d' % float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100) + '%')

#改变隐藏层单元数量进行训练预测查看
plt.figure(figsize=(16, 32))
hidden_layer_sizes = [1, 2, 3, 4, 5, 10, 20,50]
for i, n_h in enumerate(hidden_layer_sizes):
    # plt.subplot(5, 2, i+1)
    plt.title('Hidden Layer of size %d' % n_h)
    plt.show()
    parameters = nn_model(X, Y, n_h, num_iterations = 5000)
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    predictions = predict(parameters, X)
    accuracy = float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)
    print ("Accuracy for {} hidden units: {} %".format(n_h, accuracy))


import numpy as np
import os
lists=os.listdir('/mnt/blobfuse/train-output/ByMZ/multiplex10_patches_labelled/multiplex_training_with_background_label/')
#('/mnt/blobfuse/cnn-minibatches/maozheng_data/maozheng_tumor_region_ratio_240')
print(len(lists))
count=np.zeros((1,10))
for i in lists:
    a=i.rstrip('.npy')
    a=a.split('_')
    a=a[1:]
    a=[float(x) for x in a]
    a=np.array(a)
    count=count+a
print(count)


#!/usr/bin/python
from scipy.integrate import odeint
import matplotlib.pyplot as plt # for plotting
import numpy as np
from copy import copy

class Particle (object):

    """Class that describes particle"""
    m = 1.0

    def __init__(self, x0=1.0, v0=0.0,  tf = 10.0, dt = 0.001):
        self.x = x0
        self.v = v0
        self.x0 = x0
        self.v0 = v0
        self.t = 0.0
        self.tf = tf
        self.dt = dt

        self.tlabel = 'time (s)'
        self.xlabel = 'x (m)'
        self.vlabel = 'v (m/s)'

        npoints = int(tf/dt) # always starting at t = 0.0
        self.npoints = npoints
        self.tarray = np.linspace(0.0, tf,npoints, endpoint = True) # include final timepoint
        self.xv0 = np.array([self.x, self.v]) # NumPy array with initial position and velocity

    def F(self, x, v, t):
        # The force on a free particle is 0
        return array([0.0])

    def Euler_step(self):
        """
        Take a single time step using Euler method
        """

        a = self.F(self.x, self.v, self.t) / self.m
        self.x += self.v * self.dt
        self.v += a * self.dt
        self.t += self.dt

    def Verlet_step(self):

        a = self.F(self.x, self.v, self.t)/self.m
        self.x += self.v * self.dt + 0.5 * self.dt**2 * a

        a2 = self.F(self.x, self.v, self.t)/self.m
        self.v += 0.5 * self.dt * (a + a2)

        self.t +=self.dt

    def Euler_trajectory(self):
        """
        Loop over all time steps to construct a trajectory with Euler method
        Will reinitialize euler trajectory everytime this method is called
        """

        x_euler = []
        v_euler = []
        self.x = self.x0
        self.v = self.v0
        self.t = 0.0

        for ii in range(self.npoints):
            v_euler.append(self.v)
            x_euler.append(self.x)
            self.Euler_step()

        self.x_euler = np.array(x_euler)
        self.v_euler = np.array(v_euler)

    def verlet_trajectory(self):
        """
        Loop over all time steps to construct a trajectory with verlet method
        Will reinitialize verlet trajectory everytime this method is called
        """

        x_verlet = []
        v_verlet = []
        self.x = self.x0
        self.v = self.v0
        self.t = 0.0

        for ii in range(self.npoints):
            v_verlet.append(self.v)
            x_verlet.append(self.x)
            self.Verlet_step()

        self.x_verlet = np.array(x_verlet)
        self.v_verlet = np.array(v_verlet)

    def scipy_trajectory(self):
        """calculate trajectory using SciPy ode integrator"""
        self.xv = odeint(self.derivative, self.xv0, self.tarray)

    def derivative(self, xv, t):
        """right hand side of the differential equation"""
        x =xv[0]
        v =xv[1]
        a = self.F(x, v, t) / self.m
        return np.ravel(np.array([v, a]))

    def results(self):
        """
        Print out results in a nice format
        """

        print('Initial: t = 0.0, x = {}, v = {}, dt = {}'.format(self.x0 , self.v0, self.dt))
        print('Current: t = {}, x = {}, v = {}'.format(self.t, self.x , self.v))

        if hasattr(self, 'xv'):
            print('SciPy ODE Integrator:')
            print('t = {} x = {} v = {}'.format(self.tarray[-1], self.xv[-1, 0], self.xv[-1,1]))

    def plot(self, pt = 'trajectory', axs = None):
        """
        Make nice plots of our results
        """
        if not axs:
            fig1 = plt.figure()
            ax1 = fig1.add_subplot(111)

        if hasattr(self,'xv'):

            if pt == 'trajectory':
                if not axs:
                    ax1.plot(self.tarray, self.xv[:, 0], "k", label = 'odeint')
                else:
                    axs.plot(self.tarray, self.xv[:, 0], "k", label = 'odeint')

            if pt == 'phase':
                if not axs:
                    ax1.plot(self.xv[:, 0], self.xv[:, 1], "k",'.', label = 'odeint')
                else:
                    axs.plot(self.xv[:, 0], self.xv[:, 1], "k",'.', label = 'odeint')

        if hasattr(self,'x_euler'):

            if pt == 'trajectory':
                if not axs:
                    ax1.plot(self.tarray, self.x_euler, "b", label = 'euler')
                else:
                    axs.plot(self.tarray, self.x_euler, "b", label = 'euler')

            if pt == 'phase':
                if not axs:
                    ax1.plot(self.x_euler, self.v_euler, "b",'.', label = 'euler')
                else:
                    axs.plot(self.x_euler, self.v_euler, "b",'.', label = 'euler')

        if hasattr(self,'x_verlet'):

            if pt == 'trajectory':
                if not axs:
                    ax1.plot(self.tarray, self.x_verlet, "r", label = 'verlet')
                else:
                    axs.plot(self.tarray, self.x_verlet, "r", label = 'verlet')

            if pt == 'phase':
                if not axs:
                    ax1.plot(self.x_verlet, self.v_verlet, "r",'.', label = 'verlet')
                else:
                    axs.plot(self.x_verlet, self.v_verlet, "r",'.', label = 'verlet')
        if not axs:
            if pt == 'trajectory':
                ax1.set_xlabel(self.tlabel)
                ax1.set_ylabel(self.xlabel)

            if pt == 'phase':
                ax1.set_xlabel(self.xlabel)
                ax1.set_ylabel(self.vlabel)

            ax1.legend()

class Pendulum(Particle):

    """Subclass of Particle Class that describes a pendulum in a harmonic potential"""
    def __init__(self, l = 9.8, m = 1.0, x0 = 0.0 ,v0 = 0.0, tf = 50.0, dt = 0.001):

        super().__init__(x0,v0,tf,dt)
        # for pendulum x = theta [-pi, pi]
        g = 9.8
        omega0 = np.sqrt(g/l)

        self.l = l # length
        self.m = m # mass
        self.omega0 = omega0 # natural frequency

        self.tlabel = 'time ($1/\omega_0$)'
        self.xlabel = '$\\theta$ (radians)'
        self.vlabel = '$\omega$ (radians/s)'

    def Euler_step(self):
        # overload method to wrap x between [-pi,pi]

        Particle.Euler_step(self)
        if self.x > np.pi:
            self.x -= 2*np.pi
        elif self.x < -np.pi:
            self.x += 2*np.pi

    def verlet_step(self):
        # overload method to wrap x between [-pi,pi]

        Particle.verlet_step(self)
        if self.x > np.pi:
            self.x -= 2*np.pi
        elif self.x < -np.pi:
            self.x += 2*np.pi

    # overload method to wrap x between [-pi,pi]
    def scipy_trajectory(self):
        Particle.scipy_trajectory(self)

        x = self.xv[:,0]
        x_new = np.zeros(np.shape(x))
        x_new[0] = x[0]

        # find change in x between each point
        dx = np.diff(x)
        nx = np.shape(x)[0]

        for ii in range(1,nx):
            # reconstruct x array, checking for out of range values
            x_new[ii] = x_new[ii-1]+dx[ii-1]
            if x_new[ii] > np.pi:
                x_new[ii] -= 2*np.pi

            elif x_new[ii] < -np.pi:
                x_new[ii] += 2*np.pi

        self.xv_unwrap = copy(self.xv)
        self.xv[:,0] = x_new

    def F(self, x, v, t):
        g = 9.8
        F = - g/self.l*np.sin(x)

        return F


""" run decoding of X-ext (+ abs)"""
import argparse
import json
import os
from os.path import join
from datetime import timedelta
from time import time
from collections import Counter, defaultdict
from itertools import product
from functools import reduce
import operator as op

from cytoolz import identity, concat, curry

import torch
from torch.utils.data import DataLoader
from torch import multiprocessing as mp

from data.batcher import tokenize

from decoding import Abstractor, Extractor, DecodeDataset, BeamAbstractor, ConditionalAbstractor, BeamConditionalAbstractor
from decoding import make_html_safe

MAX_ABS_NUM = 6  # need to set max sentences to extract for non-RL extractor

def decode(save_path, abs_dir, ext_dir, split, batch_size, max_len, beam_size, diverse, cuda, disable_final_rerank=False, is_conditional_abs=False):
    start = time()
    # setup model
    if abs_dir is None:
        # NOTE: if no abstractor is provided then
        #       the whole model would be extractive summarization
        abstractor = identity
    elif is_conditional_abs:
        if beam_size == 1:
            abstractor = ConditionalAbstractor(abs_dir, max_len, cuda)
        else:
            abstractor = BeamConditionalAbstractor(abs_dir, max_len, cuda)
    else:
        #abstractor = Abstractor(abs_dir, max_len, cuda)
        if beam_size == 1:
            abstractor = Abstractor(abs_dir, max_len, cuda)
        else:
            abstractor = BeamAbstractor(abs_dir, max_len, cuda)

    if ext_dir is None:
        # NOTE: if no abstractor is provided then
        #       it would be  the lead-N extractor
        extractor = lambda art_sents: list(range(len(art_sents)))[:MAX_ABS_NUM]
    else:
        extractor = Extractor(ext_dir, max_ext=MAX_ABS_NUM, cuda=cuda)

    # setup loader
    def coll(batch):
        articles = list(filter(bool, batch))
        return articles
    dataset = DecodeDataset(split)

    n_data = len(dataset)
    loader = DataLoader(
        dataset, batch_size=batch_size, shuffle=False, num_workers=4,
        collate_fn=coll
    )

    # prepare save paths and logs
    for i in range(MAX_ABS_NUM):
        os.makedirs(join(save_path, 'output_{}'.format(i)))
    dec_log = {}
    dec_log['abstractor'] = (None if abs_dir is None
                             else json.load(open(join(abs_dir, 'meta.json'))))
    dec_log['extractor'] = (None if ext_dir is None
                            else json.load(open(join(ext_dir, 'meta.json'))))
    dec_log['rl'] = False
    dec_log['split'] = split
    dec_log['beam'] = beam_size
    with open(join(save_path, 'log.json'), 'w') as f:
        json.dump(dec_log, f, indent=4)

    # Decoding
    i = 0
    with torch.no_grad():
        for i_debug, raw_article_batch in enumerate(loader):
            tokenized_article_batch = map(tokenize(None), raw_article_batch)
            num_articles = len(raw_article_batch)
            num_ext_sents = 0
            if is_conditional_abs:
                sequential_ext_sents = []
                sequential_article_ids = []
            else:
                ext_arts = []
            ext_inds = []
            for article_i, raw_art_sents in enumerate(tokenized_article_batch):
                ext = extractor(raw_art_sents)
                ext_inds += [(num_ext_sents, len(ext))]
                num_ext_sents += len(ext)

                if is_conditional_abs:
                    # insert place holder to sequential_ext_sents
                    num_selected_sents_excluded_eos = len(ext)
                    if num_selected_sents_excluded_eos > len(sequential_ext_sents):
                        [sequential_ext_sents.append([]) for _ in
                         range(num_selected_sents_excluded_eos - len(sequential_ext_sents))]
                        [sequential_article_ids.append([]) for _ in
                         range(num_selected_sents_excluded_eos - len(sequential_article_ids))]

                    for idx_i, idx in enumerate(ext):
                        sequential_ext_sents[idx_i].append(raw_art_sents[idx])
                        sequential_article_ids[idx_i].append(article_i)
                else:
                    ext_arts += list(map(lambda i: raw_art_sents[i], ext))

            if beam_size > 1:
                if is_conditional_abs:
                    dec_outs = abstractor(sequential_ext_sents, sequential_article_ids, num_articles, beam_size,
                                          diverse)
                else:
                    all_beams = abstractor(ext_arts, beam_size, diverse)  # a list of beam for the whole batch
                    dec_outs = rerank_mp(all_beams, ext_inds, disable_final_rerank)
            else:
                if is_conditional_abs:
                    dec_outs = abstractor(sequential_ext_sents, sequential_article_ids, num_articles)
                else:
                    dec_outs = abstractor(ext_arts)
            assert i == batch_size*i_debug
            for j, n in ext_inds:
                decoded_sents = [' '.join(dec) for dec in dec_outs[j:j+n]]
                for k, dec_str in enumerate(decoded_sents):
                    with open(join(save_path, 'output_{}/{}.dec'.format(k, i)),
                              'w') as f:
                        f.write(make_html_safe(dec_str))

                i += 1
                print('{}/{} ({:.2f}%) decoded in {} seconds\r'.format(
                    i, n_data, i/n_data*100, timedelta(seconds=int(time()-start))
                ), end='')
    print()

_PRUNE = defaultdict(
    lambda: 2,
    {1:5, 2:5, 3:5, 4:5, 5:5, 6:4, 7:3, 8:3}
)

def rerank(all_beams, ext_inds, disable_final_rerank=False):
    final_rerank = not disable_final_rerank
    beam_lists = (all_beams[i: i+n] for i, n in ext_inds if n > 0)
    if final_rerank:
        return list(concat(map(rerank_one, beam_lists)))
    else:
        return list(concat(map(top_one, beam_lists)))

def rerank_mp(all_beams, ext_inds, disable_final_rerank=False):
    final_rerank = not disable_final_rerank
    beam_lists = [all_beams[i: i+n] for i, n in ext_inds if n > 0]
    # a list of beam list, each beam list contains the beam for one article
    with mp.Pool(8) as pool:
        if final_rerank:
            reranked = pool.map(rerank_one, beam_lists)
        else:
            reranked = pool.map(top_one, beam_lists)
    return list(concat(reranked))

def rerank_one(beams):
    @curry
    def process_beam(beam, n):
        for b in beam[:n]:
            b.gram_cnt = Counter(_make_n_gram(b.sequence))
        return beam[:n]
    beams = map(process_beam(n=_PRUNE[len(beams)]), beams)
    best_hyps = max(product(*beams), key=_compute_score)  # a tuple of
    dec_outs = [h.sequence for h in best_hyps]
    return dec_outs

def top_one(beams):
    return [hyps[0].sequence for hyps in beams]

def _make_n_gram(sequence, n=2):
    return (tuple(sequence[i:i+n]) for i in range(len(sequence)-(n-1)))

def _compute_score(hyps):
    all_cnt = reduce(op.iadd, (h.gram_cnt for h in hyps), Counter())
    repeat = sum(c-1 for g, c in all_cnt.items() if c > 1)
    lp = sum(h.logprob for h in hyps) / sum(len(h.sequence) for h in hyps)
    return (-repeat, lp)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=('combine an extractor and an abstractor '
                     'to decode summary and evaluate on the '
                     'CNN/Daily Mail dataset')
    )
    parser.add_argument('--path', required=True, help='path to store/eval')
    parser.add_argument('--abs_dir', help='root of the abstractor model')
    parser.add_argument('--ext_dir', help='root of the extractor model')

    # dataset split
    data = parser.add_mutually_exclusive_group(required=True)
    data.add_argument('--val', action='store_true', help='use validation set')
    data.add_argument('--test', action='store_true', help='use test set')

    # decode options
    parser.add_argument('--batch', type=int, action='store', default=32,
                        help='batch size of faster decoding')
    parser.add_argument('--n_ext', type=int, action='store', default=4,
                        help='number of sents to be extracted')
    parser.add_argument('--max_dec_word', type=int, action='store', default=30,
                        help='maximun words to be decoded for the abstractor')

    parser.add_argument('--no-cuda', action='store_true',
                        help='disable GPU training')
    parser.add_argument('--is_conditional_abs', action='store_true', help='use conditional abstractor')

    parser.add_argument('--beam', type=int, action='store', default=1,
                        help='beam size of abstractor decoding (reranking included)')
    parser.add_argument('--div', type=float, action='store', default=1.0,
                        help='diverse ratio for the diverse beam-search')
    parser.add_argument('--disable_final_rerank', action='store_true',
                        help='disable the final rerank of the beam search output by n-gram repeat')

    args = parser.parse_args()
    args.cuda = torch.cuda.is_available() and not args.no_cuda

    data_split = 'test' if args.test else 'val'
    decode(args.path, args.abs_dir, args.ext_dir,
           data_split, args.batch, args.max_dec_word, args.beam, args.div, args.cuda, args.disable_final_rerank, args.is_conditional_abs)


from bs4 import BeautifulSoup
import requests
import time
import re

# Declaring variables

# Crawl limit = 1000
links_crawl_limit = 1000

# Maxiumum depth limit to crawl
maximum_depth_limit = 5

# Frontier (Queue)
frontier_queue = []

# List of Visited links
links_visit = []

# Main page
page_wiki = "https://en.wikipedia.org"

#Seed link of the assignment
#karen_seed_wiki_link = 'https://en.wikipedia.org/wiki/Karen_Sparck_Jones'
#keyword = 'retrieval'

def main():
    web_crawler(karen_seed_wiki_link)
    link2_file_name = 'Link_Task_2.txt'
    output_file = open(link2_file_name, 'w')
    ind = 1

    # Output file will be created
    for i in links_visit:
        row_var = str(ind) + " " + str(i) + "\n"
        output_file.write(row_var)
        ind += 1
    output_file.close()

# Define a function called web_crawler that will store URL's and will choose the URL should be crawled in future
def web_crawler(seed):
    frontier_queue.append((seed, 1))
    while len(links_visit) != links_crawl_limit and len(frontier_queue) != 0:
        curr = frontier_queue.pop()
        url_info = curr[0]
        if url_info in links_visit:
            continue
        links_visit.append(url_info)
        web_crawler_page(curr)

    while len(frontier_queue) != 0:
        curr = frontier_queue.pop()
        links_visit.append(curr[0])

# Define a function called Web Crawler Page that crawls page of a particular URL and gets the URL's
def web_crawler_page(curr):
    if curr[1] == maximum_depth_limit or len(links_visit) == links_crawl_limit:
        return
    link_info = ""
    url_info = curr[0]

    # Declaring Sleep time politeness policy
    time.sleep(1)
    depth = curr[1]
    doc = requests.get(url_info)
    content_text = BeautifulSoup(doc.text, 'html.parser').find('div', {'id': 'mw-content-text'})

    # Need to maintain and restore the order of links that is obtained
    temp_frontier = []

    if len(content_text.find('ol', class_='references') or ()) > 1:
        content_text.find('ol', class_='references').decompose()

    for link_new in content_text.find_all('a', {'href': re.compile("^/wiki")}):
        if ':' not in link_new.get('href') and len(frontier_queue) + len(links_visit) != links_crawl_limit:
            try:
                link_info = str(link_new.text)
            except UnicodeEncodeError as e:
                pass
            if (keyword.lower() in str(link_new.get('href')).lower()) or (keyword.lower() in link_info.lower()):
                #print(link_new)
                link_new = page_wiki + link_new.get('href')
                link_new = link_new.split('#')
                link_new = link_new[0]
                if not ((link_new in temp_frontier) or (link_new in links_visit)):
                    #print(depth)
                    temp_frontier.append((link_new, depth + 1))

    # Back to original link restoration
    while len(temp_frontier) != 0:
        #print(temp_frontier)
        frontier_queue.append(temp_frontier.pop())

if __name__ == "__main__":
    karen_seed_wiki_link = input("Enter the seed link: ")
    keyword = input("\nPlease enter keyword retrieval: ")
    main()


from django import template
from django.utils import timezone
from postapp.models import Post

register = template.Library()

@register.simple_tag
def get_tags():
    tag_tuple = Post.objects.filter(
        deleted__isnull=True, date_post__lte=timezone.now()).values_list(
        'tags__name', 'tags__slug')
    tag_dct = dict()
    for tag in tag_tuple:
        if tag[1] and '0' not in tag[1]:
            tag_dct.setdefault(tag[1], {'name': tag[0], 'count': 0})
            tag_dct[tag[1]]['count'] += 1
    return tag_dct

@register.simple_tag
def get_calendar():
    post_qs = Post.objects.filter(deleted__isnull=True, date_post__lte=timezone.now()).order_by('-date_post')

    date_dct = dict()
    # date_format = "%d-%B-%Y"
    date_format = "%B %Y"
    date_format_slug = "%Y-%m"
    for post in post_qs:
        date_post = post.date_post.strftime(date_format)
        date_post_slug = post.date_post.strftime(date_format_slug)
        date_dct.setdefault(date_post_slug, {'name': date_post, 'count': 0})
        date_dct[date_post_slug]['count'] += 1
    return date_dct


def calculate_points(x, y, board):
    points = 0

    # sprawdzam poziomo
    if all(element is not 0 for element in board[x]):
        points += 4

    # sprawdzam pionowo
    if all(board[i][y] is not 0 for i in range(0, 4)):
        points += 4

    # sprawdzam ukosy
    isFull = True
    counter = 0
    # sprawdzamy górną lewą przekątną
    if not (x is 0 or y is 0):
        for i, col in zip(range(x, -1, -1), range(y, -1, -1)):
            counter += 1
            if board[i][col] == 0:
                isFull = False
                break

    # sprawdzamy dolną prawą przekątną
    if isFull and not (x is 3 or y is 3):
        if counter is not 0:
            counter -= 1
        for i, column in zip(range(x, 4, 1), range(y, 4, 1)):
            counter += 1
            if board[i][column] == 0:
                isFull = False
                break
    if isFull:
        points += counter
    counter = 0
    isFull = True

    # sprawdzamy górną prawą przekątną
    if not (x is 0 or y is 3):
        for i, col in zip(range(x, -1, -1), range(y, 4, 1)):
            counter += 1
            if board[i][col] == 0:
                isFull = False
                break

    # sprawdzamy dolną lewą przekątną
    if isFull and not (x is 3 or y is 0):
        if counter is not 0:
            counter -= 1
        for i, col in zip(range(x, 4, 1), range(y, -1, -1)):
            counter += 1
            if board[i][col] == 0:
                isFull = False
                break
    if isFull:
        points += counter
    return points

board = [[1,1,1,1],
         [1,0,1,0],
         [1,1,0,1],
         [0,1,1,1]]
print()


from .base import BaseProcessor

from collections import defaultdict
from urllib.parse import quote

import logging
import markdown2
import textwrap
import os
import re

logger = logging.getLogger(__name__)

with_pil = False

try:
    from PIL import Image, UnidentifiedImageError
    with_pil = True
except ImportError:
    pass

from jinja2 import Environment, FileSystemLoader, ChoiceLoader, DictLoader

try:
    from pygments import highlight
    from pygments.styles import get_style_by_name
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter
    with_pygments = True
except ImportError:
    with_pygments = False

try:
    from bs4 import BeautifulSoup as bs
    with_bs = True
except ImportError:
    with_bs = False

class JinjaProcessor(BaseProcessor):

    def highlight_styles(self, code, style_name=None):
        if style_name is None:
            style_name = self.site.config.get('pygments', {}).get('style', 'monokai')
        style = get_style_by_name(style_name)
        return '<style type="text/css">{}</style>'.format(HtmlFormatter(style=style).get_style_defs('.highlight .{}'.format(style_name)))

    def highlight(self, code, language='python', style_name=None, strip=True, deindent=True):
        if style_name is None:
            style_name = self.site.config.get('pygments', {}).get('style', 'monokai')
        lexer = get_lexer_by_name(language)
        style = get_style_by_name(style_name)
        if deindent:
            code = textwrap.dedent(code)
        if strip:
            code = code.strip()
        return highlight(code, lexer, HtmlFormatter(style=style, cssclass='{}'.format(style_name)))

    def picture(self, filename, prefs=['webp', 'png', 'jpeg'], sizes='50vw', **kwargs):
        file_path = self.file(filename)
        file_dir = os.path.dirname(file_path)
        if not file_path:
            return ""
        source_path = self.source_path(filename)
        filename = os.path.basename(source_path)
        source_dir = os.path.dirname(source_path)
        basename, zoom, ext = re.match(r"^(.*?)(?:@(\d+x))?\.(.*)$", filename).groups()
        extra_args = []
        for k, v in kwargs.items():
            extra_args.append(f'{k}="{v}"')
        extra_args = " ".join(extra_args)
        files = os.listdir(source_dir)
        alternatives = defaultdict(list)
        for fname in files:
            if re.match("{}(?:@\d+x)?\.\w+$".format(re.escape(basename)), fname):
                _, zoom, ext = re.match(r"^(.*?)(?:@(\d+x))?\.(.*)$", fname).groups()
                if zoom is None:
                    zoom = '1x'
                if with_pil:
                    try:
                        # we determine the exact image width to include
                        image = Image.open(os.path.join(source_dir, fname))
                        zoom = '{}w'.format(image.size[0])
                    except UnidentifiedImageError:
                        # we cannot properly read this image, ignore it
                        logger.warning(f"Cannot read image {source_dir}/{fname}, ignoring its size...")
                        pass
                alternatives[ext].append((fname, zoom))

        html_alternatives = ""

        sizestr = ""
        if sizes:
            sizestr = f'sizes="{sizes}"'

        def generate_alternative(ext, ext_alternatives):

            alts = []
            for f, z in ext_alternatives:
                fp = quote(f"{file_dir}/{f}")
                alts.append(f"{fp} {z}")
            joined_alternatives = ", ".join(alts)
            return f"""
    <source type="image/{ext}" srcset="{joined_alternatives}" {sizestr}>
"""

        # we add the preferred formats first
        for pref in prefs:
            if pref in alternatives:
                html_alternatives += generate_alternative(pref, alternatives[pref])
        # then we add the rest
        for ext, ext_alternatives in alternatives.items():
            if not ext in prefs:
                html_alternatives += generate_alternative(ext, ext_alternatives)
        return f"""
<picture>
    {html_alternatives}
    <img src="{file_path}" {extra_args}>
</picture>
"""

    def markdown(self, text):
        result = markdown2.markdown(text, extras=['footnotes', 'fenced-code-blocks'])
        return result

    def translate(self, key, *args, **kwargs):
        result = super().translate(key, *args, **kwargs)
        translate_hint = self.site.config.get('translate-hint')
        if translate_hint:
            return translate_hint.format(value=result, key=key, args=args, kwargs=kwargs)
        return result

    def jinja(self, vars, input, **kwargs):
        context = vars.copy()
        context.update(kwargs)
        return self.process(input, context)

    def get_jinja_env(self, input, vars):
        dict_loader = DictLoader({'input' : input})
        choice_loader = ChoiceLoader([dict_loader, FileSystemLoader('{}/templates'.format(self.site.src_path)), FileSystemLoader(self.site.src_path)])
        env = Environment(loader=choice_loader)

        # we add some useful filters
        env.filters['href'] = self.href
        env.filters['full_href'] = self.full_href
        env.filters['file'] = self.file
        env.filters['markdown'] = self.markdown
        env.filters['picture'] = self.picture
        env.filters['jinja'] = lambda input, **kwargs: self.jinja(vars, input, **kwargs)
        if with_pygments:
            env.filters['highlight'] = self.highlight
            env.filters['highlight_styles'] = self.highlight_styles
        env.filters['translate'] = self.translate
        for filters in self.site.addons['jinja-filters']:
            for name, f in filters:
                env.filters[name] = f
        return env

    def process(self, input, vars):
        env = self.get_jinja_env(input, vars)
        template = env.get_template('input')
        result = template.render(**vars)
        if with_bs and False:
            soup=bs(result, "html.parser")
            return soup.prettify()
        return result


from lxml import html
import requests, time, zhihu_oauth

start_time = time.time()  # 初始时间戳

# ========================登录========================
from zhihu_oauth import ZhihuClient

save_file_path = '/Users/alicewish/我的坚果云/找出任意用户粉丝中的轮子粉.txt'

client = ZhihuClient()
client.load_token('/Users/alicewish/我的坚果云/token.pkl')

# ========================我========================
me = client.me()
pid = "wang-sheng-70-88"

people = client.people(pid)
# ==================查询我的粉丝==================
followers_line = []
i = 0
n = 11
for follower in people.followers:
    entry_start_time = time.time()
    i = i + 1
    if i > n:
        # 查询可用信息
        gender = follower.gender
        id = follower.id
        name = follower.name
        sina_weibo_name = follower.sina_weibo_name
        sina_weibo_url = follower.sina_weibo_url
        uid = follower.uid
        # 整理
        follower_info = [name, str(id), str(uid), str(gender), str(sina_weibo_name), str(sina_weibo_url)]
        info_line = '\t'.join(follower_info)
        print(info_line)
        # ==================查询是否关注了轮子vczh==================
        followings = follower.followings
        status = False
        for following in followings:
            # 查询可用信息
            id = following.id
            name = following.name
            uid = following.uid
            print(name)
            if name == "vczh":
                status = True
        if status:
            print("找到一个!")
            followers_line.append(info_line)
            f = open(save_file_path, 'a')
            append_line = str(i) + info_line + "\r\n"
            try:
                f.write(append_line)
            finally:
                f.close()
    # ================每项时间计时================
    entry_run_time = time.time() - entry_start_time
    entry_print = "耗时:{:.4f}秒".format(entry_run_time)
    print(entry_print)
# 合并数据
text = '\r\n'.join(followers_line)
print(text)
# ================写入剪贴板================
import pyperclip

pyperclip.copy(text)
spam = pyperclip.paste()

# ================运行时间计时================
run_time = time.time() - start_time
if run_time < 60:  # 两位小数的秒
    print("耗时:{:.2f}秒".format(run_time))
elif run_time < 3600:  # 分秒取整
    print("耗时:{:.0f}分{:.0f}秒".format(run_time // 60, run_time % 60))
else:  # 时分秒取整
    print("耗时:{:.0f}时{:.0f}分{:.0f}秒".format(run_time // 3600, run_time % 3600 // 60, run_time % 60))

知乎-找出任意用户粉丝中的轮子粉.py

知乎-找出任意用户粉丝中的轮子粉.py


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 22 11:49:41 2019

@author: admin
"""
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

bc = load_breast_cancer()
X, y = bc.data, bc.target
num_negetive, num_positive = np.bincount(y)
print('data shape: {0}, num. positive: {1}; num. negetive: {2}'.format(X.shape,
                                            num_positive, num_negetive))

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
lgr = LogisticRegression()
lgr.fit(X_train, y_train)
train_score = lgr.score(X_train, y_train)
test_score = lgr.score(X_test, y_test)
print('train score: {0:.6f}; test score: {1:.6f}'.format(train_score, test_score))


'''
Basic Picture Viewer
====================

This simple image browser demonstrates the scatter widget. You should
see three framed photographs on a background. You can click and drag
the photos around, or multi-touch to drop a red dot to scale and rotate the
photos.

The photos are loaded from the local images directory, while the background
picture is from the data shipped with kivy in kivy/data/images/background.jpg.
The file kivy_multi.kv describes the interface and the file shadow32.png is
the border to make the images look like framed photographs. Finally,
the file android.txt is used to package the application for use with the
Kivy Launcher Android application.

For Android devices, you can copy/paste this directory into
/sdcard/kivy/kivy_multi on your Android device.

The images in the image directory are from the Internet Archive,
`https://archive.org/details/PublicDomainImages`, and are in the public
domain.

'''

import kivy
kivy.require('1.0.6')

from glob import glob
from random import randint
from os.path import join, dirname
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.scatter import Scatter
from kivy.properties import StringProperty
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.graphics import *

from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2, time
import numpy as np
from scratch.kivy_multi.videostream import VideoStream, example_VideoCapture
from FLIR_pubsub.FLIR_videocapture import FlirCapture
from FLIR_pubsub.FLIR_client_utils import *

import multiprocessing
from multiprocessing import Process, freeze_support, Lock, current_process, Value, Array
from scratch.kivy_multi.flir_cam_controller \
    import NP_SharedMemory, fake_camera_process, flir_camera_process, SpinProcess
import timeit

class Picture(Scatter):
    '''Picture is the class that will show the image with a white border and a
    shadow. They are nothing here because almost everything is inside the
    picture.kv. Check the rule named <Picture> inside the file, and you'll see
    how the Picture() is really constructed and used.

    The source property will be the filename to show.
    '''

    source = StringProperty(None)

class PicturesApp(App):

    def build(self):
        self.vs1 = VideoStream(src="rtsp://192.168.183.242:554", verbose=True).start()
        # self.vs2 = VideoStream(src="rtsp://192.168.183.242:554", verbose=True).start()
        # self.vs3 = VideoStream(src="rtsp://192.168.183.242:554", verbose=True).start()
        self.vs4 = VideoStream(src="test", name='test', videocapture=example_VideoCapture, verbose=True).start()
        # self.vcap = cv2.VideoCapture("rtsp://192.168.183.242:554")
        self.i = 0

        name = 'FrontLeft'
        url = 'localhost'

        # # shared memory
        self.sm_flir = NP_SharedMemory((2000, 3000, 3), name='camera_1')
        self.sm_flir.set_local_np_view()
        self.sm_fake = NP_SharedMemory((2000, 3000, 3), name='camera_1')
        self.sm_fake.set_local_np_view()
        # sm2 = NP_SharedMemory((2000, 3000, 3), name='camera_2')
        # sm2.set_local_np_view()

        self.proc_fake = SpinProcess(target=fake_camera_process, sharemem=self.sm_fake)
        self.proc_flir = SpinProcess(target=flir_camera_process, sharemem=self.sm_flir)
        # p2 = Process(target=demo_camera_process, args=(sm2,)).start()

        # the root is created in kivy_multi.kv
        root = self.root
        # self.img1 = Image()
        # root.add_widget(self.img1)

        # get any files into images directory
        curdir = dirname(__file__)
        self.stream_widget_list = []
        for filename in glob(join(curdir, 'images', '*')):
            try:
                # load the image
                picture = Picture(source=filename, rotation=randint(-30, 30))
                # add to the main field
                root.add_widget(picture)
                self.stream_widget_list.append(picture)
            except Exception as e:
                Logger.exception('Pictures: Unable to load <%s>' % filename)

        Clock.schedule_interval(self.update_kivy, 1.0 / 30.0)
        # Clock.schedule_interval(self.wastetime, 1.0 / 100.0)

    def wastetime(self, dt):
        # time.sleep(0.01)
        start = time.monotonic()
        while time.monotonic() - start < 0.1:
            pass

    def on_pause(self):
        return True

    def stream2widget(self, vs, _widgit, bg=None):
        _buf = vs.read()
        # time.sleep(1)

        if _buf is None:
            buf = None
            pass
        elif isinstance(_buf, NP_SharedMemory):
            shape = (_buf._height.value, _buf._width.value, 3)
            buf = _buf.arr[:shape[0]*shape[1]*shape[2]]
            buf.shape = shape

        else:
            buf = _buf

        if isinstance(buf, np.ndarray) and buf.size > 100000:
            # if hasattr(buf, 'size') and buf.size > 100000:
            # convert it to texture
            # all ready flattened
            #
            if 1:
                # start = timeit.timeit()
                if len(buf.shape) == 3:
                    texture1 = Texture.create(size=(buf.shape[1], buf.shape[0]), colorfmt='bgr')
                    buf = cv2.flip(buf, 0).flatten()
                    # buf = buf.flatten()
                elif len(buf.shape) == 1:
                    texture1 = Texture.create(size=(3000, 2000), colorfmt='bgr')
                else:
                    raise RuntimeError(f'Received frame shape should be 1 or 3, got: {buf.shape}')

               # end1 = timeit.timeit()
                texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
                # end2 = timeit.timeit()

                # end2 = timeit.timeit()
                # display image from the texture
                if bg is not None:
                   bg.texture = texture1
                _widgit.children[0].texture = texture1
                # end = timeit.timeit()
                # print('Texture: ', end - start)
            else:
                _widgit.children[0].texture.blit_buffer(buf,
                                                        size=(frame.shape[1], frame.shape[0]),
                                                        colorfmt='bgr', bufferfmt='ubyte')

            # widget.canvas
            # with _widgit.children[0].canvas:  todo jn get this going
            #     Rectangle(texture=_widgit.children[0].texture, pos=_widgit.children[0].pos, size=(64, 64))

    def update_kivy(self, dt):
        '''display image from cam in kivy window'''
        self.stream2widget(self.vs1, self.stream_widget_list[0])
        self.i += 1
        if self.i % 5 == 0:
            # bodge to slow these down
            # self.stream2widget(self.vs2, self.stream_widget_list[1])
            # self.stream2widget(self.vs3, self.stream_widget_list[2])
            # self.stream2widget(self.vs4, self.stream_widget_list[3])
            self.stream2widget(self.proc_fake, self.stream_widget_list[3])
            self.stream2widget(self.proc_flir, self.stream_widget_list[4])
            pass

    def on_request_close(self, *args):
        self.textpopup(title='Exit', text='Are you sure?')
        return True

    def on_stop(self, *args):
        print('!!!! Running on_stop')
        self.proc_flir.release()
        self.proc_fake.release()
        self.vs1.release()
        self.vs4.release()
        return True

    def textpopup(self, title='', text=''):
        """Open the pop-up with the name.

        :param title: title of the pop-up to open
        :type title: str
        :param text: main text of the pop-up to open
        :type text: str
        :rtype: None
        """
        box = BoxLayout(orientation='vertical')
        box.add_widget(Label(text=text))
        mybutton = Button(text='OK', size_hint=(1, 0.25))
        box.add_widget(mybutton)
        popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
        mybutton.bind(on_release=self.stop)
        popup.open()

import cv2, imutils
import os, sys, logging

if __name__ == '__main__':

    multiprocessing.log_to_stderr()
    logger = multiprocessing.get_logger()
    logger.setLevel(logging.INFO)

    app = PicturesApp().run()

    print('Main: End of program')

    # time.sleep(2)
    print('Main: join')
    # p1.join()
    # p2.join()
    print('Main: bye')
    cv2.destroyAllWindows()


from configparser import ConfigParser
from os import environ
from pathlib import Path
from threading import Lock

STATE_FILE_DIR = "/var/lib" if environ.get("TESTING", "") != "1" else "/tmp"
STATE_FILE_PATH = f"{STATE_FILE_DIR}/pt-os-web-portal/state.cfg"
config_parser = ConfigParser()

path = Path(STATE_FILE_PATH)
lock = Lock()

if not path.exists():
    Path(path.parent).mkdir(parents=True, exist_ok=True)
    path.touch()

config_parser.read(STATE_FILE_PATH)

def get(section: str, key: str, fallback=None):
    with lock:
        val = fallback
        try:
            val = config_parser.get(section, key)
        except Exception:
            if fallback is None:
                raise
        finally:
            return val

def set(section: str, key: str, value):
    with lock:
        try:
            if not config_parser.has_section(section):
                config_parser.add_section(section)
            config_parser.set(section, key, value)
        except Exception:
            raise
        __save()

def remove(section: str, key: str = ""):
    with lock:
        try:
            if key and config_parser.has_option(section, key):
                config_parser.remove_option(section, key)
            elif len(key) == 0 and config_parser.has_section(section):
                config_parser.remove_section(section)
        except Exception:
            raise
        __save()

def __save():
    with open(STATE_FILE_PATH, "w") as f:
        config_parser.write(f)