#! /Users/rkrsn/anaconda/bin/python
from __future__ import print_function
from __future__ import division
from os import environ, getcwd
from pdb import set_trace
from random import uniform, randint
import sys

# Update PYTHONPATH
HOME = environ['HOME']
axe = HOME + '/git/axe/axe/'  # AXE
pystat = HOME + '/git/pystat/'  # PySTAT
cwd = getcwd()  # Current Directory
sys.path.extend([axe, pystat, cwd])

from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier

from Prediction import *
from _imports import *
from abcd import _Abcd
from cliffsDelta import *
# from contrastset import *
# from dectree import *
from hist import *
from smote import *
import makeAmodel as mam
from methods1 import *
import numpy as np
import pandas as pd
import sk

class deltas():

  def __init__(self, row, myTree):
    self.row = row
    self.loc = drop(row, myTree)
    self.contrastSet = None
    self.newRow = row
    self.score = self.scorer(self.loc)

  def scorer(self, node):
    return mean([r.cells[-2] for r in node.rows])

  def createNew(self, stuff, keys, N=1):
    newElem = []
    tmpRow = self.row
    prob = [2 ** -d for d in xrange(1, len(stuff) + 1)]
    for _ in xrange(N):
      for ss, p in zip(stuff, prob):
        lo, hi = ss[1]
        pos = keys[ss[0].name]
        # if uniform(0,1)>0.5 else tmpRow.cells[pos]
        tmpRow.cells[pos] = int(lo)
      newElem.append(tmpRow)
    return newElem

  def patches(self, keys, N_Patches=10):
    # Search for the best possible contrast set and apply it
    isles = []
    newRow = self.row
    for stuff in self.contrastSet:
      isles.append(self.createNew(stuff, keys, N=N_Patches))
    return isles

class store():

  def __init__(self, node):
    self.node = node
    self.dist = 0
    self.DoC = 0
    self.score = self.scorer(node)

  def scorer(self, node):
    return mean([r.cells[-2] for r in node.rows])

class treatments():

  "Treatments"

  def __init__(self, train=None, test=None, test_DF=None,
               verbose=True, smoteit=True, bin=False):
    self.train, self.test = train, test
    self.train_DF = createTbl(train, _smote=smoteit, isBin=False)
    if not test_DF:
      self.test_DF = createTbl(test, isBin=False)
    else:
      self.test_DF = test_DF
    self.verbose, self.smoteit = verbose, smoteit
    self.mod, self.keys = [], self.getKey()

  def flatten(self, x):
    """
    Takes an N times nested list of list like [[a,b],[c, [d, e]],[f]]
    and returns a single list [a,b,c,d,e,f]
    """
    result = []
    for el in x:
      if hasattr(el, "__iter__") and not isinstance(el, basestring):
        result.extend(self.flatten(el))
      else:
        result.append(el)
    return result

  def leaves(self, node):
    """
    Returns all terminal nodes.
    """
    L = []
    if len(node.kids) > 1:
      for l in node.kids:
        L.extend(self.leaves(l))
      return L
    elif len(node.kids) == 1:
      return [node.kids]
    else:
      return [node]

  def scorer(self, node):
    """
    Score an leaf node
    """
    return mean([r.cells[-2] for r in node.rows])

  def isBetter(self, me, others):
    """
    Compare [me] with a bunch of [others,...], return the best person
    """
    for notme in others:
      #       if '%.2f' % self.scorer(notme) == 0:
      if self.scorer(notme) < self.scorer(me):
        return True, notme.branch
      else:
        return False, []

  def attributes(self, nodes):
    """
    A method to handle unique branch variables that characterizes
    a bunch of nodes.
    """
    xx = []
    attr = []

    def seen(x):
      xx.append(x)
    for node in nodes:
      if not node.node.branch in xx:
        attr.append(node.node.branch)
        seen(node.node.branch)
    return attr

  def finder2(self, node, alpha=0.01, pos='far'):
    """
    finder2 is a more elegant version of finder that performs a search on
    the entire tree to find leaves which are better than a certain 'node'
    """

    euclidDist = lambda a, b: ((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) ** 0.5
    midDist = lambda a, b: abs(sum(b) - sum(a)) / 2
    vals = []
    current = store(node)  # Store current sample
    while node.lvl > -1:
      node = node.up  # Move to tree root

    # Get all the terminal nodes
    leaves = self.flatten([self.leaves(_k) for _k in node.kids])

    for leaf in leaves:
      l = store(leaf)
      for b in leaf.branch:
        dist = []
        if b[0] in [bb[0] for bb in current.node.branch]:
          l.DoC += 1
          dist.extend([midDist(b[1], bb[1])
                       for bb in current.node.branch if b[0] == bb[0]])
      l.dist = np.sqrt(np.sum(dist))
      vals.append(l)
    vals = sorted(vals, key=lambda F: F.DoC, reverse=False)
    best = [v for v in vals if v.score < alpha * current.score]
    if not len(best) > 0:
      best = vals

    # Get a list of DoCs (DoC -> (D)epth (o)f (C)orrespondence, btw..)
    # set_trace()
    attr = {}
    bests = {}
    unq = sorted(list(set([v.DoC for v in best])))  # A list of all DoCs..
    for dd in unq:
      bests.update(
          {dd: sorted([v for v in best if v.DoC == dd], key=lambda F: F.dist)})
      attr.update({dd: self.attributes(
          sorted([v for v in best if v.DoC == dd], key=lambda F: F.score))})

    if pos == 'near':
      return attr[unq[-1]][0]
    elif pos == 'far':
      return attr[unq[0]][-1]

  def getKey(self):
    keys = {}
    for i in xrange(len(self.test_DF.headers)):
      keys.update({self.test_DF.headers[i].name[1:]: i})
    return keys

  def main(self):
    # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Main
    # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    # Decision Tree
    t = discreteNums(self.train_DF, map(lambda x: x.cells, self.train_DF._rows))
    myTree = tdiv(t)
    # showTdiv(myTree)
    # set_trace()
    # Testing data
    testCase = self.test_DF._rows
    for tC in testCase:
      newRow = tC
      node = deltas(newRow, myTree)  # A delta instance for the rows

      if newRow.cells[-2] == 0:
        node.contrastSet = []
        self.mod.append(node.newRow)
      else:
        node.contrastSet = [self.finder2(node.loc, pos='near')]
        # set_trace()
        # Now generate 1 potential patch
        patch = node.patches(self.keys, N_Patches=1)

        p = patch.pop()
        tmpTbl = clone(self.test_DF,
                       rows=[k.cells for k in p],
                       discrete=True)
        self.mod.append(choice(tmpTbl._rows))

      # <<<<<<<<<<< Debug >>>>>>>>>>>>>>>
        # set_trace()
    return clone(self.test_DF, rows=[k.cells for k in self.mod], discrete=True)

def planningTest():
  # Test contrast sets
  n = 0
  Dir = 'Data/'
  one, two = explore(Dir)
  # Training data
  _ = treatments(train=one[n],
                 test=two[n],
                 verbose=True,
                 smoteit=False).main()

  # <<<<<<<<<<< Debug >>>>>>>>>>>>>>>
  set_trace()

if __name__ == '__main__':
  planningTest()


#!/usr/bin/env python

from Cython.Build import cythonize
# from distutils.core import setup
from setuptools import setup, find_packages, Extension

extensions = [
    Extension(
        "dataprovider.augmentation.warping._warping",
        sources=['dataprovider/augmentation/warping/*.pyx']
    ),
]

setup(
    name='dataprovider',
    version='0.0.1',
    description='Deep learning platform-independent volumetric data provider for 3D neural nets',
    author='Kisuk Lee',
    author_email='kisuklee@mit.edu',
    url='https://github.com/torms3/DataProvider',
    # packages=find_packages()
    packages=['dataprovider',
              'dataprovider.inference',
              'dataprovider.augmentation',
              'dataprovider.augmentation.warping'],
    ext_modules = cythonize(extensions)
)


import matplotlib.pyplot as plt
import numpy as np
import timeit

from PyMieSim.Plots import PlotField
from PyMieSim.LMT.python.Sphere import Fields as PyField
from PyMieSim.Source import PlaneWave
from PyMieSim.LMT.Scatterer import SPHERE as SPHERELMT
from PyMieSim.GLMT.Scatterer import SPHERE as SPHEREGLMT

def Speed(setup):
    BenchPython = """PyField(**argsPy)"""

    GLMTClass = """B.sFields(Phi=Phi, Theta=Theta, R=1. );"""

    LMTClass = """A.sFields(Phi=Phi, Theta=Theta, R=1. ); """

    Bench = timeit.timeit(setup = setup,stmt = BenchPython, number = 1)
    print('\nLMT PYTHON BENCHMARK: ', Bench)

    Bench = timeit.timeit(setup = setup,stmt = LMTClass, number = 1)
    print('\n\n'+'='*50 + '\n\n LMT C++ class BENCHMARK', Bench)

    Bench = timeit.timeit(setup = setup,stmt = GLMTClass, number = 1)
    print('\n\n'+'='*50 + '\n\n GLMT C++ BENCHMARK', Bench)

def Correctness():
    Phi = np.linspace(-np.pi/2,np.pi/2,100);
    Theta = np.linspace(-np.pi,np.pi,100)

    THETA, PHI = np.meshgrid(Theta, Phi)

    beam = PlaneWave(Wavelength=1e-6)
    BSC = beam.GetBSC(MaxOrder=30)

    argsPy = {"Index":        1.4,
              "Diameter":     10e-6,
              "Wavelength":   1e-6,
              "nMedium":      1.0,
              "Polarization": 0.0,
              "E0":           1.0,
              "R":            1.0,
              "Phi":          Phi,
              "Theta":        Theta}

    argsLMT = {"Index":        1.4,
               "Diameter":     10e-6,
               "Wavelength":   1e-6,
               "nMedium":      1.0,
               "Polarization": 0.0,
               "E0":           1.0}

    argsGLMT = {"Index":        1.4,
                "Diameter":     10e-6,
                "Wavelength":   1e-6,
                "nMedium":      1.0,
                "Polarization": 0.0,
                "E0":           1.0,
                "BSC":          beam._BSC_}

    PyEPhi, PyETheta = PyField(**argsPy);

    CppEPhi, CppETheta = SPHERELMT(**argsLMT).sFields(Phi=Phi, Theta=Theta, R=1. );

    GLMTCppEPhi, GLMTCppETheta = SPHEREGLMT(**argsGLMT).sFields(Phi=Phi, Theta=Theta, R=1. );

    PlotField(Theta, Phi, CppEPhi, CppETheta)

    PlotField(Theta, Phi, GLMTCppEPhi, GLMTCppETheta)

    #PlotField(Theta, Phi, PyEPhi, PyETheta)

    plt.show()

setup = """
import numpy as np
from PyMieSim.LMT.python.Sphere import Fields as PyField

from PyMieSim.Source import PlaneWave
from PyMieSim.LMT.Scatterer import SPHERE as SPHERELMT
from PyMieSim.GLMT.Scatterer import SPHERE as SPHEREGLMT

beam = PlaneWave(Wavelength=1e-6)
BSC = beam.GetBSC(MaxOrder=30)
Phi = np.linspace(-np.pi/2,np.pi/2,800); Theta = np.linspace(-np.pi,np.pi,800)

argsPy = {"Index":        1.4,
          "Diameter":     10e-6,
          "Wavelength":   1e-6,
          "nMedium":      1.0,
          "Polarization": 0.0,
          "E0":           1.0,
          "R":            1.0,
          "Phi":          Phi,
          "Theta":        Theta}

argsLMT = {"Index":        1.4,
           "Diameter":     10e-6,
           "Wavelength":   1e-6,
           "nMedium":      1.0,
           "Polarization": 0.0,
           "E0":           1.0}

argsGLMT = {"Index":        1.4,
            "Diameter":     10e-6,
            "Wavelength":   1e-6,
            "nMedium":      1.0,
            "Polarization": 0.0,
            "E0":           1.0,
            "BSC":          beam._BSC_}

A=SPHERELMT(**argsLMT);
B=SPHEREGLMT(**argsGLMT);

"""

if __name__=='__main__':
    Speed(setup)
    Correctness()

    # -


import csv
import pandas
'''
This scrpit uses the adjacent suburbs csv to create a list of unquie suburbs in
Brisbane. This will be saved as a seperate csv which will be used to creae the main
reference table of suburds in the database.
'''

# Create a list containing the column names from the csv file.
colnames = ['SUBURB', 'Adjoining_SUBURB']
# read csv file using pandas module.
data = pandas.read_csv('original_data_files/adjacentsuburbs20171108.csv', names=colnames)

# create list from cotaining everything is the 'SUBURB' column, including header.
suburb = data.SUBURB.tolist()

print(suburb[0:6])

# slice list to remove the header row.
suburb = suburb[1:]

print(suburb[0:6])

# the following line demonstrates creating a set. The following code also does this and
# creates a turns it into a list data type in one go.
print(type(set(suburb)))

# Use 'set' function to remove duplicates, and the 'sorted' function to
# get alphabetical order, this also turns the variable back into a list.
suburb_set = sorted(set(suburb))

#suburb_set = set(suburb)

print(type(suburb_set))
print(suburb_set)

suburb_list = list(suburb_set)

with open('new_data_files/suburbs.csv', 'w') as csvfile:
    wr = csv.writer(csvfile,  )
    wr.writerow(['suburb '])
    for item in suburb_list:
        wr.writerow([item])

csvfile.close()


# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from Bio.Blast import NCBIXML
import subprocess, shlex, os
from pprint import pprint
from inspect import getmembers

def index(request):
    template = loader.get_template('argannot/index.html')
    return HttpResponse(template.render({}, request))
def result(request):
    print(request.method)
    template = loader.get_template('argannot/result.html')
    # Appel du traitement biopython
    seq = request.GET['seq']
    if(len(seq) == 0):
        error = {}
        error["error"] = "Séquence vide"
        return HttpResponse(template.render(error,request))
    with open("query.fasta", "w") as query_file:
        query_file.write(seq)
        query_file.close()
        blastp = "blastp -query query.fasta -db argannot/argannot -out result.xml -outfmt 5"
        args = shlex.split(blastp)
        blast = {}
        with subprocess.Popen(args, stderr=subprocess.PIPE) as blast_process:
            out, err = blast_process.communicate()
            if err:
                print(err)
            result = open("result.xml","r")
            blast_record = NCBIXML.read(result)
            pprint(getmembers(blast_record))
            blast['result'] = blast_record
            return HttpResponse(template.render(blast, request))
# Create your views here.


import pytest

from datasciencebox.core.cloud import Cluster, Instance, BareInstance, AWSInstance
from datasciencebox.core.dsbfile import DSBFile

dsbfile = DSBFile()

def test_instance_bare():
    ins = Instance.new(dsbfile)
    assert type(ins) == BareInstance

    ins = Instance.new(dsbfile, info={'id': '123', 'ip': '1.2.3.4'})
    assert ins.ip == '1.2.3.4'
    assert ins.uid == '123'

def test_instance_aws():
    dsbfile = DSBFile()
    dsbfile['cloud'] = 'aws'

    ins = Instance.new(dsbfile)
    assert type(ins) == AWSInstance

def test_cluster_from_to_list():
    data = [{'id': 0, 'ip': '0.0.0.0'}, {'id': 1, 'ip': '1.1.1.1'}, {'id': 2, 'ip': '2.2.2.2'}]
    cluster = Cluster.from_list(data, dsbfile)

    exported = cluster.to_list()
    assert type(exported) == list
    assert data == exported
    assert len(cluster.instances) == 3


# šahovnica
# GUI: tukaj se riše in zaznava klike, za pravila povprašamo logiko

import tkinter as tk
import logika

def narisi_sahovnico(platno, velikost_polj, odmik):
    '''Nariše šahovnico 8d X 8d. Desno spodaj je belo polje.'''
    x1, y1 = odmik, odmik # določimo odmik
    for i in range(8): # vrstice
        for j in range(8): # stolpci
            barva = "white" if (i + j) % 2 == 0 else "gray"
            platno.create_rectangle(x1, y1, x1 + velikost_polj, y1 + velikost_polj, fill=barva)
            x1 += velikost_polj # naslednji kvadratek v vrsti
        x1, y1 = odmik, odmik + velikost_polj * (i + 1) # premaknemo se eno vrstico navzdol

class Sahovnica:
    print("RAZRED ŠAHOVNICA")

    def __init__(self, master):
        # nastavitve velikosti
        self.velikost_polj = 110
        self.odmik = 80
        self.platno = tk.Canvas(master, width=self.velikost_polj * 10, height=self.velikost_polj * 10)
        self.platno.pack()

        self.sah = logika.Šah()
        self.IGRA = self.sah.IGRA

        self.oznacena_figura = None

        # narišemo šahovnico
        narisi_sahovnico(self.platno, self.velikost_polj, self.odmik)

        # registriramo se za klike z miško
        self.platno.bind('<Button-1>', self.klik)

        # naredimo oznako za izpisovanje

        okvir_oznake = tk.LabelFrame(self.platno)
        okvir_oznake.pack()
        self.izpis_potez = tk.StringVar(value='klikni nekam')
        oznaka_izpis_potez = tk.Label(okvir_oznake, textvariable=self.izpis_potez)
        oznaka_izpis_potez.pack()
        x, y = self.odmik + 8 * self.velikost_polj / 2, self.odmik / 2
        self.platno.create_window(x, y, window=okvir_oznake, width = 140)
        # self.platno.create_text(600, 20, text=self.izpis_potez.get()) ZAKAJ SE TO NE SPREMINJA?

        self.zacni_igro()

    def klik(self, event):
        '''Zazna klik in to sporoči logiki igre.'''

        # najprej pridobi koordinate
        i = int((event.y - self.odmik) // self.velikost_polj) # vrstica
        j = int((event.x - self.odmik) // self.velikost_polj) # stolpec
        # stolpci = 'ABCDEFGH'
        # self.izpis_potez.set('Kliknil si na {}{}.'.format(stolpci[j], 7 - i + 1))

        if not(0 <= i <= 7 and 0 <= j <= 7): # klik izven šahovnice
            return

        if self.sah.konec_igre:
            return

        if self.oznacena_figura is None:
            # jo označimo
            if self.sah.lahko_oznacimo(i, j):
                self.oznacena_figura = self.IGRA[i][j]
                self.sah.oznacena_figura = self.oznacena_figura # prenesemo informacijo
                # sedaj tudi pobarvamo polje z označeno figuro
                self.oznaceno_polje = self.platno.find_overlapping(event.x, event.y, event.x + 1, event.y + 1)[0]
                self.barva_oznacenega_polja = self.platno.itemcget(self.oznaceno_polje, "fill")
                self.platno.itemconfig(self.oznaceno_polje, fill="blue")
                self.oznacena_figura = self.IGRA[i][j]
                # povemo logiki, katero figuro smo označili; logika vrne možne poteze in pokliče metodo za barvanje kvadratkov

        else:
            # ponastavimo barvo polja :)
            self.platno.itemconfig(self.oznaceno_polje, fill=self.barva_oznacenega_polja) # lahko bi tudi z (i + j) % 2, ko bi za i in j vprašal označeno figuro
            if self.sah.je_poteza_veljavna(i, j):
                # sporočimo logiki, da se je nekaj spremenilo
                self.sah.premakni_figuro(i, j)
                self.izpis_potez.set('Na vrsti je {}.'.format(self.sah.na_potezi))

                # premaknemo sliko figure na novi koordinati
                x = self.odmik + (j * self.velikost_polj) + self.velikost_polj / 2 # centriramo klik
                y = self.odmik + (i * self.velikost_polj) + self.velikost_polj / 2
                # če je na polju kakšna druga figura, jo 'pojemo'
                if self.IGRA[i][j] is not None:
                    nasprotna_figura = self.IGRA[i][j]
                    id_slike = nasprotna_figura.id_slike
                    self.platno.delete(id_slike)
                # narišemo novo sliko in shranimo nov id_slike
                foto = self.oznacena_figura.foto
                id_slike = self.platno.create_image(x, y, image=foto)
                self.oznacena_figura.id_slike = id_slike

            # odznačimo figuro
            self.oznacena_figura = None

            print('nova poteza')
            for i in range(8):
                print(self.IGRA[i])

    def zacni_igro(self):
        '''Prične igro.'''
        self.prikaz_figur()
        # nastavi odštevalnik ure

    def prikaz_figur(self):
        '''Na šahovnici prikaže figure.'''
        for i in range(8):
            for j in range(8):
                figura = self.IGRA[i][j]
                if figura is not None:
                    i, j = figura.polozaj # rišemo lahko direktno iz položaja figur, lahko bi tudi iz položaja v matriki
                    foto = figura.foto
                    x = self.odmik + (j * self.velikost_polj) + self.velikost_polj / 2
                    y = self.odmik + (i * self.velikost_polj) + self.velikost_polj / 2
                    id_slike = self.platno.create_image(x, y, image=foto)
                    figura.id_slike = id_slike

root = tk.Tk()

partija_saha = Sahovnica(root)

root.mainloop()

#===========================================================#
#                  NASVETI PROFESORJA                       #
#===========================================================#
# matrika je zaradi rekonstrukcije, zato da programer vidi, kaj se dogaja
# IGRA v logiki
# najprej spremeniš v logiki, nato sporočiš GUI, da nariše drugam
# class Figura: x, y, barva -> kar je skupno vsem figuram
# class Kmet(Figura): mozne poteze, slika


import math
real_stats = [0.08167,0.01492,0.02782,0.04253,0.12702,0.02228,0.02015,0.06094,0.06966,0.00153,0.00772,0.04025,0.02406,0.06749,0.07507,0.01929,0.00095,0.05987,0.06327,0.09056,0.02758,0.00978,0.02360,0.00150,0.01974,0.00074]

def rotate_char(c,r):
    """
    Rotates character c by amount r.
    """
    let = ord(c) + r

    if let > 122:
        let = let - 26

    elif let < 97:
       let = let + 26

    return chr(let)

def encode_string(s,r):
    """
    rotate a string (lower case letters only)
    """
    result = ""
    for letter in s:
        if letter in "abcdefghijklmnopqrstuvwxyz":
            letter = rotate_char(letter,r)
        result = result + letter
    return result

def full_encode(s):
    """
takes a string and returns a string representing all the valid rotations,
one per line (embed the \n to get a newline).
    """
    x = ''
    for i in range(26):
        x += encode_strings(s,i) + "\n"
    return x

def distance(l1,l2):
    """
    Euclidean distance between l1 and l2. If the lists are of
    different lengths, go to the lenght of the shorter one
    """
    length = len(l1)
    if length>len(l2):
        length = len(l2)
    sum=0
    for i in range(length):
        sum = sum + (l1[i]-l2[i])*(l1[i]-l2[i])
    d = math.sqrt(sum)
    return d

def build_frequency_vector(s):
    # count the letters in the string
    spaces = s.count(' ')
    num_letters = len(s) - spaces
    v=[]
    for letter in "abcdefghijklmnopqrstuvwxyz":
        v.append(s.count(letter) / num_letters)

    return v
build_frequency_vector
def print_vector_table(v):
    s="abcdefghijklmnopqrstuvwxyz"
    for i in range(26):
        print(s[i]," : ",v[i])

def decode(s):
    freq = []
    for i in range(26):
        all_str = encode_string(s,i)
        all_freq = build_frequency_vector(all_str)
        dt = distance(real_stats, all_freq) #I know what this does just not how
        freq.append(dt)
        s_pos = freq.index(min(freq))  #position of the smallest value/creds to Shariar for that nice use of 'index'
        small = encode_string(s, s_pos)
    return small

s = 'Living that bum life'
print ("encoded sentence: " + encode_string(s,9))
print ("decoded sentence: " + decode(s))


#Jason Pinto
#100592099
#X509 certifcate

from OpenSSL import crypto, SSL
from socket import gethostname
from pprint import pprint
from time import gmtime, mktime
from os.path import exists, join

def X509():

    certificateimg = "X509 Certificate.crt" #this is the certificate file
    key = "X509 Key.key" #this is the key file

    def create_certificate(cert_dir):

        if not exists(join(cert_dir, certificateimg)) \
                or not exists(join(cert_dir, key)):

            # create a key pair
            k = crypto.PKey()
            k.generate_key(crypto.TYPE_RSA, 1024)

            # create a self-signed cert
            cert = crypto.X509()
            cert.get_subject().C = input("Please enter your Country's 2-letter abbreviation: ")
            cert.get_subject().ST = input("Please enter your Province: ")
            cert.get_subject().L = input("Please enter your City: ")
            cert.get_subject().O = input("Please enter your Organization: ")
            cert.get_subject().OU = input("Please enter your Organizational Unit, if unknown enter 'na': ")
            cert.get_subject().CN = input("Please enter the Creator/Common Name: ")

            cert.set_serial_number(1000)
            cert.gmtime_adj_notBefore(0) #get current date/time
            cert.gmtime_adj_notAfter(10*365*24*60*60) #set expiry to 10 years for current time
            cert.set_issuer(cert.get_subject()) #set the issuers of the certificate
            cert.set_pubkey(k) #set the public key
            cert.sign(k, 'sha256') # sign using sha256 can be changed to sha1 if desired

            open(join(cert_dir, certificateimg), "wb").write(
                crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
            open(join(cert_dir, key), "wb").write(
                crypto.dump_privatekey(crypto.FILETYPE_PEM, k))

    create_certificate(".") # create if successful

    print("")
    print("Congratulations! You have successfully created a self-signed X.509 Certificate.")
    print("")
    print("Using the information the user has provided, this program successfully created a self signed certificate by creating a certificate file, creating a key file, combining the user provided info, getting the current date, setting the file to expire 10 years from the current date, setting the issuer of the file, setting the public key, and signed using sha256. Both the certificate and key files were then written to the users directory.")
    while True:
        i = input("Would you like to see a more indepth breakdown?: (y/n) :")
        if i == "y":
            print("")
            print("BREAKDOWN: An instance of generating a key pair is done first using 1024-bit RSA. key pair k = crypto.PKey() k.generate_key(crypto.TYPE_RSA, 1024).")
            print("The input from the user is then used in the generation of the X509 certificate. The serial number of the certificate is then set to 1000 and will increment when a new one is renewed/generated. cert.set_serial_number(1000).")
            print("The current date is retrieved with this snippet of code. cert.gmtime_adj_notBefore(0).")
            print("The expiry time of the certificate is set with the same code as the current time, but it uses math to calculate the expiry in 10 years from the date of creation. cert.gmtime_adj_notAfter(10*365*24*60*60).")
            print("Then the issuer/creator of the certificate gets set. cert.set_issuer(cert.get_subject()).")
            print("The public key is also set at this time. cert.set_pubkey(k) And signed with sha256. cert.set_pubkey(k).")
            print("Everything is then written to the certificate file and the key file using this code. open(join(cert_dir, certificateimg), 'wb'). write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) open(join(cert_dir, key), 'wb').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)).")
            print("The certificate is created at this point and placed in the directory where this .py file is saved.")
            break
        elif i == "n":
            break
        else:
            print("")
            print("Enter either y/n")


from flask import Flask, render_template, request, redirect, url_for
import requests
import os
from todo_app.card import Card
from todo_app.data.view_model import ViewModel
from todo_app.flask_config import Config
from todo_app.data.trello_items import Trello_service

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config())

    with app.app_context():
        service = Trello_service()
        service.initiate()

    @app.route('/')
    def index():
        board_id = os.getenv('TRELLO_BOARD_ID')
        items_response = requests.get(f'https://api.trello.com/1/boards/{board_id}/cards',params={'key': os.getenv('TRELLO_KEY'),'token': os.getenv('TRELLO_TOKEN')})
        items_list = items_response.json()

        cards = []
        for item in items_list:
            new_card = Card(item["id"], item["name"], item["idList"])
            cards.append(new_card)

        # change to view model class - unit test
        view_model = ViewModel(cards)

        return render_template('index.html', view_model = view_model)

    @app.route('/items', methods=['POST'])
    def add_item():
        todo_title = request.form['text-input']
        items_response = requests.post(
            f'https://api.trello.com/1/cards',
            params={
                'key': os.getenv('TRELLO_KEY'),
                'token': os.getenv('TRELLO_TOKEN'),
                'name': todo_title,
                'idList': os.getenv('TRELLO_TODO_ID')
            }
        )
        return redirect('/')

    @app.route('/complete-item', methods=['POST'])
    def complete_item():
        card_id = request.form['id']
        items_response = requests.put(
            f'https://api.trello.com/1/cards/{card_id}',
            params={
                'key': os.getenv('TRELLO_KEY'),
                'token': os.getenv('TRELLO_TOKEN'),
                'idList': os.getenv('TRELLO_DONE_ID')
            }
        )
        return redirect('/')

    @app.route('/start-item', methods=['POST'])
    def start_item():
        card_id = request.form['id']
        items_response = requests.put(
            f'https://api.trello.com/1/cards/{card_id}',
            params={
                'key': os.getenv('TRELLO_KEY'),
                'token': os.getenv('TRELLO_TOKEN'),
                'idList': os.getenv('TRELLO_DOING_ID')
            }
        )
        return redirect('/')

    # if __name__ == '__main__':
    #     app.run(debug=True)

    return app


import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class  StatusBarDemo(QMainWindow):
    def __init__(self):
        super(StatusBarDemo, self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("状态栏")
        self.resize(300,200)
        bar=self.menuBar()
        file=bar.addMenu("File")
        file.addAction("show")
        file.triggered.connect(self.processTriger)
        self.statusBar=QStatusBar(self)
        self.setStatusBar(self.statusBar)
    def processTriger(self,a):
        if a.text()=='show':
            self.statusBar.showMessage(a.text()+"被点击",500)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    main=StatusBarDemo()
    main.show()
    sys.exit(app.exec_())


import numpy as np
import matplotlib.pyplot as plt
from ROOT import TH2D, TH1D, TCanvas, gPad, TF1, TGraph, gEnv, TFile, gStyle, TLegend
from root_numpy import fill_hist

proton = np.load('/hpcwork/jara0052/sichen/analysis_6.2/B1042_pr.pl1.flux.l1o9.2016000_7.6_all/147-1000GeV/transferdata/positive/147_175GeV/pattern0/positive_147_175_pattern_0.npy',encoding="latin1")
antiproton = np.load('/hpcwork/jara0052/sichen/analysis_6.2/B1042_antipr.pl1.1800_7.6_all/147-1000GeV/transferdata/negative/147_175GeV/pattern0/negative_147_175_pattern_0.npy',encoding="latin1")

for i in range(16):
	TH_proton = TH1D("proton","", 100,-5,5)
	TH_antiproton = TH1D("antiproton","", 100,-5,5)
	TH_proton.SetFillColor(6)
	TH_proton.SetFillStyle(3004)
	TH_proton.SetLineColor(6)
	TH_antiproton.SetFillColor(4)
	TH_antiproton.SetFillStyle(3005)
	TH_antiproton.SetLineColor(4)

	fill_hist(TH_proton, proton[:,i])
	fill_hist(TH_antiproton, antiproton[:,i] )

	scale = 150.0/TH_proton.Integral()
	TH_proton.Scale(scale)
	scale = 150.0/TH_antiproton.Integral()
	TH_antiproton.Scale(scale)

	c1 = TCanvas()
	gPad.SetGrid()
	gPad.SetFrameFillColor(0)
	gStyle.SetOptStat("00000000")
	TH_proton.Draw()
	TH_antiproton.Draw("same")

	leg =TLegend(.1,.7,.3,.9,)
	leg.SetFillColor(0)
	leg.AddEntry(TH_proton,"Chargeproton")
	leg.AddEntry(TH_antiproton,"Chargeantiproton")
	leg.Draw()

	TH_proton.GetXaxis().SetTitle("L1L9RigidityMatching");

	c1.Update()
	c1.SaveAs("proton_antiproton_correct"+str(i)+".pdf")


# coding=UTF8
import cProfile
import io
import os
from datetime import datetime

from django.conf import settings
from django.http import HttpResponse
from py_zipkin.zipkin import zipkin_span

from apps.core import zipkin
from apps.core.helpers import get_request_cache, get_schema_cache, set_tracing_attrs
from apps.data.models import DataObject
from apps.instances.helpers import set_current_instance

def clear_request_data():
    get_request_cache().clear()
    get_schema_cache().clear()
    set_tracing_attrs(None)
    set_current_instance(None)
    DataObject.loaded_klass = None

class PrepareRequestMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        clear_request_data()
        return self.get_response(request)

class InstrumentMiddleware:  # pragma: no cover
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if 'profile' in request.GET:
            request.profiler = cProfile.Profile()
            request.profiler.enable()

        response = self.get_response(request)

        if hasattr(request, 'profiler'):
            import pstats

            request.profiler.disable()
            stamp = (request.META['REMOTE_ADDR'], datetime.now())
            request.profiler.dump_stats('/tmp/%s-%s.pro' % stamp)

            stream = io.StringIO()
            stats = pstats.Stats('/tmp/%s-%s.pro' % stamp, stream=stream)
            stats.strip_dirs()
            stats.sort_stats('time')
            stats.print_stats(12)
            stats.print_callers(12)
            stats.print_callees(12)
            os.remove('/tmp/%s-%s.pro' % stamp)
            data = b'
' + stream.getvalue().encode() + b'
'
            data += b'
Original response:
' + response.content + b'
'
            stream.close()
            return HttpResponse(content=data)
        return response

class ZipkinMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        self.zipkin_attrs = zipkin.create_zipkin_attr_from_request(request)
        set_tracing_attrs(self.zipkin_attrs)

        if self.zipkin_attrs.is_sampled:
            self.zipkin_context = zipkin_span(
                service_name=settings.TRACING_SERVICE_NAME,
                span_name='{0} {1}'.format(request.method, request.path),
                zipkin_attrs=self.zipkin_attrs,
                transport_handler=zipkin.transport_handler,
            )
            self.zipkin_context.start()

        response = self.get_response(request)

        if self.zipkin_attrs.is_sampled:
            self.zipkin_context.update_binary_annotations(
                zipkin.get_binary_annotations(request, response),
            )
            self.zipkin_context.stop()
        return response


import random
import math
# Going off of the Papers structure, a gnome is one

class Genome(object):
    def __init__(self, fitness: int = 0, connections = [], nodes = []):
        self.connections = connections
        self.nodes = nodes
        self.fitness = fitness
        self.adjusted_fitness = 0
        self.other_adjusted_fitness = 0

    def __str__(self):
        result = "fitness: " + str(self.fitness) + '\n'
        result += "Nodes in Genome:\n"
        for node in self.nodes:
            result += str(node) + '\n'

        result += "Connections details:\n"
        for connect in self.connections:
            result += str(connect) + '\n\n'

        return result


from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from random import randint

import cv2
import mmcv
import numpy as np
import os
import torch
import torch.distributed as dist
import torch.nn as nn
from mmcv.runner import auto_fp16
from mmcv.utils import print_log
from mmcv.image import imread, imwrite

from mmdet.utils import get_root_logger

class PostProcessor():
    def __init__(self,
                classes,
                score_thr=0.3,
                thickness=1,
                font_scale=0.5,
                win_name='',
                wait_time=0):
        self.num_classes = len(classes)
        self.classes = classes
        self.score_thr = score_thr
        self.thickness = thickness
        self.font_scale = font_scale
        self.win_name = win_name
        self.wait_time = wait_time
        self.colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0), (0, 255, 255),
                        (255, 0, 255), (255, 255, 255), (0, 0, 0), (255, 0, 128), (0, 191, 255),
                        (10, 255, 128), (191, 255, 0), (255, 191, 0), (255, 128, 10), (50, 152, 89)]
        self.makeColors()
        self.iitpID = 1
        self.iitpJson = {'annotations':[]}
        self.box_real_class = [0, 1, 1, 2, 3, 4, 5, 6, 5, 6]

    def makeColors(self):
        if len(self.colors) >= self.num_classes:
            return
        else:
            while len(self.colors) < self.num_classes:
                self.colors.append((randint(20, 230), randint(20, 230), randint(20, 230)))
            return

    def saveResult(self,
                    img,
                    result,
                    show=False,
                    out_file=None):
        img, bboxes, labels = self.extractInfo(img, result, show=False, out_file=out_file)

        # draw bounding boxes
        return self.imshow_det_bboxes(img, bboxes, labels, show=show, out_file=out_file)

    def labelChanger(self, labels):
        appliedLabels = []
        for i in labels:
            if i == 8:
                if 3 in labels or 4 in labels:
                    i = 3
            if i == 9:
                if 3 in labels:
                    i = 3
                elif 4 in labels:
                    i = 0
            i = self.box_real_class[i]
            i += 1
            appliedLabels.append(i)

        return appliedLabels

    def saveIitp(self, imgPath, result):
        bboxes, labels = self.extractInfo(result)
        bboxes, labels = self.iitpProcess(bboxes, labels)
        if len(labels) < 1:
            return False
        return self.annoMaker(imgPath, bboxes, labels)

    def annoMaker(self, imgPath, bboxes, labels):
        anno = {}
        anno['id'] = self.iitpID
        self.iitpID += 1
        fileName = imgPath.split('/')[-1]
        anno['file_name'] = fileName
        anno['object'] = []
        for box, label in zip(bboxes, labels):
            anno['object'].append({
                'box': box,
                'label': 'c'+str(label)
                })
        self.iitpJson['annotations'].append(anno)

        return labels

    def saveScorechecker(self, file_path, result, cascadecsv):
        bboxes, labels = self.extractInfo(result)
        _, labels = self.iitpProcess(bboxes, labels)
        output_class = [0] * 8
        cascadecsv.write(file_path.split("/")[-1])
        for label in labels:
            output_class[label] = 1
        output_class.pop(0)
        for i in output_class:
            cascadecsv.write("," + str(i))
        cascadecsv.write("," + "\n")

    def iitpProcess(self, bboxes, labels):
        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
        box_scores = []

        if self.score_thr > 0:
            assert bboxes.shape[1] == 5
            scores = bboxes[:, -1]
            inds = scores > self.score_thr
            bboxes = bboxes[inds, :]
            labels = labels[inds]
            box_scores = scores[inds]

        if len(labels) > 1:
            bboxes, labels, box_scores = self.bb_intersection_over_union(bboxes, labels, box_scores)

        labels = self.labelChanger(labels)

        processedBoxes = []
        for box in bboxes:
            box = box.tolist()
            box.pop()
            box = list(map(int, box))
            processedBoxes.append(box)

        return processedBoxes, labels

    def bb_intersection_over_union(self, bboxes, labels, box_scores):
        # determine the (x, y)-coordinates of the intersection rectangle
        best_indexes = []

        for i in range(0, len(bboxes) - 1):

            best_iou = -1
            best_list = []

            for j in range(i + 1 , len(bboxes)):
                xA = max(bboxes[i][0], bboxes[j][0])
                yA = max(bboxes[i][1], bboxes[j][1])
                xB = min(bboxes[i][2], bboxes[j][2])
                yB = min(bboxes[i][3], bboxes[j][3])
                # compute the area of intersection rectangle
                interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
                # compute the area of both the prediction and ground-truth
                # rectangles
                boxAArea = (bboxes[i][2] - bboxes[i][0] + 1) * (bboxes[i][3] - bboxes[i][1] + 1)
                boxBArea = (bboxes[j][2] - bboxes[j][0] + 1) * (bboxes[j][3] - bboxes[j][1] + 1)
                # compute the intersection over union by taking the intersection
                # area and dividing it by the sum of prediction + ground-truth
                # areas - the interesection area
                iou = interArea / float(boxAArea + boxBArea - interArea)
                if iou > best_iou:
                    best_iou = iou
                    best_list = [i , j, best_iou]

                    best_indexes.append(best_list)

        index = []
        for best_index in best_indexes:
            if best_index[2] > 0.98: # best_iou
                if box_scores[best_index[0]] > box_scores[best_index[1]]:
                    index.append(best_index[1])

                else :
                    index.append(best_index[0])

        index = set(index)
        index = sorted(list(index), reverse=True)

        for i in index :
            if box_scores[i] < 0.35:
                bboxes = np.delete(bboxes, i, axis = 0)
                labels = np.delete(labels, i, axis = 0)
                box_scores = np.delete(box_scores, i, axis = 0)

        # return the intersection over union value
        return bboxes, labels, box_scores

    def cropBoxes(self, img, result, out_file=None):
        img, bboxes, labels = self.extractInfo(img, result, show=False, out_file=out_file)
        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
        img = imread(img)
        box_scores = []

        if self.score_thr > 0:
            assert bboxes.shape[1] == 5
            scores = bboxes[:, -1]
            inds = scores > self.score_thr
            bboxes = bboxes[inds, :]
            labels = labels[inds]
            box_scores = scores[inds]

        img = np.ascontiguousarray(img)
        croppedImgs = []
        out_label = []

        if len(labels) > 1:
            bboxes, labels, box_scores = self.bb_intersection_over_union(bboxes, labels, box_scores)

        for idx, (bbox, label) in enumerate(zip(bboxes, labels)):
            if label != 8 and label != 9:
                bbox_int = bbox.astype(np.int32)
                heightRange = (bbox_int[1], bbox_int[3])
                widthRange = (bbox_int[0], bbox_int[2])

                dst = img.copy()

                center_x = int(int(bbox_int[0]) - int(bbox_int[0])*0.15)
                center_y = int(int(bbox_int[1]) - int(bbox_int[0])*0.15)
                width = int(int(bbox_int[2]) + int(bbox_int[2])*0.15)
                height = int(int(bbox_int[3]) + int(bbox_int[3])*0.15)

                dst = dst[center_y:height, center_x:width]

                croppedImgs.append(dst)

            out_label.append(label)

        out_label = self.labelChanger(out_label)

        return croppedImgs, out_label

    def extractInfo(self,
                    # img,
                    result,
                    # show=False,
                    # out_file=None
                    ):

        # batch_size = len(result)
        # print('batch_size : ', batch_size)
        # print('result : ', len(result[0]), result)

        # img = mmcv.imread(img)
        # img = img.copy()
        if isinstance(result, tuple):
            bbox_result, segm_result = result
            if isinstance(segm_result, tuple):
                segm_result = segm_result[0]  # ms rcnn
                # print('check msrcnn : ', len(segm_result))
        else:
            bbox_result, segm_result = result, None
        bboxes = np.vstack(bbox_result)
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bbox_result)
        ]
        labels = np.concatenate(labels)
        # draw segmentation masks
        # if segm_result is not None and len(labels) > 0:  # non empty
        #     # print('check segm_result is not None')
        #     segms = mmcv.concat_list(segm_result)
        #     inds = np.where(bboxes[:, -1] > self.score_thr)[0]
        #     np.random.seed(42)
        #     color_masks = [
        #         np.random.randint(0, 256, (1, 3), dtype=np.uint8)
        #         for _ in range(max(labels) + 1)
        #     ]
        #     for i in inds:
        #         i = int(i)
        #         color_mask = color_masks[labels[i]]
        #         mask = segms[i].astype(bool)
        #         img[mask] = img[mask] * 0.5 + color_mask * 0.5
        # # if out_file specified, do not show image in window
        # if out_file is not None:
        #     show = False

        # # if not (show or out_file):
        # #     return img

        # return img, bboxes, labels
        return bboxes, labels

    def imshow_det_bboxes(self,
                        img,
                        bboxes,
                        labels,
                        show=True,
                        out_file=None):

        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
        img = imread(img)

        if self.score_thr > 0:
            assert bboxes.shape[1] == 5
            scores = bboxes[:, -1]
            inds = scores > self.score_thr
            bboxes = bboxes[inds, :]
            labels = labels[inds]

        img = np.ascontiguousarray(img)
        for bbox, label in zip(bboxes, labels):
            bbox_int = bbox.astype(np.int32)
            left_top = (bbox_int[0], bbox_int[1])
            right_bottom = (bbox_int[2], bbox_int[3])
            cv2.rectangle(
                img, left_top, right_bottom, self.colors[label], thickness=self.thickness)
            label_text = self.classes[
                label] if self.classes is not None else f'cls {label}'
            if len(bbox) > 4:
                label_text += f'|{bbox[-1]:.02f}'
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - (label*2*randint(0, 1))),
                        cv2.FONT_HERSHEY_COMPLEX, self.font_scale, self.colors[label])

        if show:
            imshow(img, self.win_name, self.wait_time)
        if out_file is not None:
            imwrite(img, out_file)
        return img


from typing import Tuple, List, Any
from prefect import task

@task
def concat(datas: List[Tuple[Any, str]]) -> dict:
    """Load raw data of investing.com from the datalake

    Args:
        datas (List[str]): list of tuple data. We assume tuple = (raw, meta).

    Returns:
        dict: raw data of the symbol
    """

    result = {}

    # Load and clean data
    for data in datas:
        print(data[1])
        result[data[1]] = data[0]

    return result


import pandas as pd
import numpy as np
import lxml
import sqlite3

conn = sqlite3.connect('/media/maciej/hodor/Stackoverflow.sqlite')

import xml.etree.ElementTree as ET
tree_Badges = ET.iterparse('/media/maciej/hodor/Badges.xml')

conn.execute("""CREATE TABLE Badges (
            Class INT,
            Date DATETIME,
            Id INT PRIMARY KEY,
            Name CHAR(30),
            TagBased CHAR(10),
            UserId INT
            )""")
z=0
import time
start=time.time()
for el in tree_Badges:
    if z%10000==0:
        conn.commit()
        print(z,time.time()-start)
    conn.executemany("""INSERT INTO Badges VALUES(?,?,?,?,?,?)""",pd.DataFrame(el[1].attrib,index=[el[1].attrib['Id']]).values)
    z+=1

conn.commit()
conn.close()


import os
import sys
import logging

bin_path = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.split(bin_path)[0]

src_path = os.path.join(root_path,'src')
config_path = os.path.join(root_path,'config')
static_path = os.path.join(root_path,'static')
templates_path = os.path.join(root_path,'templates')
log_path = os.path.join(root_path,'log')
append_path =(root_path,src_path,bin_path, config_path,static_path,templates_path,log_path)
sys.path.extend(append_path)

logging.basicConfig(level=logging.INFO,
        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='%s/MaterialWeb.log'%log_path,
        filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

import service as MaterialWeb

config = {}
config['static'] = static_path
config['templates'] = templates_path
exec(open(os.path.join(config_path,'MaterialWeb.conf')).read(),config)

def start_server():
    print("start material web")
    MaterialWeb.start_http(config)

if __name__ == '__main__':
    start_server()


# -*- coding: utf-8 -*-
from args import args
import aae
from aae import AAE, Conf

conf = Conf()
conf.gpu_enabled = True if args.gpu_enabled == 1 else False
conf.learning_rate = 0.0001
conf.gradient_momentum = 0.1
conf.gradient_clipping = 5.0
conf.ndim_z = 2
conf.ndim_y = 10

conf.batchnorm_before_activation = True

conf.generator_hidden_units = [600, 600]
conf.generator_activation_function = "elu"
conf.generator_apply_dropout = False
conf.generator_apply_batchnorm = False
conf.generator_apply_batchnorm_to_input = False

conf.decoder_hidden_units = [600, 600]
conf.decoder_activation_function = "elu"
conf.decoder_apply_dropout = False
conf.decoder_apply_batchnorm = False
conf.decoder_apply_batchnorm_to_input = False

conf.discriminator_z_hidden_units = [600, 600]
conf.discriminator_z_activation_function = "elu"
conf.discriminator_z_apply_dropout = False
conf.discriminator_z_apply_batchnorm = False
conf.discriminator_z_apply_batchnorm_to_input = False

conf.q_z_x_type = aae.Q_Z_X_TYPE_DETERMINISTIC

aae = AAE(conf, name="aae")
aae.load(args.model_dir)


# 测试用例级别

import pytest
from selenium import webdriver
from web_basepage.TestDatas import Common_Datas as CD
from web_basepage.PageObjects.login_page import LoginPage
import logging
from web_basepage.Common import logger

@pytest.fixture
def init_driver():
    """
    前置：打开浏览器，访问系统网址
    后置：退出浏览器。
    """
    logging.info("***** conftest.py共享的 init_driver 的前置  *****")
    driver = webdriver.Chrome()
    driver.get(CD.login_url)
    yield driver
    driver.quit()
    logging.info("***** conftest.py共享的 init_driver 的后置  *****")

"""
前置：打开浏览器，访问系统网址 + 登陆系统
后置：退出浏览器。
"""
@pytest.fixture
def init_login(init_driver): # init_driver代表的是它的返回值：driver
    LoginPage(init_driver).login(CD.user, CD.passwd)
    yield init_driver  # 返回driver对象。
    print("1111111111111")
"""
init_driver的前置
init_login的前置
init_login的后置
init_driver的后置

假设：init_driver是class级，init_login是function级别？
      可以"继承"。
      init_driver是function级，init_login是function级别?
      可以调用。
      init_driver是function级，init_login是class级别?
      不可以。

一个fixture，可以使用比它高的/与它同级的 fixture作为它的参数。

   function，可以调用class,function,module,session.
   function最小单位。最后执行。其它的级别一定是比它先执行。

进去：校门 -> 楼大门 -> 教室门
出来：教室门 -> 楼大门 -> 校门
"""

@pytest.fixture(scope="session",autouse=True)
def mySs():
    print("**** 我是session级别的fixture 前置 ****")
    yield
    print("**** 我是session级别的fixture 后置 ****")

@pytest.fixture(scope="module")
def myMo():
    print("**** 我是module级别的fixture 前置 ****")
    yield
    print("**** 我是module级别的fixture 后置 ****")


"""Implementation of Rule L011."""
from typing import List, Optional, Union

from sqlfluff.core.parser import (
    WhitespaceSegment,
    KeywordSegment,
)

from sqlfluff.core.rules.base import BaseRule, LintResult, LintFix, RuleContext
from sqlfluff.core.rules.doc_decorators import document_fix_compatible

@document_fix_compatible
class Rule_L011(BaseRule):
    """Implicit/explicit aliasing of table.

    Aliasing of table to follow preference
    (explicit using an `AS` clause is default).

    | **Anti-pattern**
    | In this example, the alias 'voo' is implicit.

    .. code-block:: sql

        SELECT
            voo.a
        FROM foo voo

    | **Best practice**
    | Add `AS` to make it explicit.

    .. code-block:: sql

        SELECT
            voo.a
        FROM foo AS voo

    """

    config_keywords = ["aliasing"]

    _target_elems = ("from_expression_element",)

    def _eval(self, context: RuleContext) -> Optional[LintResult]:
        """Implicit aliasing of table/column not allowed. Use explicit `AS` clause.

        We look for the alias segment, and then evaluate its parent and whether
        it contains an AS keyword. This is the _eval function for both L011 and L012.

        The use of `raw_stack` is just for working out how much whitespace to add.

        """
        fixes = []

        if context.segment.is_type("alias_expression"):
            if context.parent_stack[-1].is_type(*self._target_elems):
                if any(e.name.lower() == "as" for e in context.segment.segments):
                    if self.aliasing == "implicit":  # type: ignore
                        if context.segment.segments[0].name.lower() == "as":

                            # Remove the AS as we're using implict aliasing
                            fixes.append(LintFix("delete", context.segment.segments[0]))
                            anchor = context.raw_stack[-1]

                            # Remove whitespace before (if exists) or after (if not)
                            if (
                                len(context.raw_stack) > 0
                                and context.raw_stack[-1].type == "whitespace"
                            ):
                                fixes.append(LintFix("delete", context.raw_stack[-1]))
                            elif (
                                len(context.segment.segments) > 0
                                and context.segment.segments[1].type == "whitespace"
                            ):
                                fixes.append(
                                    LintFix("delete", context.segment.segments[1])
                                )

                            return LintResult(anchor=anchor, fixes=fixes)

                else:
                    insert_buff: List[Union[WhitespaceSegment, KeywordSegment]] = []

                    # Add initial whitespace if we need to...
                    if context.raw_stack[-1].name not in ["whitespace", "newline"]:
                        insert_buff.append(WhitespaceSegment())

                    # Add an AS (Uppercase for now, but could be corrected later)
                    insert_buff.append(KeywordSegment("AS"))

                    # Add a trailing whitespace if we need to
                    if context.segment.segments[0].name not in [
                        "whitespace",
                        "newline",
                    ]:
                        insert_buff.append(WhitespaceSegment())

                    return LintResult(
                        anchor=context.segment,
                        fixes=[
                            LintFix("create", context.segment.segments[0], insert_buff)
                        ],
                    )
        return None


# Factorial
# Factorial is for positive number only
# 6! = 6 * 5 * 4 * 3 * 2 * 1
# 6! = 6 * (6 - 1)!
# 6! = 6 * 5! --> 5! = 6!/6
# n! = n * (n - 1)! = (n + 1)! / (n + 1)
# Recursive function is a function calling it self until a condition is met. Basically, it divide
# itself in to smaller part.

def facc(n):
	result = 1
	if n > 1:
		for i in range(2, n + 1):
			result *= i
	return result

def facc2(n):
	# n! = n * (n - 1)!
	if n < 1:
		return 1
	if n >= 1:
		return (n * (facc2(n - 1)))
		# n = 5
		# 5 * fac4 = 120
		# 4 * fac3 = 24
		# 3 * fac2 = 6
		# 2 * fac1 = 2
		# 1 * fac0 = 1

# def facc3(n):
# 	# n = (n + 1)! / (n + 1)
# 	if n < 1:
# 		return 1
# 	if n >= 1:
# 		return int(facc3(n + 1)/ (n + 1))

for i in range(120):
	print(i, facc2(i))

# Think of a recusive version of the function f(n) = 3 * n, i.e. the multiples of 3
n = [1,2,3,4,5,6]

def sumc(n):
	lenn = len(n)
	if lenn == 0:
		pass
	else:
		return n * (sumc())


import numpy as np
import tensorflow as tf
from dirac_layers import *
import time
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
w=200
h=200
c=3
start = time.time()
print('_________')
print('start:',start)
train_ = np.load("train_data.npz")
val_ = np.load("val_data.npz")
train_images = train_["images"]
train_visibility = train_["visibility"]
val_images = val_["images"]
val_visibility = val_["visibility"]
print(train_images.shape,train_visibility.shape)

# 打乱顺序
num_example = train_images.shape[0]     #shape 矩阵维度
arr = np.arange(num_example)
np.random.shuffle(arr)
data = train_images[arr]
visibility = train_visibility[arr]

x_train = train_images
y_train = train_visibility[:, np.newaxis]
print(x_train.shape,y_train.shape)
x_val = val_images
y_val = val_visibility[:, np.newaxis]

x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.float32,shape=[None,1],name='y_')

def inference (input_tensor):
    with tf.variable_scope('conv1'):
        conv1_weights = tf.get_variable("weight", [7, 7, 3, 64],
                                        initializer=tf.truncated_normal_initializer(stddev=0.0001))
        conv1_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0))
        conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 2, 2, 1], padding='VALID')
        conv1 = tf.layers.batch_normalization(conv1, axis=3)
        x = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
    outdim = 16
    for group in range(0, 3):
        for block in range(0, 2):
            #x = ncrelu(x, name="crelu_" + str(group) + "_" + str(block))
            x = dirac_conv2d(x, outdim, 3, 3, 1, 1, name="conv_" + str(group) + "_" + str(block))
            x = dirac1_conv2d(x, outdim, 3, 3, 1, 1, name="conv1_" + str(group) + "_" + str(block))
            x = ncrelu(x, name="crelu_" + str(group) + "_" + str(block))
        if (group != 3 - 1):
            x = tf.nn.pool(x, [2, 2], "MAX", "VALID", None, [2, 2], name="maxpool_" + str(group))

        outdim = outdim * 2

    temp_shape = x.get_shape().as_list()
    x = tf.nn.avg_pool(x, [1, temp_shape[1], temp_shape[2], 1], [1, temp_shape[1], temp_shape[2], 1],
                               "VALID", name="avgpool")
    flatten = tf.contrib.layers.flatten(x)
    dense1 = tf.layers.dense(flatten, units=50, activation=tf.nn.relu)
    logits = tf.layers.dense(dense1, units=1, activation=None)
    return dense1,logits

dense1,logits = inference(x)
b = tf.constant(value=1,dtype=tf.float32)
dense1_eval = tf.multiply(dense1,b,name='dense1_eval')
logits_eval = tf.multiply(logits,b,name='logits_eval')
loss = tf.reduce_mean(tf.square(y_ - logits))

train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

a = tf.abs(tf.subtract(logits,y_))
d = y_*0.1
correct_prediction=tf.less_equal(a,d)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
mape = tf.reduce_mean(tf.divide(tf.abs(y_ - logits),y_))

def shuffle_data(inputs=None, targets=None, shuffle=True):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    return inputs[indices], targets[indices]

model_path='./1.24&-dirac_model/1.24model.ckpt'
# 训练和测试数据，可将n_epoch设置更大一些
n_epoch = 100
batch_size = 64
loss_basic = 100000
num = 0
loss_end = 0
mape_end = 0
acc_end = 0
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(n_epoch):
    x_train_all, y_train_all = shuffle_data(x_train, y_train)
    print(x_train_all.shape, y_train_all.shape)

    train_loss, train_mape, train_acc, n_batch = 0, 0, 0, 0

    for i in range(0, len(x_train_all) + 1, batch_size):
        start = i
        end = min(i+batch_size,len(x_train))
        x_train_a, y_train_a = x_train_all[start:end], y_train_all[start:end]
        #print(x_train_a.shape, y_train_a.shape)
        _, err, ac, mape_loss, yuce, zhi = sess.run([train_op, loss, acc, mape, logits, y_], feed_dict={x: x_train_a, y_: y_train_a})
        train_loss += err;
        train_acc += ac;
        train_mape += mape_loss
        n_batch += 1
        print('epoch ' + str(epoch) + ', batch ' + str(n_batch) )
        print("   this batch train loss: %f" % (err))
        print("   this batch train mape: %f" % (mape_loss))
        print("   this batch train acc: %f" % (ac))
        print('真实值为:', zhi)
        print('预测值为：',yuce)
    trainLoss = train_loss / n_batch
    trainMape = train_mape / n_batch
    trainAcc = train_acc / n_batch
    print("epoch " + str(epoch) + " train loss: %f" % (trainLoss))
    print("epoch " + str(epoch) + " train mape: %f" % (trainMape))
    print("epoch " + str(epoch) + " train acc: %f" % (trainAcc))

    # validation
    start2 = epoch * batch_size % len(x_val)
    end2 = min(start2 + batch_size, len(x_val))
    #val_loss, val_acc, n_batch = 0, 0, 0
    #for batches in range(end2-start2):
    err, ac, mape_loss = sess.run([loss, acc, mape], feed_dict={x: x_val[start2:end2], y_: y_val[start2:end2]})
    print("   validation loss: %f" % (err))
    print("   validation mape: %f" % (mape_loss))
    print("   validation acc: %f" % (ac))
    print('epoches:', epoch)
    if err < loss_basic:
        loss_basic = err
        saver.save(sess, model_path)
        num = epoch
        loss_end = err
        mape_end = mape_loss
        acc_end = ac
print("epoches_end=", num)
print("loss_end=", loss_end)
print("mape_end=",mape_end)
print("acc_end=", acc_end)
sess.close()
finish = time.time()
print('finish:',finish)
TIME = finish-start
print('time:',TIME)


import os

DEBUG = True

APP_DB = os.getenv("BLOG_DB_FULL_PATH")
APP_LOG = os.getenv("BLOG_LOG_FULL_PATH")

if not APP_DB:
    raise Exception("Environment variable BLOG_DB_FULL_PATH not set")
if not APP_LOG:
    raise Exception("Environment variable BLOG_LOG_FULL_PATH not set")

SQLALCHEMY_DATABASE_URI = "sqlite:///" + APP_DB
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = b'9d236d8464e74cedb4c8d7042cd8bb16'


import asyncio
import logging
import re
import math
from datetime import datetime, timezone, timedelta
from functools import partial
from typing import Dict, List, NewType, Optional, Tuple, NamedTuple

from galaxy.api.errors import UnknownBackendResponse
from galaxy.api.types import Achievement, Game, LicenseInfo, UserInfo, UserPresence, PresenceState, SubscriptionGame
from galaxy.api.consts import LicenseType
from http_client import paginate_url
from parsers import PSNGamesParser

# game_id_list is limited to 5 IDs per request
GAME_DETAILS_URL = "https://pl-tpy.np.community.playstation.net/trophy/v1/apps/trophyTitles" \
    "?npTitleIds={game_id_list}" \
    "&fields=@default" \
    "&npLanguage=en"

GAME_LIST_URL = "https://gamelist.api.playstation.com/v1/users/{user_id}/titles" \
    "?type=owned,played" \
    "&app=richProfile" \
    "&sort=-lastPlayedDate" \
    "&iw=240"\
    "&ih=240"\
    "&fields=@default"

PLAYED_GAME_LIST_URL = "https://gamelist.api.playstation.com/v2/users/{user_id}/titles" \
    "?type=owned,played" \
    "&app=richProfile" \
    "&sort=-lastPlayedDate" \
    "&iw=240"\
    "&ih=240"\
    "&fields=@default"

TROPHY_TITLES_URL = "https://pl-tpy.np.community.playstation.net/trophy/v1/trophyTitles" \
    "?fields=@default" \
    "&platform=PS4" \
    "&npLanguage=en"

EARNED_TROPHIES_PAGE = "https://pl-tpy.np.community.playstation.net/trophy/v1/" \
    "trophyTitles/{communication_id}/trophyGroups/{trophy_group_id}/trophies?" \
    "fields=@default,trophyRare,trophyEarnedRate,trophySmallIconUrl,groupId" \
    "&visibleType=1" \
    "&npLanguage=en"

USER_INFO_URL = "https://pl-prof.np.community.playstation.net/userProfile/v1/users/{user_id}/profile2" \
    "?fields=accountId,onlineId"

USER_INFO_PSPLUS_URL = "https://pl-prof.np.community.playstation.net/userProfile/v1/users/{user_id}/profile2" \
    "?fields=plus"

DEFAULT_AVATAR_SIZE = "l"
FRIENDS_URL = "https://us-prof.np.community.playstation.net/userProfile/v1/users/{user_id}/friends/profiles2" \
    "?fields=accountId,onlineId,avatarUrls&avatarSizes={avatar_size_list}"

FRIENDS_WITH_PRESENCE_URL = "https://us-prof.np.community.playstation.net/userProfile/v1/users/{user_id}/friends/profiles2" \
    "?fields=accountId,onlineId,primaryOnlineStatus,presences(@titleInfo,lastOnlineDate)"

ACCOUNTS_URL = "https://accounts.api.playstation.com/api/v1/accounts/{user_id}"

PSN_PLUS_SUBSCRIPTIONS_URL = 'https://store.playstation.com/subscriptions'

DEFAULT_LIMIT = 100
MAX_TITLE_IDS_PER_REQUEST = 5

CommunicationId = NewType("CommunicationId", str)
TitleId = NewType("TitleId", str)
UnixTimestamp = NewType("UnixTimestamp", int)
TrophyTitleName = NewType("TrophyTitleName", str)
TrophyTitles = Dict[CommunicationId, UnixTimestamp]

class TrophyTitleInfo(NamedTuple):
    communication_id: CommunicationId
    trophy_title_name: TrophyTitleName

def parse_timestamp(earned_date) -> UnixTimestamp:
    date_format = "%Y-%m-%dT%H:%M:%S.%fZ" if '.' in earned_date else "%Y-%m-%dT%H:%M:%SZ"
    dt = datetime.strptime(earned_date, date_format)
    dt = datetime.combine(dt.date(), dt.time(), timezone.utc)
    return UnixTimestamp(int(dt.timestamp()))

def parse_play_duration(duration: Optional[str]) -> int:
    """Returns time of played game in minutes from PSN API format `PT{HOURS}H{MINUTES}M{SECONDS}S`. Example: `PT2H33M3S`"""
    if not duration:
        raise UnknownBackendResponse(f'nullable playtime duration: {type(duration)}')
    try:
        result = re.match(
            r'(?:PT)?'
            r'(?:(?P<hours>\d*)H)?'
            r'(?:(?P<minutes>\d*)M)?'
            r'(?:(?P<seconds>\d*)S)?$',
            duration
        )
        mapped_result = {k: float(v) for k, v in result.groupdict(0).items()}
        time = timedelta(**mapped_result)
    except (ValueError, AttributeError, TypeError):
        raise UnknownBackendResponse(f'Unmatchable gametime: {duration}')

    total_minutes = math.ceil(time.seconds / 60 + time.days * 24 * 60)
    return total_minutes

def date_today():
    return datetime.today()

class PSNClient:
    def __init__(self, http_client):
        self._http_client = http_client

    @staticmethod
    async def _async(method, *args, **kwargs):
        loop = asyncio.get_running_loop()
        return await loop.run_in_executor(None, partial(method, *args, **kwargs))

    async def fetch_paginated_data(
        self,
        parser,
        url,
        counter_name,
        limit=DEFAULT_LIMIT,
        *args,
        **kwargs
    ):
        response = await self._http_client.get(paginate_url(url=url, limit=limit), *args, **kwargs)
        if not response:
            return []

        try:
            total = int(response.get(counter_name, 0))
        except ValueError:
            raise UnknownBackendResponse()

        responses = [response] + await asyncio.gather(*[
            self._http_client.get(paginate_url(url=url, limit=limit, offset=offset), *args, **kwargs)
            for offset in range(limit, total, limit)
        ])

        try:
            return [rec for res in responses for rec in parser(res)]
        except Exception:
            logging.exception("Cannot parse data")
            raise UnknownBackendResponse()

    async def fetch_data(self, parser, *args, **kwargs):
        response = await self._http_client.get(*args, **kwargs)

        try:
            return parser(response)
        except Exception:
            logging.exception("Cannot parse data")
            raise UnknownBackendResponse()

    async def async_get_own_user_info(self):
        def user_info_parser(response):
            logging.debug(f'user profile data: {response}')
            return response["profile"]["accountId"], response["profile"]["onlineId"]

        return await self.fetch_data(
            user_info_parser,
            USER_INFO_URL.format(user_id="me")
        )

    async def get_psplus_status(self) -> bool:
        def user_subscription_parser(response):
            status = response["profile"]["plus"]
            if status in [0, 1, True, False]:
                return bool(status)
            raise TypeError

        return await self.fetch_data(
            user_subscription_parser,
            USER_INFO_PSPLUS_URL.format(user_id="me")
        )

    async def async_get_owned_games(self):
        def game_parser(title):
            return Game(
                game_id=title["titleId"],
                game_title=title["name"],
                dlcs=[],
                license_info=LicenseInfo(LicenseType.SinglePurchase, None)
            )

        def games_parser(response):
            return [
                game_parser(title) for title in response["titles"]
            ] if response else []

        return await self.fetch_paginated_data(
            games_parser,
            GAME_LIST_URL.format(user_id="me"),
            "totalResults"
        )

    async def async_get_game_trophy_title_info_map(self, game_ids: List[TitleId]) \
            -> Dict[TitleId, List[TrophyTitleInfo]]:
        def communication_ids_parser(response):
            def get_trophy_title_infos(trophy_titles) -> List[TrophyTitleInfo]:
                result = []
                for trophy_title in trophy_titles:
                    comm_id = trophy_title.get("npCommunicationId")
                    trophy_title_name = trophy_title.get("trophyTitleName", "")
                    if comm_id is not None:
                        result.append(TrophyTitleInfo(
                            CommunicationId(comm_id),
                            TrophyTitleName(trophy_title_name)
                        ))
                return result

            try:
                return {
                    app["npTitleId"]: get_trophy_title_infos(app["trophyTitles"])
                    for app in response["apps"]
                } if response else {}
            except (KeyError, IndexError):
                return {}

        mapping = await self.fetch_data(
            communication_ids_parser,
            GAME_DETAILS_URL.format(game_id_list=",".join(game_ids))
        )

        return {
            game_id: mapping.get(game_id, [])
            for game_id in game_ids
        }

    async def get_trophy_titles(self) -> TrophyTitles:
        def title_parser(title) -> Tuple[CommunicationId, UnixTimestamp]:
            return (title["npCommunicationId"], parse_timestamp((title.get("fromUser") or {})["lastUpdateDate"]))

        def titles_parser(response) -> List[Tuple[CommunicationId, UnixTimestamp]]:
            return [
                title_parser(title) for title in response.get("trophyTitles", [])
            ] if response else []

        result = await self.fetch_paginated_data(
            parser=titles_parser,
            url=TROPHY_TITLES_URL,
            counter_name="totalResults"
        )
        return dict(result)

    async def async_get_earned_trophies(self, communication_id) -> List[Achievement]:
        def trophy_parser(trophy) -> Achievement:
            return Achievement(
                achievement_id="{}_{}".format(communication_id, trophy["trophyId"]),
                achievement_name=str(trophy["trophyName"]),
                unlock_time=parse_timestamp(trophy["fromUser"]["earnedDate"])
            )

        def trophies_parser(response) -> List[Achievement]:
            return [
                trophy_parser(trophy) for trophy in response.get("trophies", [])
                if trophy.get("fromUser") and trophy["fromUser"].get("earned")
            ] if response else []

        return await self.fetch_data(trophies_parser, EARNED_TROPHIES_PAGE.format(
            communication_id=communication_id,
            trophy_group_id="all"))

    async def async_get_friends(self):
        def friend_info_parser(profile):

            avatar_url = None
            for avatar in profile["avatarUrls"]:
                avatar_url = avatar["avatarUrl"]

            return UserInfo(
                user_id=str(profile["accountId"]),
                user_name=str(profile["onlineId"]),
                avatar_url=avatar_url,
                profile_url=f"https://my.playstation.com/profile/{str(profile['onlineId'])}"
            )

        def friend_list_parser(response):
            logging.info(response)
            return [
                friend_info_parser(profile) for profile in response.get("profiles", [])
            ] if response else []

        return await self.fetch_paginated_data(
            friend_list_parser,
            FRIENDS_URL.format(user_id="me", avatar_size_list=DEFAULT_AVATAR_SIZE),
            "totalResults"
        )

    async def async_get_friends_presences(self):
        def friend_info_parser(profile):

            if profile["primaryOnlineStatus"] == "online":
                presence_state = PresenceState.Online
            else:
                presence_state = PresenceState.Offline

            game_title = game_id = None

            if "presences" in profile:
                for presence in profile["presences"]:
                    try:
                        if presence["onlineStatus"] == "online" and presence["platform"] == "PS4":
                            game_title = presence["titleName"]
                            game_id = presence["npTitleId"]
                    except:
                        continue

            return {profile['accountId']: UserPresence(presence_state, game_id, game_title)}

        def friends_with_presence_parser(response):
            return [
                friend_info_parser(profile) for profile in response.get("profiles", [])
            ] if response else []

        return await self.fetch_paginated_data(
            friends_with_presence_parser,
            FRIENDS_WITH_PRESENCE_URL.format(user_id="me"),
            "totalResults"
        )

    async def get_subscription_games(self) -> List[SubscriptionGame]:
        return await self.fetch_data(PSNGamesParser().parse, PSN_PLUS_SUBSCRIPTIONS_URL, get_json=False, silent=True)

    async def async_get_played_games(self):
        def get_games_parser(response):
            return response.get('titles', [])

        return await self.fetch_paginated_data(get_games_parser, PLAYED_GAME_LIST_URL.format(user_id="me"), 'totalItemCount')


def groupAnagrams(words):
    # Write your code here.
    result = []
    tempRes = []
    tempRes.append(words[0])
    i = 1
    while len(words) != 0:
        if isAnagram(words[0], words[i]):
            tempRes.append(words[i])
            words = words[:i] + words[i + 1:]
            i -= 1
        i += 1
        if i == len(words):
            result.append(tempRes[:])
            words = words[1:]
            i = 1
            tempRes = []
            if len(words) != 0:
                tempRes.append(words[0])
    return result

def isAnagram(str1, str2):
    if len(str1) != len(str2):
        return False
    dict1 = {}
    dict2 = {}
    i = 0
    while i < len(str1):
        if str1[i] in dict1.keys():
            dict1[str1[i]] += 1
        else:
            dict1[str1[i]] = 1
        if str2[i] in dict2.keys():
            dict2[str2[i]] += 1
        else:
            dict2[str2[i]] = 1
        i += 1
    if dict1 == dict2:
        return True
    return False

def groupAnagramAlgoMethod(words):
    sortedWords_LetterOnly = [''.join(sorted(w)) for w in words]
    indices = [i for i in range(len(words))]
    # sortedWords = sorted(sortedWords_LetterOnly)
    indices.sort(key = lambda x: sortedWords_LetterOnly[x])

print(groupAnagramAlgoMethod(["yo", "act", "flop", "tac", "cat", "oy", "olfp"]))


import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import math
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence

from .kirchhoff import KirchhoffNormalisation
from . import hinton

class Conv1d(nn.Module):
    """1D convolution layer."""

    def __init__(self, hidden_size, kernel_size, dilation=1):
        """Initialization.

        Args:
          hidden_size: dimension of input embeddings
          kernel_size: convolution kernel size
          dilation: the spacing between the kernel points
        """
        super(Conv1d, self).__init__()

        if kernel_size % 2 == 0:
            padding = (kernel_size // 2) * dilation
            self.shift = True
        else:
            padding = ((kernel_size - 1) // 2) * dilation
            self.shift = False
        self.conv = nn.Conv1d(
            hidden_size,
            hidden_size,
            kernel_size,
            padding=padding,
            dilation=dilation)

        self._reset_parameters()

    def _reset_parameters(self):
        init.xavier_uniform_(self.conv.weight)
        init.zeros_(self.conv.bias)

    def forward(self, x):
        """Compute convolution.

        Args:
          x: input embeddings
        Returns:
          conv_output: convolution results
        """

        if self.shift:
            return self.conv(x.transpose(1, 2)).transpose(1, 2)[:, 1:]
        else:
            return self.conv(x.transpose(1, 2)).transpose(1, 2)

class WeightedGNNLayer(nn.Module):
    def __init__(self,
                 hidden_dim, nrels,
                 dropout=0.1, dropatt=0.1):
        super(WeightedGNNLayer, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.nrels = nrels
        self.proj_nbrs = nn.Linear(hidden_dim, nrels * hidden_dim)
        self.in_proj = nn.Linear(hidden_dim, 3 * hidden_dim)
        self.out_proj = nn.Linear(hidden_dim, hidden_dim)

    def shrink(self, x):
        return x - torch.tanh(x)

    def forward(self, x, h_prev, attn_mask):
        batch_size, length, hdim = x.size()
        h_prev = self.dropout(h_prev)
        proj_h_prev = self.proj_nbrs(h_prev)
        proj_h_prev = F.layer_norm(
            proj_h_prev.view(batch_size, length, self.nrels, hdim),
            normalized_shape=(hdim,)
        )
        lin_sum = torch.einsum('bijh,bihd->bjd', attn_mask, proj_h_prev)
        shift, scale, proj_x = self.in_proj(x).chunk(3, dim=-1)
        h = F.relu(
            shift * F.layer_norm(
                proj_x + self.shrink(lin_sum),
                normalized_shape=(hdim,),
            ) + scale
        )
        y = torch.tanh(self.out_proj(h))
        # g = torch.sigmoid(lin_g)
        # y = g * torch.tanh(lin_y) + (1 - g) * torch.tanh(x)
        return y

class WeightedGNN(nn.Module):
    def __init__(self, input_size, hidden_size,
                 ntokens, padding_idx,
                 snip_start_end=False,
                 dropout=0.2, dropoutm=0.1,
                 parser_dropout=0.05,
                 parser_seq_processor='rnn',
                 prefix_attachment=False,
                 gumbel_noise=True):
        super(WeightedGNN, self).__init__()

        self.snip_start_end = snip_start_end
        self.padding_idx = padding_idx

        self.emb_syn = nn.Embedding(ntokens, input_size, padding_idx=self.padding_idx)
        self.emb_sem = nn.Embedding(ntokens, input_size, padding_idx=self.padding_idx)
        self.emb_aux = nn.Embedding(ntokens, input_size, padding_idx=self.padding_idx)

        self.parse = Parser(input_size, num_layers=2,
                            dropout=parser_dropout,
                            parser_seq_processor=parser_seq_processor,
                            prefix_attachment=prefix_attachment,
                            gumbel_noise=gumbel_noise)

        self.compute_layer = WeightedGNNLayer(hidden_dim=input_size, nrels=8,
                                              dropout=dropout, dropatt=0.1)
        # self.compute_layer = GraphBertLayer(hidden_dim=input_size, nrels=8,
        #                                     dropout=dropout, dropatt=0.1)
    def parse_only(self, input):
        A_rels, X_aux, X_sem, lengths, mask, parser_h, root_rels = \
            self.initial_processing(input)
        return A_rels

    def forward(self, input):
        A_rels, X_aux, X_sem, lengths, mask, \
        parser_h, root_rels, external_idxs = self.initial_processing(input)
        root = root_rels.sum(-1)
        prev_level = torch.zeros_like(X_sem)
        for i in range(root.size(1), 0, -1):
            # print((i > lengths).long())
            prev_level = self.compute_layer(X_sem, prev_level, attn_mask=A_rels)
            prev_level = prev_level.masked_fill((i > lengths)[:, None, None], 0.)
        # print(prev_level)
        # print(root)
        final_state = torch.einsum('bih,bi->bh', prev_level, root)
        flattened_internal = prev_level.transpose(1, 0)
        flattened_internal_mask = mask
        external_key = parser_h.transpose(1, 0)
        external_val = X_aux.transpose(1, 0)

        return final_state,\
            (flattened_internal,
             flattened_internal,
             flattened_internal_mask,
             external_key, external_val, external_idxs, mask)

    def initial_processing(self, input):
        max_length, batch_size = input.size()
        if self.snip_start_end:
            input = input.clone()
            input = input[1:]
            mask = (input != self.padding_idx)[1:]
            lengths = mask.sum(0)
            input[lengths, torch.arange(batch_size)] = self.padding_idx
            input = input[:-1]
        else:
            mask = input != self.padding_idx
            lengths = mask.sum(0)
        mask_ = mask.t()
        # X = self.embedding(input)
        # X_ = X.transpose(1, 0)
        # X_aux = X_.chunk(3, dim=-1)
        input_ = input.t()
        X_sem = self.emb_sem(input_)
        X_syn = self.emb_syn(input_)
        X_aux = self.emb_aux(input_)
        A_rels, root_rels, parser_h = self.parse(X_syn, mask_)
        return A_rels, X_aux, X_sem, lengths, mask, parser_h, root_rels, input

class RelativeEncodingScore(nn.Module):
    def __init__(self, input_size, output_size, dropout=0.1, max_len=500):
        super(RelativeEncodingScore, self).__init__()
        self.dropout = nn.Dropout(p=dropout)
        pe = torch.zeros(max_len * 2 + 1, input_size)
        position = -(torch.arange(max_len * 2 + 1) - max_len)
        div_term = torch.exp(torch.arange(0, input_size, 2, dtype=torch.float) *
                             (-math.log(10000.0) / input_size))

        vals = position[:, None] * div_term[None, :]
        pe[:, 0::2] = torch.sin(vals)
        pe[:, 1::2] = torch.cos(vals)
        # pe = position[:, None].repeat(1, d_model)
        self.register_buffer('pe', pe)
        self.max_len = max_len
        self.input_size = input_size
        self.output_size = output_size
        self.transform = nn.Linear(input_size, output_size)

    def forward(self, l):
        pos = self.transform(self.pe)
        relpos = pos.as_strided(
            (l, l, self.output_size),
            (self.output_size, self.output_size, 1),
            (self.max_len - l + 1) * self.output_size
        )[:, torch.arange(l - 1, -1, -1)]
        return relpos

class Parser(nn.Module):
    def __init__(self, input_size, num_layers=3, conv_size=3,
                 dropout=0.2,
                 parser_seq_processor='rnn',
                 prefix_attachment=True,
                 gumbel_noise=True):
        super(Parser, self).__init__()
        self.num_layers = num_layers

        self.bilinear_dim = 10
        self.rel_types = 8
        self.type = parser_seq_processor
        self.gumbel_noise = gumbel_noise

        self.parser_ff = nn.Sequential(
            nn.Linear(input_size, self.bilinear_dim),
        )

        self.relpos = RelativeEncodingScore(input_size, input_size // 4)
        self.parser_drop = nn.Dropout(dropout)
        self.parser_c = nn.Linear(input_size, input_size // 4)
        self.parser_p = nn.Linear(input_size, input_size // 4)
        self.parser_cumul = nn.Linear(input_size, input_size // 4)
        self.parser_act = nn.Sequential(
            nn.LayerNorm(input_size // 4),
            nn.Tanh(),
            self.parser_drop
        )
        self.parser_o = nn.Linear(input_size // 4, self.rel_types)
        nn.init.zeros_(self.parser_o.weight)
        nn.init.zeros_(self.parser_o.bias)

        self.relaxed_structure = KirchhoffNormalisation(
            dropout=0.01,
            smoothing_eps=0.,
            plus_one=False, tril=prefix_attachment
        )
        if self.type == 'conv':
            self.parser_layers = nn.ModuleList([
                nn.Sequential(
                    Conv1d(input_size, conv_size),
                    nn.LayerNorm(input_size, elementwise_affine=False),
                    nn.Tanh()
                ) for i in range(num_layers)])
        elif self.type == 'rnn':
            self.parser_layers = nn.LSTM(input_size, input_size // 2, num_layers,
                                         dropout=dropout, bidirectional=True)

    def forward(self, X, mask):
        lengths = mask.sum(1)
        visibility = mask[:, :, None] & mask[:, None, :]
        h = X

        cumul_h = F.pad(
            torch.cumsum(
                self.parser_cumul(h) \
                    .masked_fill(mask[..., None], 0.),
                dim=1
            ), (0, 0, 1, 0)
        )
        f_cumul_h = cumul_h[:, :-1]
        b_cumul_h = cumul_h[:, -1, None] - cumul_h[:, 1:]
        cumul_lin = (f_cumul_h[:, :, None] - b_cumul_h[:, None, :])

        if self.type == 'conv':
            for i in range(self.num_layers):
                h_in = h.masked_fill(~mask[:, :, None], 0)
                h = self.parser_layers[i](h_in)
                h = self.parser_drop(h)

        elif self.type == 'rnn':
            h = pack_padded_sequence(h, lengths.cpu(), enforce_sorted=False,
                                     batch_first=True)
            h, _ = self.parser_layers(h)
            h, _ = pad_packed_sequence(h, batch_first=True)
            h = self.parser_drop(h)

        c_lin = self.parser_c(h)
        p_lin = self.parser_p(h)

        hid = self.parser_act(
            c_lin[:, :, None] +
            p_lin[:, None, :] +
            cumul_lin +
            self.relpos(h.size(1))[None]
        )
        logits_rels = self.parser_o(hid)
        logits_rels = torch.tanh(logits_rels / 7.5) * 7.5

        if self.training and self.gumbel_noise:
            logits_rels = (
                logits_rels +
                -torch.log(
                    -torch.log(
                        torch.rand_like(logits_rels).clamp(min=1e-6)))
            )
        logits_rels = logits_rels.masked_fill(~visibility[..., None], -64)

        root_logits_rels = torch.diagonal(logits_rels, dim1=1, dim2=2).permute(0, 2, 1)
        p, p_root, entrpy = self.relaxed_structure.forward(
            head_dep_score=logits_rels,
            head_root_score=0 * root_logits_rels,
            mask=mask
        )
        print(hinton.plot(p[0].sum(-1).detach().cpu().numpy(), max_val=1.))
        return p, p_root, h

if __name__ == "__main__":
    length = 9
    x = torch.randint(low=1, high=20, size=(length, 5))
    x[-8:, 0] = 0
    x[-2:, 1] = 0
    x[-4:, 2] = 0
    mask = x != 0
    print(mask.long())

    gn = WeightedGNN(input_size=80, hidden_size=80, ntokens=20, padding_idx=0,
                     snip_start_end=False)
    gn(x)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-07-01 16:36:29
# @Author  : ${DAPTX} (${DAPTX4869@foxmail.com})
# @Link    : http://example.org
# @Version : $Id$

import os
import urllib.request
from bs4 import BeautifulSoup
import re
import time

url = 'http://www.hrb6.com/hrb6/15/15103/'
link= '3798930.html'
i=1
while True:
	f = urllib.request.urlopen(url+link)
	string = f.read().decode('ANSI','replace')
	Smain=BeautifulSoup(string,'html5lib').find('div',{'id':'novel_content'})
	writing=BeautifulSoup(str(Smain), 'html5lib').get_text()

	st=BeautifulSoup(string,'html5lib').find('h1',{'class':'novel_title'})
	title=BeautifulSoup(str(st), 'html5lib').get_text()

	la=BeautifulSoup(string,'html5lib').find('div',{'class':'novel_bottom'})
	las=BeautifulSoup(str(la), 'html5lib').find_all('a')
	last = BeautifulSoup(str(las), 'html5lib').get_text()
	link = las[2].get('href')

	ebook=open("book.txt","a+",encoding='utf-8')
	if last[13]=="二":
		ebook.write('\n\n')
		ebook.write(title)
		ebook.write('\n\n')
	ebook.write(writing)
	# ebook.write('\n\n')
	ebook.close()

	print("第",i,"章写入成功")

	i+=1
	time.sleep(1)
	if link == '3799268.html':
		print('over')
		break


#!/usr/bin/env python

import sys

#
# This program simply represents the identity function.
#

alpha = 0.85
iteration_limit = 10
iter_no = 0
n = 0 # number of nodes
graph = {}
rank_sum = 0

for line in sys.stdin:
    key_part, value_part = line.strip().split('\t')
    value_list = value_part.split(',')

    if key_part == '#':
        node_i = value_list[0]
        r = float(value_list[1])
        iter_no = int(value_list[2])
        if graph.get(node_i) is None:
            graph[node_i] = {'old': 0, 'new': 0, 'neighbors': []}
        graph[node_i]['old'] = r
        rank_sum += r
        n += 1
    else:
        node_i = key_part
        new_rank = float(value_list[0])
        if graph.get(node_i) is None:
            graph[node_i] = {'old': 0, 'new': 0, 'neighbors': []}
        graph[node_i]['new'] = new_rank
        neighbors = value_list[1:]
        for node_j in neighbors:
            if graph.get(node_j) is None:
                graph[node_j] = {'old': 0, 'new': 0, 'neighbors': []}
            graph[node_j]['neighbors'].append(node_i)

residual = (1 - alpha) / float(n) * rank_sum
for key, value in graph.iteritems():
    value['new'] = value['new'] * alpha + residual

if iter_no < iteration_limit:
    for node_i, value in graph.iteritems():
        old_rank = value['old']
        new_rank = value['new']
        outline = 'NodeId:%s:%d\t%f,%f' % (node_i, iter_no + 1, new_rank, old_rank)
        sys.stdout.write(outline)
        for node_j in value['neighbors']:
            sys.stdout.write(',%s' % node_j)
        sys.stdout.write('\n')
else:
    new_list = []
    for node, value in graph.iteritems():
        new_list.append([-value['new'], node])
    new_list.sort()
    for i in range(20):
        outline = 'FinalRank:%f\t%s\n' % (-new_list[i][0], new_list[i][1])
        sys.stdout.write(outline)


"""Monitors drone attitude values writes them to a redis database

Run a redis server on a computer attached to the network
    The redis server needs to be configured to accept connections from other
    computers
Note the ip address or hostname of the computer running the redis server
"""
from DroneControl import DroneComm
import redis
import sys

host = '192.168.0.165' # ip or hostname of computer running the redis server
r = redis.StrictRedis(host=host)
drone = DroneComm()

try:
    while (True):
        # Fetching current pitch/roll values
        roll = drone.get_roll()
        pitch = drone.get_pitch()
        yaw = drone.get_yaw()
        sys.stdout.write(
            "roll:%6.1f pitch:%5.1f yaw:%5.1f\r"%(roll, pitch, yaw))
        sys.stdout.flush()
        r.set('roll', roll)
        r.set('pitch', pitch)
        r.set('yaw', yaw)

except (KeyboardInterrupt, SystemExit):
    # Graceful Exit
    print("")
    drone.exit()


from logging.config import dictConfig
from os.path import expanduser

from flask import Flask
import yaml

from cloudlabs import (
    context_processors,
    sso,
    views,
    views_host
)
from cloudlabs.admin import views as admin
from cloudlabs.extensions import db, migrate

def create_app(config_name=None):
    """Build our Flask application with the given config source.

    If no config_name is given, we default to cloudlabs.config.Config,
    unless overridden by the environment variable APP_SETTINGS.
    """
    if config_name is None:
        import os
        config_name = os.getenv('APP_SETTINGS', 'cloudlabs.config.Config')
    app = Flask(__name__)
    app.config.from_object(config_name)
    # Configure logging
    with open(app.config["LOG_CONFIG_FILE"], "r") as config_file:
        config = yaml.safe_load(config_file.read())
        # The app may be launched by a different user (usually if running on
        # a server), but the configuration file cannot know that in advance.
        # It therefore uses "~" to refer to the user's home directory, which
        # we have to replace with the actual path, as the dictConfig method
        # will just use the path passed to it without doing any substitutions.
        # This currently only affects the "production" configuration, as in
        # the DevConfig the logging file is local to where to the code is
        # launched from (without using "~").
        filename = config["handlers"]["file_handler"]["filename"]
        config["handlers"]["file_handler"]["filename"] = expanduser(filename)
    dictConfig(config)
    db.init_app(app)
    migrate.init_app(app, db)
    context_processors.setup(app)
    app.register_blueprint(views.blueprint)
    app.register_blueprint(views_host.blueprint)
    app.register_blueprint(admin.blueprint, url_prefix='/admin')
    sso.setup_login(app)
    return app


import os
import numpy as np
import xml.etree.ElementTree as ET
from deg2str import *
from astropy import coordinates as coord
from astropy import units as u
from simbad import *
from datetime import *
from PIL import Image
from query_wsa_fits import *
from query_pso_fits import *
from jdcal import *
import pdb
import glob
import re
import astropy.coordinates as coord
import astropy.units as u

def psocolor(source_name,keepfiles=False,allcolor='#FFFF00',rejcolor='b',tm_color='r',plot=False,savepdf=True,secondary='',skipdownloads=False,circle_radius=0.0015,size=2.0,override_directory=None,primarypos_label=None,title=None,filename=None,buffer=False):
    # Set $FINDER_PATH in your bash_profile if you would like to control where the finder charts are output
    # size: arcmin
    # allwise: overplot AllWISE catalog positions
    # rejallwise = overplot AllWISE reject positions
    # tmass = overplot 2MASS psc positions
    # keepfiles = keep fits, tbl, and xml files
    # allcolor = color of allwise symbols
    # rejcolor = color of allwise reject symbols
    # tm_color = color of tmass symbols
    # plot = show plot (otherwise, finder is just made)
    # savepdf = save a pdf of the finder

    circle_width = 2.5
    circle_alpha = 0.8
    fig_xsize = 11
    fig_ysize = 8.5

    #Convert sexagecimal
    if source_name.find(' ') == -1:
    	symbol = '+'
    	if source_name.find('+') == -1:
    		symbol = '-'
    	radec = re.split('[\+ | -]',source_name)
    	ras = radec[0][0:2]+' '+radec[0][2:4]+' '+radec[0][4:]
    	decs = radec[1][0:2]+' '+radec[1][2:4]+' '+radec[1][4:]
    	ra = coord.Angle(ras, unit=u.hour)
    	dec = coord.Angle(symbol+decs,unit=u.degree)
    	source_name = str(ra.degree)+' '+str(dec.degree)

    #Use buffer if needed
    if buffer:
        import matplotlib
        matplotlib.use('Agg')
        plot=False
    import pylab,pyfits,aplpy

    #Verify whether a working directory is set in the bash profile
    main_dir = None
    if override_directory:
        main_dir = override_directory
    else:
        proc = subprocess.Popen(["echo $FINDER_PATH"], stdout=subprocess.PIPE,shell=True)
        (out, err) = proc.communicate()
        if out != '\n':
            main_dir = out.split('\n')[0]
    if main_dir:
        initial_dir = os.getcwd()
        if not os.path.exists(main_dir):
            os.makedirs(main_dir)
        os.chdir(main_dir)

    #List of colors
    color_blue = '#377eb8'#RGB=[55,126,184]
    color_red = '#e41a1c'#RGB=[228,26,28]
    color_purple = '#b27bba'#RGB=[178,123,186]
    color_green = '#4daf4a'#RGB = [77,175,74]

    t1 = datetime.now()

    ra,de = simbad(source_name)
    ra2 = None
    de2 = None
    if secondary:
        ra2,de2 = simbad(secondary)
    if filename is None:
        filename = source_name

    if skipdownloads is not True:
        print("Downloading Pan-Starrs data")
        #Remove previous data
        os.system("rm *_PSO_TMP.fits*")
        query_pso_fits(ra,de,size=size,output_file='PSO_TMP.fits')

    #If no PSO data could be downloaded, return
    if len(glob.glob('*_PSO_TMP.fits*')) == 0:
        print("No PSO data !")
        return

    if plot:
        pylab.ion()
    fig = pylab.figure(figsize=(fig_xsize,fig_ysize))
    pylab.rcParams['font.family'] = 'serif'

    #Create and plot RGB PSO image
    files = ['y_PSO_TMP.fits','i_PSO_TMP.fits','g_PSO_TMP.fits']
    aplpy.make_rgb_cube(files,'PSO_rgb.fits')
    impsoc = aplpy.FITSFigure('PSO_rgb_2d.fits',figure=fig)
    impsoc.add_label(0.05,0.9,'PSO $y$/$i$/$g$',relative=True,size='medium',color='k',bbox=dict(facecolor='white', alpha=0.5),horizontalalignment='left')

    meds = []
    mads = []
    devs = []
    for filei in files:
        datai = pyfits.getdata(filei)
        medi = np.nanmedian(datai)
        madi = np.nanmedian(abs(datai - np.nanmedian(datai)))
        devi = np.nanpercentile(datai,95) - np.nanpercentile(datai,5)
        meds.append(medi)
        mads.append(madi)
        devs.append(devi)

    mins = []
    maxs = []
    for i in range(0,len(files)):
        mini = (meds[i] - 2.0*mads[i])
        #maxi = (meds[i] + 10.0*mads[i])
        maxi = meds[i] + 2.0*devs[i]
        mins.append(mini)
        maxs.append(maxi)

    aplpy.make_rgb_image('PSO_rgb.fits','PSO_rgb.png',vmin_r=mins[0],vmin_g=mins[1],vmin_b=mins[2],vmax_r=maxs[0],vmax_g=maxs[1],vmax_b=maxs[2])

    impsoc.show_rgb('PSO_rgb.png')
    impsoc.hide_tick_labels()
    impsoc.ticks.hide()
    impsoc.hide_xaxis_label()
    impsoc.hide_yaxis_label()
    impsoc.recenter(ra,de,width=(size/60.0),height=(size/60.0))
    impsoc.show_circles(ra,de,edgecolor=color_red,linewidth=circle_width,facecolor='none',radius=circle_radius,alpha=circle_alpha)
    if secondary:
        impsoc.show_circles(ra2,de2,edgecolor=color_blue,linewidth=circle_width,facecolor='none',    radius=circle_radius,alpha=circle_alpha)

    # Remove files (or not)
    if keepfiles:
        pass
    else:
        print("Removing files...")
        cmdrm1 = "rm PSO_rgb.png PSO_rgb*.fits"
        os.system(cmdrm1)
    if savepdf:
        pylab.savefig(filename+'_pso_color.pdf')

    #Return to initial directory
    if main_dir:
        os.chdir(initial_dir)

    t2 = datetime.now()
    tdiff = (t2 - t1)
    print("PSO image creation took %s seconds" % (round(tdiff.total_seconds(),0)))