#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python 2.7
""" Calc rms for xyz signals from force-save.txt files, walks through folders
"""

### SCRIPT INFO
__author__ = 'Nick Chahley, https://github.com/pantsthecat/laser-tweezers'
__version__ = '0.1.2'
__day__ = '2018-05-22'
__codename__ = 'Nina Bonita Brown'

### Import Modules
import csv
import numpy as np
from glob import glob
import os
import sys
import pandas as pd
import datetime
import collections
import logging
import argparse
import scipy.signal

### Arguments
# I think defining arguments like this is still readable (if a bit cumbersome)
# and allows for a future in which scripts are called from the commandline (in
# a pipeline, or by .exe launchers), even though we are not doing this atm.
# --Nick
parser = argparse.ArgumentParser()

## Filter toggle and cutoff
parser.add_argument('-cl', '--cf_low', type=float, default=1.,
    help='Low cutoff freq for bandpass filter in Hz (def 1Hz)')
parser.add_argument('--nofilter', dest='filter_on', action='store_false',
                    help='Highpass filter off (default)')
parser.add_argument('--filter', dest='filter_on', action='store_true',
                    help='Highpass filter on')
parser.set_defaults(filter_on=False)

# Access an arg value by the syntax 'args.<argument_name>'
args = parser.parse_args()

### Function Defs
class CommentedFile:
    """ Skips all lines that start with commentstring
    """
    def __init__(self, f, commentstring="#"):
        self.f = f
        self.commentstring = commentstring
    def next(self):
        line = self.f.next()
        while line.startswith(self.commentstring):
            line = self.f.next()
        return line
    def __iter__(self):
        return self
def get_cwd():
    ### Check OS switch, this is a bad name b/c of similarity to os.getcwd()
    if sys.platform.lower().startswith('win'):
        cdir = os.getcwd().split('\\')[-1] # Windows
    else:
        cdir = os.getcwd().split('/')[-1] # *.nix
    return cdir
def dirsep():
    """Check OS and set appropriate dir separator
    """
    if sys.platform.lower().startswith('win'):
        dirsep = '\\'
    else:
        dirsep = '/'
    return dirsep
def read_forcesave(f, col=2):
    """ Reads NanoTracker force-save.txt
    Depends on: class CommentedFile
    In:
        f: force-save file to be opened
        col: column of f to be extracted, 2=z1, 5=z2
    Returns:
        sig: 1D time series signal
    """
    # TODO how to tell when we've reached end of file / next seg
    # Probs a more robust way to do this using with open() as :
    csv_file = csv.reader(CommentedFile(open(f, "rb")),
                          delimiter=' ')

    try:
        # This loop is to skip the Position Ramp segment, the next segment, 'Pause'
        # is preceeded by a blank line in the force-save-*.txt
        line = csv_file.next()
        while line: # While the next line is not blank
            line = csv_file.next()

        # Pull out the signal into a list of strings
        sig=[]
        for row in csv_file:
            try:
                sig.append(row[col])
            except IndexError:
                print("IndexError. Something is up here: ")
                print(row)

        # List of strings >> list of floats
        sig = map(float, sig)
        return sig
    except StopIteration:
        print("Problem reading %s" %f)
def get_params(f):
    """ Returns: t, fs, date (ndarray, float, string)
    """
    # TODO num segments -> num loops (only rel for old ramps)
    T = float(get_match_val(f, "settings.segment.1.duration:"))
    N = int(get_match_val(f, "settings.segment.1.num-points:"))
    fs = N/T
    dt = 1/fs
    t = np.arange(dt, T+dt, dt)
    date = get_match_val(f, "bead-id:")
    date = date.split('-')[0] # date is at front of bead-id string
    return t, fs, date

def path_dialog_no_p(whatyouwant):
    """ Prompt user to select a dir (def) or file, return its path
    In
    ---
    whatyouwant : str opts=['folder', 'file']
    """
    import Tkinter
    root = Tkinter.Tk()
    root.withdraw()

    opt = {}
    opt['parent'] = root
    opt['initialdir'] = './'

    if whatyouwant == 'folder':
        from tkFileDialog import askdirectory
        ask_fun = askdirectory
        # dirpath will be to dir that user IS IN when they click confirm
        opt['title'] = 'Please select your experiment directory (be IN this folder)'

    if whatyouwant == 'file':
        from tkFileDialog import askopenfilename
        ask_fun = askopenfilename
        opt['title'] = 'Select psd file to detect peaks from'
        opt['filetypes'] = (('CSV files', '*.csv'), ('All files', '*.*'))

    path = ask_fun(**opt)
    return path

#-------------------------------------------------
### Imported Functions from fftz
## - General logging, param storage/find and headers
# Paramater and Logger functions are all intertwined and interdependent, sry
def path_dialog(whatyouwant):
    """ Prompt user to select a dir (def) or file, return its path

    In
    ---
    whatyouwant : str opts=['folder', 'file']

    Out
    ---
    path : str, Absolute path to file

    """
    import Tkinter
    root = Tkinter.Tk()
    root.withdraw()

    opt = {}
    opt['parent'] = root
    opt['initialdir'] = './'

    if whatyouwant == 'folder':
        from tkFileDialog import askdirectory
        ask_fun = askdirectory
        # dirpath will be to dir that user IS IN when they click confirm
        opt['title'] = 'Please select your experiment directory (be IN this folder)'

    if whatyouwant == 'file':
        from tkFileDialog import askopenfilename
        ask_fun = askopenfilename
        opt['title'] = 'Select psd file to detect peaks from'
        opt['filetypes'] = (('CSV files', '*.csv'), ('All files', '*.*'))

    path = ask_fun(**opt)
    return path
def header_get_params(f, p, kind='rms'):
    """ Where params is a dict containing info/settings

    Issues:
    - date could be different across forcesave files
    """

    logger.info("Read params from file %s" %f)

    # TODO exception handling for TypeError
    T = float(get_match_val(f, "settings.segment.1.duration:")) # (s)
    N = int(get_match_val(f, "settings.segment.1.num-points:"))
    fs = N/T
    dt = 1/fs
    ## please don't let this appear on r/shittyprogramming
    p['T'] = T
    p['N'] = N
    p['fs'] = fs

    ## Don't need to calculate these things if rms, but is it even worthwile
    # to make a diff case for rms to *not* calc them?
    if kind == 'psd':
        p['dt'] = dt
        p['t'] = np.arange(dt, T+dt, dt) # generate time-points for signal (s)
        p['freq'] = freq_calc(len(p['t']), p['fs'])

    id_list = ['bead-id:', 'approachID:']
    date = get_match_val(f, id_list)
    # date is at front of id string ~ "yyyy.mm.dd-hh.mm.ss-*"
    p['date'] = date.split('-')[0]

    return p
def walk_get_params(path, p):
    """ Read scan params from first force-save encountered and break, assumes
    all files have the same parameters
    This is a quick fix, and probably bad form but should do alright in a pinch

    "2": uses dict p to store vars
    """
    success = False
    for subdir, dirs, files in os.walk(path):
        if success == False:
            os.chdir(subdir)
            infile = glob('force-save*.txt')
            if len(infile) > 0:
                logger.info('Reading Scan Parameters from dir: %s' %subdir)
                p = header_get_params(infile[0], p, kind='rms')
                success == True
                break
    return p
def get_match_val(f, match):
    search = open(f, "r")

    # This ifel is so that we can arbitrarily search for both 'bead-id'
    # (optical trap) and 'approachID' (AFM) -- or more options if need be
    if type(match) == list:
        for line in search.readlines():
            if any(x in line for x in match):
                return line.split()[-1]
                break
    else: # Assume that match is a string
        for line in search.readlines():
            if match in line:
                return line.split()[-1]
                break
def detect_forcesave_type(path):
    """ Returns a dict of optical/afm and a bool indicating if the file read
        is of that type. Walks through directory tree, terminates at first
        successful identification.
    """

    # Use this dict to store bools for afm vs optical trap ft id
    # global fs_type
    fs_type = {'optical' : False,
               'afm' : False}
    fs_type_matches = {'optical' : '# bead-id',
                       'afm' : '# approachID'}

    success = False
    for subdir, dirs, files in os.walk(path):
        if success == False:
            os.chdir(subdir)
            infile = glob('force-save*.txt')
            if len(infile) > 0:
                logger.info('Infering force-save type from dir: %s\n file: %s'\
                      %(subdir,infile[0]))

                search = open(infile[0], 'r')

                for line in search.readlines():
                    for key in fs_type_matches.keys():
                        if line.split(':')[0] == fs_type_matches[key]:
                            fs_type[key] = True
                            success = True
                            break
                    if success == True:
                        break

    true_type = [t for t in fs_type.keys() if fs_type[t] == True]
    ## Overbearing mother sanity check
    if len(true_type) == 1:
        logger.info('Found force-save type: %s' %true_type[0])
    else:
        logger.warning('Found multiple force-save types?? : %s' %true_type)

    return fs_type
def premain_setup():
    """ Define params dict p, and setup logger
    """
    # dict to store settings, params and pass 'em to others
    # global p
    p = {}
    p['rootpath'] = path_dialog('folder') # user selects starting folder (QT)

    logger = logger_setup(p) # need rootpath to set logfilename
    logger.info("Version %s (%s) -- \"%s\"" \
                 %(__version__, __day__, __codename__) )
    now = datetime.datetime.now()
    daterun = now.strftime('%Y-%m-%d')
    logger.info('Today is %s' % daterun)
    logger.info('Path to selected experiment folder: %s' %p['rootpath'])

    return p, logger
def logger_setup(p):
    """ Basic setup for crash logger
    """
    logfile = '/'.join((p['rootpath'], os.path.basename(__file__) ))
    logfile = logfile + '.log'
    datefmt = '%H:%M:%S'
    logfmt = '%(asctime)s %(levelname)-8s %(message)s'
    logging.basicConfig(filename=logfile, level=logging.DEBUG,
                        filemode='w', # overwrite log file if exists
                        format=logfmt, datefmt=datefmt)

    ## Console Handler
    ## Have logger print to stdout as well as log file
    ch = logging.StreamHandler(sys.stdout)
    chfmt = logging.Formatter('%(asctime)s: %(message)s', datefmt)
    ch.setFormatter(chfmt)
    # logger.getLogger().addHandler(ch)

    ## !! new testing
    ## boilerplate, "allows per module configuration"
    logger = logging.getLogger(__name__)
    logger.addHandler(ch)

    return logger
## - Highpass Filter
def butter_highpass(highcut, fs, order=3):
    """ Design a digital highpass Butterworth filter, return filter coefficents
    highcut, fs in Hz
    """
    nyq = 0.5 * fs
    norm_highcut = highcut / nyq
    b, a = scipy.signal.butter(order, norm_highcut, btype='high')
    return b, a
def butter_highpass_filter(data, highcut, fs, order=3):
    """ Return Butterworth HP filtered time series
    highcut, fs in Hz
    from scipy cookbook
    """
    b, a = butter_highpass(highcut, fs, order=order)
    y = scipy.signal.lfilter(b, a, data)
    return y

### New Funs for v 0.1x
def dict_to_df_rms(d, cnames):
    df = pd.DataFrame(d.items(), columns = cnames)
    df = df.sort_values(cnames[0]) # sort by scan name
    return df
def scan_transformation_rms(infile, scan_name, output_dict, col=2,
                            filter_hp=False, export_signal=False):
    """ Run the above functions and store psd for each scan in a dict (psd_d)
        Also store extracted raw signal in a dict

        channel/sensor codes: 0,1,2 = x,y,z
    """
    if len(infile) > 0: # if there is at least 1 force-save*.txt
        colnames = []
        infile.sort()
        if export_signal == True:
            sig_d = {}
            for i in range(len(infile)): # for each force-save*.txt
                colnames.append(''.join((scan_name, '-', str(i+1))))
                sig = read_forcesave(infile[i], col=col)
                sig = sig - np.mean(sig)

                ## Run highpass filter, if asked
                if filter_hp == True:
                    sig = butter_highpass_filter(sig, highcut=args.cf_low,
                                                 fs=p['fs'], order=3)

                rms = rms_calc(sig)
                output_dict[colnames[i]] = rms
                sig_d[colnames[i]] = sig
                logger.info(' '.join(("Finished:", colnames[i])))
            return output_dict, sig_d

        else:
            for i in range(len(infile)): # for each force-save*.txt
                colnames.append(''.join((scan_name, '-', str(i+1))))
                sig = read_forcesave(infile[i], col=col)

                ## Run highpass filter, if asked
                if filter_hp == True:
                    sig = butter_highpass_filter(sig, highcut=args.cf_low,
                                                 fs=p['fs'], order=3)
                rms = rms_calc(sig)
                output_dict[colnames[i]] = rms
                logger.info(' '.join(("Finished:", colnames[i])))
            return output_dict
def popup_message(text = '', title='Message'):
    try:
        # Python 3.x imports
        import tkinter as tk
        from tkinter import messagebox
    except ImportError:
        # Fall back to 2.x
        import Tkinter as tk
        import tkMessageBox as messagebox

    root = tk.Tk().withdraw()  # hide the root window

    messagebox.showinfo(title, text)  # show the messagebox
def rms_calc(sig):
    rms = np.sqrt(np.mean(abs(sig)**2))
    return rms
def main_rms_run_basic(p, filter_hp=False, export_signal=False):
    rms_d = {}

    # rootpath already defined in premain_setup()
    p = walk_get_params(p['rootpath'], p)

    # report to logger b/c who knows, a crash could be anywhere, anything
    logger.info('Highpass filter : %s' %filter_hp)

    # Assume the first force save file encountered is the same type (optical
    # trap / afm) as all other files involved in this run.
    # Should do this step in walk_get_params above, but w/e, legacy
    p['forcesave_type'] = detect_forcesave_type(p['rootpath'])

    ## Get the rms
    for subdir, dirs, files in os.walk(p['rootpath']):
        os.chdir(subdir)
        scan_name = get_cwd()
        logger.info(' '.join(("Entering", scan_name)))
        if export_signal == True:
            rms_d, sig_d = scan_transformation_rms(glob('force-save*.txt'), scan_name,
                                            rms_d, filter_hp=filter_hp,
                                            export_signal=True)
        else: # Regular/default b/h
            rms_d = scan_transformation_rms(glob('force-save*.txt'), scan_name,
                                            rms_d, filter_hp=filter_hp)

    ## Output csv
    # TODO probs better if df has cols something like ['scan_name',
    # 'scan_number', 'rms', 'mean_rms']
    def export(d, dv):
        """Hack in signal output for comparison test"""
        df = dict_to_df_rms(d, cnames=['Scan_Name', dv])
        rootpathname = '/'.join((p['rootpath'], p['rootpath'].split('/')[-1]))
        outfile = '_'.join((rootpathname, dv.lower() + '.csv'))
        logger.info('Exporting %s csv' %dv)
        df.to_csv(outfile, index=False)

    # We want to note on outfile (and output csv?) if filter was used
    dv = 'RMS_filtered' if filter_hp == True else 'RMS'
    df = dict_to_df_rms(rms_d, cnames=['Scan_Name', dv])
    rootpathname = '/'.join((p['rootpath'], p['rootpath'].split('/')[-1]))
    outfile = '_'.join((rootpathname, dv.lower() + '.csv'))
    logger.info('Exporting %s csv' %dv)
    df.to_csv(outfile, index=False)

    if export_signal == True:
        dv = 'sig_filtered' if filter_hp == True else 'sig'
        df = dict_to_df_rms(sig_d, cnames=['Scan_Name', dv])
        rootpathname = '/'.join((p['rootpath'], p['rootpath'].split('/')[-1]))
        outfile = '_'.join((rootpathname, dv.lower() + '.csv'))
        logger.info('Exporting %s csv' %dv)
        df.to_csv(outfile, index=False)

def main(p):
    """ Main function to log in case of crash.
    Not an example elegant programming.
    """
    main_rms_run_basic(p, filter_hp=args.filter_on,
                       export_signal=args.export_signal)

#-------------------------------------------------

## log in case main function crashes
if __name__ == "__main__":
    p, logger = premain_setup()
    try:
        main(p)
        logger.info("YOU ARE ALL FREE NOW")

    except Exception as e:
        logger.exception("Main crashed. Error: %s", e)
        popup_message("Main crashed. Error: %s" %e, title='BAD END')


# Runner script for all modules
from load_data import load_sensor_data
from house_info import HouseInfo
from datetime import datetime, date
from temperature_info import TemperatureData

##############################
# Do not remove these two lines
# They are needed to validate your unittest
data = []
print("Sensor Data App")
##############################

# Module 1 code here:
data = load_sensor_data()
print("Loaded records: {}".format(len(data)))

# Module 2 code here:
house_info = HouseInfo(data)
test_area = 1
recs = house_info.get_data_by_area("id", rec_area=test_area)
print("\nHouse sensor records for area {} = {}".format(test_area, len(recs)))

test_date = datetime.strptime("5/9/20", "%m/%d/%y")
recs = house_info.get_data_by_date("id", rec_date=test_date)
print("\nHouse sensor records for date: {} = {}".format(test_date.strftime("%m/%d/%y"), len(recs)))

# Module 3 code here:
temperature_data = TemperatureData(data)
recs = temperature_data.get_data_by_area(rec_area=test_area)
print("\nHouse Temperature sensor records for area {} = {}".format(test_area, len(recs)))
print("\tMaximum: {0}, Minimum: {1} temperatures".format(max(recs), min(recs)))

recs = temperature_data.get_data_by_date(rec_date=test_date)
print("\nHouse Temperature sensor records for date: {} = {}".format(
    test_date.strftime("%m/%d/%y"), len(recs)))
print("\tMaximum: {0}, Minimum: {1} temperatures".format(max(recs), min(recs)))
# Module 4 code here:

# Module 5 code here:


# encoding: utf-8
"""
【程序36】
题目：求100之内的素数
1.程序分析：
2.程序源代码：
"""
import math
cnt = 0
for i in range(2, 101):
    flag = False
    for x in range(2, math.floor(math.sqrt(i)) + 1):
        if i % x == 0:
            flag = True
    if not flag:
        print(i)
        cnt += 1

print('total :', cnt)


from copy import deepcopy

def imprimir(x):
    print("")
    for i in range(len(x)):
        print("x"+str(i+1)+" = "+str(x[i]))

def solucoes(n, c, cp, potencia):
    raiz = 2**potencia
    x = []
    inv = c[::-1]
    for i in range(len(cp)-1):
        x.append((cp[i+1]/cp[i])**(1/raiz))
    for i in range(len(x)):
        s = 0
        for j in range(len(c)):
            if j == 0:
                s+=inv[j]
            else:
                s+=inv[j]*x[i]**j
        if round(s)!=0:
            x[i]=(-1)*x[i]
    return x

def coeficientes_potencia(c, potencia):
    cp = deepcopy(c)
    for t in range(potencia):
        if t == 0:
            pass
        else:
            c = deepcopy(cp)
        for i in range(len(c)):
            if i == 0 or i == len(c)-1:
                cp[i] = cp[i]**2
            else:
                p = 0
                if i<=len(c)-2-i: #mais próximo do termo de maior grau
                    for j in range(1, i+1):
                        p+=((-1)**j)*(2*c[i-j]*c[i+j])
                else:
                    for j in range(1, len(c)-i):
                        p+=((-1)**j)*(2*c[i-j]*c[i+j])
                cp[i] = c[i]**2+p
    return cp

def coeficientes_polinomio(n):
    c = []
    for i in range(n+1):
        c.append(float(input("Write the term a"+str(i)+": ")))
    return c

def main():
    c = []
    cp  = []
    x = []

    n = int(input("Write the polynomial degree: "))
    potencia = int(input("Write the power of 2: "))
    potencia = int(potencia**(1/2))
    c = coeficientes_polinomio(n)
    cp = coeficientes_potencia(c, potencia)
    x = solucoes(n, c, cp, potencia)
    imprimir(x)
main()


from game1.fight import *
game = game1.fight

class user:
    playlist = []
    money = 50
    def __int__(name,age):
        self.name = name
        self.age = age
    def play(self, game):
        python(game)
    def buy_game(self,a):
        if user.age < a.limit:
            print("limitation was set for this game.")
        elif user.money < a.price:
            print("Not enough money left")
        else:
            self.playlist.append[a]

class game:
    def __int__(price,limit):
        self.price = price
        self.limit = limit

GTA = game(100,18)
Jack = user("Jack",17)


import bisect

class Solution:
    def minOperations(self, nums) -> int:
        a, n = sorted(set(nums)), len(nums)
        res = n
        for i, x in enumerate(a):
            _max = x + n - 1
            idx = bisect.bisect_right(a, _max)
            # n - (idx - 1 - i + 1)
            res = min(res, n - (idx - i))
        return res

s = Solution()
print(s.minOperations([4, 5, 8, 8, 9, 9]))
print(s.minOperations([1, 2, 3, 5, 6]))


import json
from purviewcli.common import http_get

def getGlossary(config, args):
    glossaryGuid = '' if args['--glossaryGuid'] is None else args['--glossaryGuid']
    endpoint = '/api/atlas/v2/glossary/%s' % glossaryGuid
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryCategories(config, args):
    endpoint = '/api/atlas/v2/glossary/%s/categories' % args['--glossaryGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryCategoriesHeaders(config, args):
    endpoint = '/api/atlas/v2/glossary/%s/categories/headers' % args['--glossaryGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryCategory(config, args):
    endpoint = '/api/atlas/v2/glossary/category/%s' % args['--categoryGuid']
    data = http_get('catalog', 'GET', endpoint, None, None, config)
    return data

def getGlossaryCategoryRelated(config, args):
    endpoint = '/api/atlas/v2/glossary/category/%s/related' % args['--categoryGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryCategoryTerms(config, args):
    endpoint = '/api/atlas/v2/glossary/category/%s/terms' % args['--categoryGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryDetailed(config, args):
    endpoint = '/api/atlas/v2/glossary/%s/detailed' % args['--glossaryGuid']
    data = http_get('catalog', 'GET', endpoint, None, None, config)
    return data

def getGlossaryTerm(config, args):
    endpoint = '/api/atlas/v2/glossary/term/%s' % args['--termGuid']
    data = http_get('catalog', 'GET', endpoint, None, None, config)
    return data

def getGlossaryTerms(config, args):
    endpoint = '/api/atlas/v2/glossary/%s/terms' % args['--glossaryGuid']
    data = http_get('catalog', 'GET', endpoint, None, None, config)
    return data

def getGlossaryTermsAssignedEntities(config, args):
    endpoint = '/api/atlas/v2/glossary/terms/%s/assignedEntities' % args['--termGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryTermsHeaders(config, args):
    endpoint = '/api/atlas/v2/glossary/%s/terms/headers' % args['--glossaryGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def getGlossaryTermsRelated(config, args):
    endpoint = '/api/atlas/v2/glossary/terms/%s/related' % args['--termGuid']
    params = {'limit': args['--limit'], 'offset': args['--offset'], 'sort': args['--sort']}
    data = http_get('catalog', 'GET', endpoint, params, None, config)
    return data

def newGlossaryTerm(config, args):
    glossary = getGlossary(config, args)
    glossary_guid = json.loads(glossary)[0]['guid']
    endpoint = '/api/atlas/v2/glossary/term'
    payload = {
        "name": args['--termName'],
        "anchor":{"glossaryGuid": glossary_guid},
        "status": args['--termStatus'],
        "longDescription": args['--termDescription'],
        "abbreviation": args['--termAcronym'],
        "synonyms":[],
        "seeAlso":[],
        "attributes":{}
    }
    for termGuid in args['--synonym']:
        payload['synonyms'].append({'termGuid': termGuid})

    for termGuid in args['--related']:
        payload['seeAlso'].append({'termGuid': termGuid})

    data = http_get('catalog', 'POST', endpoint, None, payload, config)
    return data

# {
#     "name":"Another Term",
#     "anchor":{"glossaryGuid":"505674a3-d8fc-4234-9038-e313829a7216"},
#     "status":"Alert",
#     "longDescription":"This is the term definition.",
#     "abbreviation":"acro1, acro2, acro3",
#     "resources":[{"displayName":"resource1","url":"https://google.com"}],
#     "synonyms":[{"termGuid":"a6bf5214-91d8-4c04-bdfc-d171bfee6eaa"},{"termGuid":"f87e4915-470a-43ac-a801-db245aa65212"}],
#     "seeAlso":[{"termGuid":"6cbfe86d-bbac-4823-a9df-d354528c99ad"}],
#     "contacts":{"Expert":[{"id":"095354ff-cae8-44ff-8120-22ec5a941b40","info":"Information here."}],"Steward":[{"id":"095354ff-cae8-44ff-8120-22ec5a941b40","info":"More information."}]},
#     "attributes":{"New Term Template":{"Employee ID":"1234"}}}


from rest_framework import routers, serializers, viewsets
from rest_framework.pagination import *
from .models import *
from .serialisers import *

class DataSetPaging(LimitOffsetPagination):
    max_limit = 20  # 最大限制默认是None
    default_limit = 10  # 设置每一页显示多少条
    limit_query_param = "limit"  # 往后取几条
    offset_query_param = "offset"  # 当前所在的位置

class OpLogViewset(viewsets.ModelViewSet):
    queryset = OpLog.objects.order_by("-id").all()
    serializer_class = OpLogSlz
    pagination_class = DataSetPaging

class UploadedImageViewset(viewsets.ModelViewSet):
    queryset = UploadImage.objects.order_by("-id").all()
    serializer_class = UploadedImageSlz
    pagination_class = DataSetPaging


#!/usr/bin/env python2.7

""" Script for sorting and transfer of downloaded read files. """

import os
import re
import shutil
from os.path import expanduser
from pdrMain import createLogger
from ConfigParser import SafeConfigParser

__author__  = "Sung Im"
__email__   = "wla9@cdc.gov"
__version__ = "2.0"

# Regular expression dictionaries for validation files.
vCampyDict = ["^V.+(PNUSA[C]\d{6}).+",    # V-PNUSAC001111*
              "^(PNUSA[C]\d{6})[V].+",    # PNUSAC000268V*
              "^\d{4}[D]-\d{4}[V].+",     # 2015D-0017V*
              "^PNUSA[C]\d{6}\-[V].+",    # PNUSAC001553-V*
              ]

vLmoDict = ["^V.+(PNUSA[L]).+",           # V-PNUSAL001627*
            "^(PNUSA[L]\d{6})[V].+",      # PNUSAL001204V*
            "^\d{4}[L]-\d{4}[V].+",       # 2015L-6529*
            "^PNUSA[L]\d{6}\-[V].+",      # PNUSAL001553-V*
            ]

vSalmDict = ["^V.+(PNUSA[S]\d{6}).+",     # V-PNUSAS001062*
             "^(PNUSA[S]\d{6})[V].+",     # PNUSAS001546V*
             "^\d{4}[K]-\d{4}[V].+",      # 2014K-0443V*
             "^PNUSA[S]\d{6}\-[V].+",     # PNUSAS001553-V*
             ]

vStecDict = ["^V.+(PNUSA[E]).+",          # V-PNUSAE001553*
             "^(PNUSA[E]\d{6})[V].+",     # PNUSAE001553V*
             "^\d{4}[C|EL]-\d{4}[V].+",   # 2014C-3642V*
             "^PNUSA[E]\d{6}\-[V].+",     # PNUSAE001553-V*
             ]

vVibrDict = ["^V.+(PNUSA[V]).+",          # V-PNUSAE001222*
             "^(PNUSA[V]\d{6})[V].+",     # PNUSAV001222V*
             "^\d{4}[V]-\d{4}[V].+",      # 2016V-1222V*
             "^PNUSA[V]\d{6}\-[V].+",     # PNUSAV001222-V*
             ]

def teleportFile(storagePath, destPath, file, organism, counter, log):
    """ Build destination file path based on file name and move.

    counter values = {
                        1 : main storage
                        2 : state certification storage
                        3 : state validation storage
                        4 : temporary storage
                     }
    """
    # Staging sub-paths extensions.
    stateCert = "State_Certifications/"
    stateValid = "State_Validations/"

    source = storagePath + file
    organismHomePath = destPath + organism
    destinationFile = organismHomePath + file
    stateCertPath = organismHomePath + stateCert
    stateCertFile = stateCertPath + file
    stateValidPath = organismHomePath + stateValid
    stateValidFile = stateValidPath + file

    if counter == 1:
        if os.path.isfile(destinationFile):
            log.info("{} already exists. Moving onto next file.".format(file))
            pass
        else:
            shutil.move(source, organismHomePath)
    elif counter == 2:
        if os.path.isfile(stateCertFile):
            log.info("{} already exists. Moving onto next file.".format(file))
            pass
        else:
            shutil.move(source, stateCertPath)
    elif counter == 3:
        if os.path.isfile(stateValidFile):
            log.info("{} already exists. Moving onto next file.".format(file))
            pass
        else:
            shutil.move(source, stateValidPath)
    elif counter == 4:
        shutil.move(source, organismHomePath)

def matchAndStage(storagePath, destPath, log):
    """ First pass matching for surveillance and certification files.

    Matches only PNUSAxxxxxxx id's and certification-strain labels. Move all
    else to tmp folder for more stringent matching.

    counter values = {
                        1 : main storage
                        2 : state certification storage
                        3 : state validation storage
                        4 : temporary storage
                     }
    """
    # Base organism directories.
    campy = "Campy/"
    lmo = "LMO/"
    salm = "Salm/"
    stec = "STEC/"
    temp = "tmp/"
    vibrio = "Vibrio/"

    for file in os.listdir(storagePath):

        # Campylobacter spp.
        if re.search("(^PNUSAC\d{6})[^V].+", file):
            log.info("{} is going to CAMPY STORAGE.".format(file))
            counter = 1
            teleportFile(storagePath, destPath, file, campy, counter, log)
        elif re.search("D5480", file):
            log.info("{} is going to CAMPY CERTIFICATION.".format(file))
            counter = 2
            teleportFile(storagePath, destPath, file, campy, counter, log)

        # Listeria spp.
        elif re.search("(^PNUSAL\d{6})[^V].+", file):
            log.info("{} is going to LISTERIA STORAGE.".format(file))
            counter = 1
            teleportFile(storagePath, destPath, file, lmo, counter, log)
        elif re.search("H8394", file):
            log.info("{} is going to LISTERIA CERTIFICATION.".format(file))
            counter = 2
            teleportFile(storagePath, destPath, file, lmo, counter, log)

        # Salmonella spp.
        elif re.search("(^PNUSAS\d{6})[^V].+", file):
            log.info("{} is going to SALMONELLA STORAGE.".format(file))
            counter = 1
            teleportFile(storagePath, destPath, file, salm, counter, log)
        elif re.search("C6472", file):
            log.info("{} is going to SALMONELLA CERTIFICATION.".format(file))
            counter = 2
            teleportFile(storagePath, destPath, file, salm, counter, log)

        # Escherichia spp.
        elif re.search("(^PNUSAE\d{6})[^V].+", file):
            log.info("{} is going to ESCHERICHIA STORAGE.".format(file))
            counter = 1
            teleportFile(storagePath, destPath, file, stec, counter, log)
        elif re.search("CDC03-98", file):
            log.info("{} is going to ESCHERICHIA CERTIFICATION.".format(file))
            counter = 2
            teleportFile(storagePath, destPath, file, stec, counter, log)

        # Vibrio spp.
        elif re.search("(^PNUSAV\d{6})[^V].+", file):
            log.info("{} is going to VIBRIO STORAGE.".format(file))
            counter = 1
            teleportFile(storagePath, destPath, file, vibrio, counter, log)
        # elif re.search("VIBRIO", file):
        #     log.info("{} is going to VIBRIO CERTIFICATION.".format(file))
        #     counter = 2
        #     teleportFile(storagePath, destPath, file, vibrio, counter, log)

        # Everything else.
        else:
            counter = 4
            log.info("{} is going to CALC_ENGINE/TMP.".format(file))
            teleportFile(storagePath, destPath, file, temp, counter, log)

def beamValidFiles(file, dict, storagePath, destPath, organism, log):
    """ Match & sort validation files quarantined in top level tmp directory.

    """
    for pattern in dict:
        if re.search(pattern, file):
            log.info("{0} is going to {1} VALIDATION.".format(file, organism))
            counter = 3
            teleportFile(storagePath, destPath, file, organism, counter, log)

if __name__ == "__main__":

    # Capture the root directory of current user.
    user_root = expanduser("~")

    # Create parser object.
    parser = SafeConfigParser()
    parser.read("{}/PycharmProjects/PulseNetDataRescue/conf.ini"
                .format(user_root)
                )

    # Import log and data storage location absolute paths.
    logPath = parser.get("logging", "log_path").strip('"')
    storagePath = parser.get("data_storage", "storage_path").strip('"')
    destPath = parser.get("data_storage", "destination_path").strip('"')
    tempPath = parser.get("data_storage", "temp_storage_path").strip('"')
    aspenLogin = parser.get("handshake_lotion", "aspen_login").strip('"')
    isilonPath = parser.get("handshake_lotion", "isilon_path").strip('"')

    # Initialise Python logging.
    log = createLogger(logPath)

    # Base organism directories as global variables.
    campy = "Campy/"
    lmo = "LMO/"
    salm = "Salm/"
    stec = "STEC/"
    temp = "tmp/"
    vibrio = "Vibrio/"

    if not os.listdir(storagePath):
        log.info("There are no files to teleport today.")
        # exit()
    else:
        matchAndStage(storagePath, destPath, log)

    if not os.listdir(tempPath):
        log.info("There are no validation files to beam.")
        # exit()
    else:
        for file in os.listdir(tempPath):
            beamValidFiles(file, vCampyDict, tempPath, destPath, campy, log)
            beamValidFiles(file, vLmoDict, tempPath, destPath, lmo, log)
            beamValidFiles(file, vSalmDict, tempPath, destPath, salm, log)
            beamValidFiles(file, vStecDict, tempPath, destPath, stec, log)
            beamValidFiles(file, vVibrDict, tempPath, destPath, vibrio, log)

    # Use rsync to transfer files from monolith0 to isilon.
    # import subprocess
    # ps = subprocess.Popen(("rsync",
    #                        "--log-file="+logPath+"pdrEvents.log",
    #                        "--remove-source-files",
    #                        "--stats",
    #                        "--update",
    #                        "-av",
    #                        destPath,
    #                        aspenLogin+":"+isilonPath,
    #                        ),
    #
    #                       stdout=subprocess.PIPE
    #                       )
    #
    # output = subprocess.check_output(("mail",
    #                                   "-s",
    #                                   "PulseNet Data Rescue",
    #                                   "wla9@cdc.gov",
    #                                   ),
    #
    #                                  stdin=ps.stdout,
    #                                  )


# Usage
"""
	python visualize.py -func 1MAX -val MRPS

"""

import argparse
import os
from matplotlib import pyplot as plt
import numpy as np
import math

# Load data from directory
def load_data_from_directory(path):

	entire_data = dict()
	subpaths = os.listdir(path)

	for subpath in subpaths:

		data = []
		problem_sizes = []

		file_paths = os.listdir(os.path.join(path, subpath))
		for file in file_paths:

			# Get the problem size via file name
			pro_size = int(file.split(".")[0])
			problem_sizes.append(pro_size)

			# Read data from the file
			with open(os.path.join(path, subpath, file), 'rb') as f:
				data_pro_size = np.load(f)

			data.append(data_pro_size)

		entire_data[subpath] = data

	return entire_data, problem_sizes

def process_data(entire_data):

	processed_data = dict()

	for crossover_way, data in entire_data.items():

		MRPS_mean_values = []
		MRPS_standard_deviations = []
		mean_evaluations = []
		standard_deviation_evaluations = []

		# Loop for each hypothesis data problem size
		for data_pro_size in data:

			mean_MRPS, mean_eval = np.mean(data_pro_size, axis=0)
			std_MRPS, std_eval = np.std(data_pro_size, axis=0)

			MRPS_mean_values.append(mean_MRPS)
			mean_evaluations.append(mean_eval)
			MRPS_standard_deviations.append(std_MRPS)
			standard_deviation_evaluations.append(std_eval)

		processed_data[crossover_way] = {'MRPS_mean_values': MRPS_mean_values,
										'MRPS_standard_deviations': MRPS_standard_deviations,
										'mean_evaluations': mean_evaluations,
										'standard_deviation_evaluations': standard_deviation_evaluations}
	return processed_data

def print_infor_table(problem_sizes, data: dict):

	print("-"*104)
	print("|{:^20}|{:^40}|{:^40}|".format(" ", "sGA-1X", "sGA-UX"))
	print("-"*104)
	print("|{:^20}|{:^19}|{:^20}|{:^19}|{:^20}|".format("Problem size", "MRPS", "# Evaluations", "MRPS", "# Evaluations"))
	print("-"*104)
def visualize_data(style, processed_data, problem_sizes, value, function, saving_path):

	try:
		plt.style.use(style)
	except:
		print("| INVALID STYLE ...")
		exit(0)

	fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(9, 6))

	i = 0
	for crossover_way, data in processed_data.items():
		# print('{}\n{}'.format(crossover_way, data))
		# input()
		if value == 'MRPS':
			#ax.plot(np.log2(problem_sizes), np.log2(data['MRPS_mean_values']), label=crossover_way)
			ax.errorbar(problem_sizes, data['MRPS_mean_values'],
						data['MRPS_standard_deviations'], uplims=True, lolims=True,
						marker=i, linestyle='solid', label=crossover_way)
			for x, y in zip(problem_sizes, data['MRPS_mean_values']):
				label = "{:.2f}".format(y)
				plt.annotate(label, # this is the text
							(x,y), # this is the point to label
			            	textcoords="offset points", # how to position the text
			                 xytext=(1,15), # distance from text to points (x,y)
			                 ha='center') # horizontal alignment can be left, right or center

		elif value == 'evaluations':
			#ax.plot(np.log2(problem_sizes), np.log2(data['mean_evaluations']), label=crossover_way)
			ax.errorbar(problem_sizes, data['mean_evaluations'],
						data['standard_deviation_evaluations'], uplims=True, lolims=True,
						marker='o', linestyle='solid', label=crossover_way)

			for x, y in zip(problem_sizes, data['mean_evaluations']):
				label = "{:.2f}".format(y)
				plt.annotate(label, # this is the text
			                 (x,y), # this is the point to label
			                 textcoords="offset points", # how to position the text
			                 xytext=(1,15), # distance from text to points (x,y)
			                 ha='left') # horizontal alignment can be left, right or center
		i += 1

	# Print information about experiment result
	print_infor_table(problem_sizes, processed_data)

	ax.set_title(r"BIỂU ĐỒ THỂ HIỆN GIÁ TRỊ {} CẦN TỐI ƯU HÀM {}".format(value, function), fontsize=14, color='blue')
	ax.set_xlabel(r'PROBLEM SIZE', fontsize=12, color='red')
	ax.set_ylabel(r'{}'.format(value.upper()), fontsize=12, color='red')

	ax.set_xscale('log')
	ax.set_yscale('log')

	ax.set_xticks([10, 20, 40, 80, 160]);
	ax.set_xticklabels(['10', '20', '40', '80', '160']);

	plt.legend(loc='best')
	plt.show()

	# Save plot to disk
	fig.savefig(saving_path)
	print("Saved the plot to {}.".format(saving_path))

def main(args):

	saving_directory = os.path.join('figure', args['function'])
	if not os.path.exists(saving_directory):
		os.mkdir(saving_directory)

	saving_path = os.path.join(saving_directory, args['value'] + '.png')

	# print Information before run
	print("\n\n", "-"*15, "THE PLOTTING DATA INFORAMTION", "-"*15)
	print('-'*62)
	print("|- {:25} | {:<30}|".format("The optimized function ", args['function']))
	print('-'*62)
	print("|- {:25} | {:<30}|".format("The saving path", saving_path))

	# Load data from directory
	data_path = os.path.join('hypothesis', args['function'])
	data, problem_sizes = load_data_from_directory(path=data_path)

	# process data
	processed_data = process_data(data)

	# Visualize data
	visualize_data(style=args['graph_style'], processed_data=processed_data, problem_sizes=problem_sizes, value=args['value'], function=args['function'], saving_path=saving_path)

if __name__ == '__main__':
	# Get arguments from bash command
	parser = argparse.ArgumentParser(description='Plot grath to visualize results for sGA')
	parser.add_argument('--function', '-func', choices=['1MAX', 'TRAP5'], required=True
						,help='The function need to be optimized')
	parser.add_argument('--value', '-val', choices=['MRPS', 'evaluations'], required=True
						,help='The value need plotting.')
	parser.add_argument('--graph_style', '-style', default='seaborn-darkgrid'
						,help='The graph style when plot.')

	args = vars(parser.parse_args())
	main(args)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from array import array
import argparse
import sys
import ROOT
import json
import re
from DataFormats.FWLite import Events, Handle
from Analysis.HLTAnalyserPy.EvtData import EvtData, EvtHandles,phaseII_products, add_product,get_objs

import Analysis.HLTAnalyserPy.CoreTools as CoreTools
import Analysis.HLTAnalyserPy.GenTools as GenTools
import Analysis.HLTAnalyserPy.HistTools as HistTools
import Analysis.HLTAnalyserPy.TrigTools as TrigTools
import Analysis.HLTAnalyserPy.L1Tools as L1Tools
from Analysis.HLTAnalyserPy.CoreTools import UnaryFunc
from Analysis.HLTAnalyserPy.NtupTools import TreeVar
from Analysis.HLTAnalyserPy.EvtWeights import EvtWeights

from functools import partial
import itertools

def get_hlt_menu(evtdata,hlt_process="HLT"):
    if not hasattr(ROOT,"getHLTMenu"):
        ROOT.gInterpreter.Declare("""
#include "FWCore/ParameterSet/interface/ParameterSet.h"
std::string getHLTMenu(edm::ParameterSet& pset){
   const auto& cfgVerPSet = pset.getParameterSet("HLTConfigVersion");
   const auto& names = cfgVerPSet.getParameterNames();
   return cfgVerPSet.getParameter<std::string>("tableName");
}
""")
    cfg = ROOT.edm.ProcessConfiguration()
    proc_hist = evtdata.event.object().processHistory()
    proc_hist.getConfigurationForProcess(hlt_process,cfg)
    cfg_pset = evtdata.event.object().parameterSet(cfg.parameterSetID())
    return str(ROOT.getHLTMenu(cfg_pset))

class TSGHLTPathNameTree:
    def __init__(self,tree_name,evtdata,trig_res_name="trig_res"):
        self.tree = ROOT.TTree(tree_name,'')
        self.evtdata = evtdata
        self.trig_res_name = trig_res_name
        self.initialised = False
        self.last_runnr = -1 #so we can watch for trigger menu changes

    def _init_tree(self):
        self.hlt_menu = ROOT.std.string("")
        self.tree.Branch("hltMenu",self.hlt_menu)
        self.path_names = ROOT.std.vector("std::string")()
        self.tree.Branch("pathNames",self.path_names)
        self.initialised = True

    def fill(self):

        if not self.initialised:
            self._init_tree()

        if self.last_runnr!=evtdata.event.eventAuxiliary().run():
            new_hlt_menu = ROOT.std.string(get_hlt_menu(self.evtdata))
            if self.hlt_menu!=new_hlt_menu:
                self.hlt_menu.swap(ROOT.std.string(get_hlt_menu(self.evtdata)))
                trig_res = self.evtdata.get(self.trig_res_name)
                trig_names = evtdata.event.object().triggerNames(trig_res).triggerNames()
                self.path_names.swap(trig_names)
                self.tree.Fill()
            self.last_runnr=evtdata.event.eventAuxiliary().run()

class TSGRateTree:
    def __init__(self,tree_name,evtdata,trig_res_name="trig_res"):
        self.tree = ROOT.TTree(tree_name,'')
        self.evtdata = evtdata
        self.trig_res_name = trig_res_name
        self.initialised = False
        self.last_runnr = -1 #so we can watch for trigger menu changes

    def _init_tree(self):
        self.evtvars = [
            TreeVar(self.tree,"runnr/i",UnaryFunc("eventAuxiliary().run()")),
            TreeVar(self.tree,"lumiSec/i",UnaryFunc("eventAuxiliary().luminosityBlock()")),
            TreeVar(self.tree,"eventnr/i",UnaryFunc("eventAuxiliary().event()")),
        ]

        self.l1bits_initial = ROOT.TBits();
        self.tree.Branch("l1ResInitial",self.l1bits_initial)
        self.l1bits_final = ROOT.TBits();
        self.tree.Branch("l1ResFinal",self.l1bits_final)
        self.hlt_bits = ROOT.TBits();
        self.tree.Branch("hltRes",self.hlt_bits)
 #       self.hlt_bits = ROOT.std.vector(int)()
 #       self.tree.Branch("hltRes",self.hlt_bits)
        self.hlt_menu = ROOT.std.string("test")
        self.tree.Branch("hltMenu",self.hlt_menu)
        self.l1_menuid = TreeVar(self.tree,"l1MenuID/i",None)
        self.initialised = True

    def fill(self):

        if not self.initialised:
            self._init_tree()

        for var_ in self.evtvars:
            var_.fill(self.evtdata.event.object())

        trig_res = self.evtdata.get(self.trig_res_name)

        if self.last_runnr!=evtdata.event.eventAuxiliary().run():
            self.hlt_menu.swap(ROOT.std.string(get_hlt_menu(self.evtdata)))
            self.last_runnr=evtdata.event.eventAuxiliary().run()
            trig_names = evtdata.event.object().triggerNames(trig_res).triggerNames()
  #          self.hlt_bits.resize(trig_names.size())

        l1_algblk = evtdata.get("algblk")
        if l1_algblk:
            l1dec_initial = l1_algblk.at(0,0).getAlgoDecisionInitial()
            l1dec_final = l1_algblk.at(0,0).getAlgoDecisionFinal()

            for trig_nr,res in enumerate(l1dec_initial):
                self.l1bits_initial.SetBitNumber(trig_nr,res)

            for trig_nr,res in enumerate(l1dec_final):
                self.l1bits_final.SetBitNumber(trig_nr,res)

    #    trig_names = evtdata.event.object().triggerNames(trig_res).triggerNames()
    #    for trig_nr,trig_name in enumerate(trig_names):
    #        print("trig_nr ",trig_nr," ",trig_name)

        for trig_nr in range(0,trig_res.size()):
            self.hlt_bits.SetBitNumber(trig_nr,trig_res[trig_nr].accept())
           # self.hlt_bits[trig_nr] = trig_res[trig_nr].accept()

        self.tree.Fill()

if __name__ == "__main__":

    CoreTools.load_fwlitelibs()

    parser = argparse.ArgumentParser(description='example e/gamma HLT analyser')
    parser.add_argument('in_filenames',nargs="+",help='input filename')
    parser.add_argument('--prefix','-p',default='file:',help='file prefix')
    parser.add_argument('--out','-o',default="output.root",help='output filename')
    args = parser.parse_args()
    std_products = []
    add_product(std_products,"algblk","BXVector<GlobalAlgBlk>","hltGtStage2Digis")
    add_product(std_products,"extblk","BXVector<GlobalExtBlk>","hltGtStage2Digis")
    add_product(std_products,"egamma","BXVector<l1t::EGamma>","hltGtStage2Digis:EGamma")
    add_product(std_products,"etsum","BXVector<l1t::EtSum>","hltGtStage2Digis:EtSum")
    add_product(std_products,"jet","BXVector<l1t::Jet>","hltGtStage2Digis:Jet")
    add_product(std_products,"muon","BXVector<l1t::Muon>","hltGtStage2Digis:Muon")
    add_product(std_products,"tau","BXVector<l1t::Tau>","hltGtStage2Digis:Tau")
    add_product(std_products,"trig_res","edm::TriggerResults","TriggerResults")

    evtdata = EvtData(std_products,verbose=True)

    events = Events(CoreTools.get_filenames(args.in_filenames,args.prefix))
    nrevents = events.size()
    print("number of events",nrevents)
    trig_res = TrigTools.TrigResults(["DST_ZeroBias_v"])
    out_file = ROOT.TFile.Open(args.out,"RECREATE")
    rate_tree = TSGRateTree("tsgRateTree",evtdata)
    path_tree = TSGHLTPathNameTree("hltPathNameTree",evtdata)
    count = 0
    seed = 178
    for eventnr,event in enumerate(events):
        if eventnr%50000==0:
            print("{}/{}".format(eventnr,nrevents))

        evtdata.get_handles(event)
        trig_res.fill(evtdata)
       # if trig_res.result("DST_ZeroBias_v"):
        rate_tree.fill()
        path_tree.fill()

        menu = evtdata.get("algblk").at(0,0)
        if menu.getAlgoDecisionFinal()[seed]:
            count+=1

    out_file.Write()

    print("count is ",count)


def read_nx():
    buf = input().strip().split(' ')
    return [int(buf[0]), int(buf[1])]

def solve(n, x):
    ways = 0
    for i in range(1, n + 1):
        for j in range(i + 1, n + 1):
            for k in range(j + 1, n + 1):
                if i + j + k == x:
                    ways += 1
    print(ways)

def main():
    while True:
        n, x = read_nx()
        if n == 0 and x == 0:
            return
        solve(n, x)

main()


# Python Import ==================
import sys

# User Import
from common_classes.FileAccess import FileAccess

# ==============================

class CompMap (FileAccess):
    TRY_COUNT = 120

    def __init__(self, strFileName, strPath="", is_one_dim=False):
        # member varbiable for compressor
        self.arr_x_values = None

        self.arr_y1_values = None
        self.arr_y2_values = None

        self.arr_capacity = None
        self.arr_power = None

        self.is_one_dim = is_one_dim

        # compressor type (1 - reciprocating; 2 - rotary)
        self.int_comp_type = 0

        self.int_x_values = 0
        self.int_y12_values = 0
        self.bytes = b''

        self.str_manf = ''
        self.str_model = ''
        self.str_kcal_hr = ''
        self.str_eer = ''
        self.str_rpm = ''
        self.str_volt = ''

        # member varbiable
        self.m_error = FileAccess.ERR_NOT_FOUND  # Current error
        self.m_error_desc = ""

        self.m_text = ""		# last line that was read from file.
        self.m_access = ""  # Access type r,w,a,x as list
        self.m_name = ""		# Path & file name to access
        self.m_lineNo = 0  # Line number, last read
        self.m_handler = ""

        self.current_pos = 0

        # default strPath is this script path
        if strPath == "":
            strPath = sys.path[0]  # get current path

        # Full name  is path and file name
        self.m_name = strPath + "\\" + strFileName

        # Save to member variable
        self.m_access = "rb"  # read binary

        try:
            # open Text file in current directory with the given access
            self.m_handler = open(self.m_name, self.m_access)

        except OSError as err_description:
            self.m_error_desc = str(err_description)
            self.m_error = CompMap.ERR_FILE_ACCESS

    # -----------------------------------------------------------
    def is_number(self, str_data):
        str_text = str_data.strip()  # remove space

        # check that no more than one(-) & ".",
        n_count_muns = str_text.count("-")
        n_count_dec = str_text.count(".")

        b_is_int = str_text.replace('.', '', 1).replace('-', '', 1).isdigit()

        if not b_is_int or n_count_muns > 1 or n_count_dec > 1:
            self.m_error_desc = " EAR App Error: Data Error, Required Number, given Text ->" + str_text + "<-"
            self.m_error = CompMap.ERR_FILE_ACCESS
            return False

        # (-) must be on left.
        str_text = str_text.replace('-', '', 1)

        flt_value = float(str_text)
        if n_count_muns == 1:
            flt_value = -1.0 * flt_value

        return True

    # -----------------------------------------------------------
    def readValue(self, int_pos=-1, isWord=False):

        # check if there is an error
        b_current_err = self.isError()
        if self.isError():
            return not b_current_err 	# if error do nothing, return false

        if int_pos != -1:
            self.current_pos = int_pos

        if isWord:
            int_bytes_count = 2
        else:
            int_bytes_count = 1

        # seek to position
        self.m_handler.seek(self.current_pos)

        # set no error flag
        self.m_error = FileAccess.ERR_NOT_FOUND
        try:
            while True:
                self.bytes = self.m_handler.read(
                    int_bytes_count)  # read rec. length
                self.current_pos = self.current_pos + int_bytes_count

                if self.bytes == b"":
                    self.m_error = FileAccess.ERR_BLANK_LINE
                    return not self.isError()

                int_rec_len = int.from_bytes(self.bytes, "big")
                if int_rec_len == 0:
                    continue
                break

        except OSError as err:
            self.m_error = err

        return not self.isError()  # if read done, then true, else false

    # -----------------------------------------------------------
    def readrecord(self, int_pos=-1):
        # check if there is an error
        b_current_err = self.isError()
        if self.isError():
            return not b_current_err 	# if error do nothing, return false

        if int_pos != -1:
            self.current_pos = int_pos

        # set no error flag
        self.m_error = FileAccess.ERR_NOT_FOUND

        try:
            # seek to position
            self.m_handler.seek(self.current_pos)
            while True:
                byte = self.m_handler.read(1)  # read rec. length
                self.current_pos = self.current_pos + 1

                if byte == b"":
                    self.m_error = FileAccess.ERR_BLANK_LINE
                    return not self.isError()

                int_rec_len = int.from_bytes(byte, "big")
                if int_rec_len == 0:
                    continue

                self.bytes = self.m_handler.read(
                    int_rec_len)  # read rec. length
                self.current_pos = self.current_pos + int_rec_len
                break

        except OSError as err:
            self.m_error = err

        return not self.isError()  # if read done, then true, else false

    # -----------------------------------------------------------
    def getRec(self, is_num=True):
        if self.isError():
            return

        self.m_text = self.bytes.decode("utf-8")

        if is_num:
            if self.is_number(self.m_text):
                self.m_text = float(self.m_text)

        return self.m_text

    # -----------------------------------------------------------
    def getValue(self):
        if self.isError():
            return
        return int.from_bytes(self.bytes, "big")

    # -----------------------------------------------------------
    def setBasicInfo(self):
        self.readrecord()
        self.str_manf = self.getRec(False)
        self.readrecord()
        self.str_model = self.getRec(False)
        self.readrecord()
        # some time is not given, text put
        self.str_kcal_hr = self.getRec(False)
        self.readrecord()
        self.str_eer = self.getRec(False)  # some time is not given, text put
        self.readrecord()
        self.str_rpm = self.getRec(False)  # some time is not given, text put
        self.readrecord()
        self.str_volt = self.getRec(False)  # some time is not given, text put

    # -----------------------------------------------------------
    def setX_Y(self):
        self.readValue()
        self.int_comp_type = self.getValue()
        self.readValue()
        self.int_y12_values = self.getValue()
        self.readValue()
        self.int_x_values = self.getValue()

    # -----------------------------------------------------------
    def read_Xdata(self):

        if self.isError():
            return

        self.arr_x_values = [0.0] * (self.int_x_values + 1)  # base value is 1

        for ncnt in range(0, self.int_x_values):

            if ncnt == 0:  # check entry point for data, starts with none zzero value
                Value = self.getNonZero()

            else:
                if not self.readrecord():
                    return
                Value = self.getRec()

            self.arr_x_values[ncnt + 1] = Value

    # -----------------------------------------------------------
    def read_Ydata(self):
        if self.isError():
            return

        self.arr_y1_values = [0.0] * (self.int_y12_values + 1)  # base is 1
        self.arr_y2_values = [0.0] * (self.int_y12_values + 1)

        for ncnt in range(0, self.int_y12_values):
            if not self.readrecord():
                return

            self.arr_y1_values[ncnt + 1] = self.getRec()

            if not self.readrecord():
                return
            self.arr_y2_values[ncnt + 1] = self.getRec()

    # -----------------------------------------------------------
    def readCapacity(self):
        if self.isError():
            return

        if self.is_one_dim:
            self.arr_capacity = [0.0] * \
                (self.int_x_values * self.int_y12_values + 1)
        else:
            self.arr_capacity = [[0.0] * (self.int_x_values + 1)
                                 for i in range(self.int_y12_values + 1)]

        # add shift one word to the current position
        for ncnt_x in range(0, self.int_y12_values):
            #self.current_pos = self.current_pos + CompMap.SEG_BLOCK
            for ncnt_y in range(0, self.int_x_values):

                if self.is_one_dim:
                    self.arr_capacity[1 +
                                      int(ncnt_x *
                                          self.int_x_values +
                                          ncnt_y)] = self.getNonZero()
                else:
                    self.arr_capacity[ncnt_x +
                                      1][ncnt_y + 1] = self.getNonZero()

    # -----------------------------------------------------------
    def getNonZero(self):
        Value = 0
        nTry = 0
        str_last_err = ""
        # skip all zeros or errors if any
        while nTry < CompMap.TRY_COUNT and (Value == 0 or Value >= 100000):
            nTry = nTry + 1
            if not self.readrecord():
                return

            Value = self.getRec()
            if self.isError():
                str_last_err = self.m_error
                self.m_error = FileAccess.ERR_NOT_FOUND
                Value = 0
                #print ("Test nTry=",nTry, self.bytes.decode("utf-8") )
                continue

        # if exit without reading
        if nTry >= CompMap.TRY_COUNT:
            self.m_error = str_last_err

        return Value

    # -----------------------------------------------------------
    def readPower(self):
        if self.isError():
            return

        if self.is_one_dim:
            self.arr_power = [
                0.0] * (self.int_x_values * self.int_y12_values + 1)  # base is 1
        else:
            self.arr_power = [[0.0] * (self.int_x_values + 1)
                              for i in range(self.int_y12_values + 1)]

        # add shift one word to the current position
        #self.current_pos = self.current_pos + CompMap.SEG_POWER - CompMap.SEG_BLOCK

        for ncnt_x in range(0, self.int_y12_values):
            #self.current_pos = self.current_pos + CompMap.SEG_BLOCK
            for ncnt_y in range(0, self.int_x_values):

                if self.is_one_dim:
                    self.arr_power[int(
                        ncnt_x * self.int_x_values + ncnt_y) + 1] = self.getNonZero()
                else:
                    self.arr_power[ncnt_x + 1][ncnt_y + 1] = self.getNonZero()

    # -----------------------------------------------------------
    def readMapData(self):
        self.setBasicInfo()
        self.setX_Y()

        self.read_Ydata()
        self.read_Xdata()

        self.readCapacity()
        self.readPower()

    def getManif(self):
        return self.str_manf
    # -----------------------------------------------------------

    def getModel(self):
        return self.str_model
    # -----------------------------------------------------------

    def getKcal(self):
        return self.str_kcal_hr
    # -----------------------------------------------------------

    def getEer(self):
        return self.str_eer
    # -----------------------------------------------------------

    def getRpm(self):
        return self.str_rpm
    # -----------------------------------------------------------

    def getVolt(self):
        return self.str_volt

    # -----------------------------------------------------------
    def getY_count(self):
        return self.int_y12_values

    # -----------------------------------------------------------
    def getX_count(self):
        return self.int_x_values

    # -----------------------------------------------------------
    def getUnit(self):
        if self.isError():
            return
        #	     capacity, temper, mass flow
        # unit 1 - kcal/hr, deg c, kg/hr
        # unit 2 - btu/hr,  deg f, lb/hr

        # power data must be in watts
        #
        if self.arr_y1_values[1] <= 50:
            return 1  # kcal/hr, deg c, kg/hr
        else:
            return 2  # btu/hr,  deg f, lb/hr

    # -----------------------------------------------------------
    def getType(self):
        return self.int_comp_type
    # -----------------------------------------------------------

    def getCapacity(self):
        return self.arr_capacity

    # -----------------------------------------------------------
    def getPower(self):
        return self.arr_power

    # -----------------------------------------------------------
    def getX_values(self):
        return self.arr_x_values

    # -----------------------------------------------------------
    def getY1_values(self):
        return self.arr_y1_values

    # -----------------------------------------------------------
    def getY2_values(self):
        return self.arr_y2_values


from django.urls import path
from . import views

urlpatterns = [
    path('', views.all_blogs, name='blogs'),
    path('add/', views.add_blog, name='add_blog'),
    path('edit/<int:blog_id>/', views.edit_blog, name='edit_blog'),
    path('delete/<int:blog_id>/', views.delete_blog, name='delete_blog'),
    path('add_comment/<int:blog_id>/', views.add_comment, name='add_comment'),
    path(
        'edit_comment/<int:comment_id>/',
        views.edit_comment,
        name='edit_comment'
        ),
    path(
        'delete_comment/<int:comment_id>/',
        views.delete_comment,
        name='delete_comment'
        ),
]


# coding: utf-8

import datetime
import copy
from urlparse import urljoin

import spiderDefault
import log

def parse_time(time_str=None):
    """
    解析时间文本
    :param time_str:
    :return:
    """
    if time_str:
        if "-" in time_str:
            time = datetime.datetime.strptime(time_str, "%H:%M - %Y/%m/%d") - datetime.timedelta(hours=7)
        else:
            time = datetime.datetime.strptime(time_str, "%H:%M %Y/%m/%d") - datetime.timedelta(hours=7)
    else:
        time = datetime.datetime.utcnow()
    return time

class MySpider(spiderDefault.Spider):
    def __init__(self, cmd_args=None):
        spiderDefault.Spider.__init__(self, cmd_args=cmd_args)
        self.info_flag = "01"
        self.siteName = "shidai.vn"
        self.site_domain = "shidai.vn"
        self.start_urls = [
            "http://shidai.vn/政治_t113c3",  # 政治
            "http://shidai.vn/政治/评论_t113c5",  # 评论
            "http://shidai.vn/政治/东海专题_t113c4",  # 东海专题
            "http://shidai.vn/经济_t113c89",  # 经济
            "http://shidai.vn/文化-体育_t113c6",  # 文化体育
            "http://shidai.vn/文化-体育/旅游_t113c90",  # 旅游
            "http://shidai.vn/文化-体育/体育_t113c91",  # 体育
            "http://shidai.vn/文化-体育/娱乐_t113c92",  # 娱乐
            "http://shidai.vn/越南家庭_t113c9",  # 越南家庭
            "http://shidai.vn/社会_t113c14",  # 社会
            "http://shidai.vn/热爱祖国_t113c20",  # 热爱祖国
            "http://shidai.vn/越中关系_t113c23",  # 越中关系
            "http://shidai.vn/越中关系/越中友好协会_t113c26",  # 越中友好协会
            "http://shidai.vn/越中关系/交流合作_t113c25",  # 交流合作
            "http://shidai.vn/越中关系/越中贸易_t113c24",  # 越中贸易
            "http://shidai.vn/四海兄弟_t113c27",  # 四海兄弟
            "http://shidai.vn/四海兄弟/非政府组织活动_t113c29",  # 四海兄弟
            "http://shidai.vn/四海兄弟/其他友好协会_t113c28",  # 其他友好协会
            "http://shidai.vn/国际_t113c30",  # 国际
        ]
        self.encoding = "utf-8"
        self.c_time = datetime.datetime.utcnow() - datetime.timedelta(days=3)
        self.page_url = {}
        self.max_interval = datetime.timedelta(days=3)

    def get_detail_page_urls(self, data):
        """
        从列表页中获取详情页URL，返回列表
        :param data:
        :return:
        """
        detail_page_urls = []
        if data is not None:
            base_url = data.response.request.url
            items = data.xpathall('//div[@class="first_item" or @class="xitem"]')

            for item in items:
                post_url = item.xpath('//h2//@href').text().strip()
                if not post_url:
                    continue
                post_url = urljoin(base_url, post_url)
                c_time = item.xpath('//div[@style="margin:5px 0;"]').text().strip()
                c_time = parse_time(c_time)
                if c_time < self.c_time:
                    continue
                detail_page_urls.append(post_url)
                self.page_url[post_url] = base_url
            detail_page_urls = list(set(detail_page_urls))
        return detail_page_urls

    def clear_special_xp(self, data, xp):
        '''删除指定xpath数据'''
        data = copy.copy(data)
        result = data._root.xpath(xp)
        for i in result:
            try:
                i.getparent().remove(i)
            except Exception as e:
                log.logger.error(e)
        return data

    def get_detail_page_info(self, data):
        """
        解析详情页信息
        :param data:
        :return:
        """
        base_url = data.response.request.url
        result = []
        pic_urls = []

        title = data.xpath('//div[@class="xtext"]//h1').text().strip()
        if not title:
            return []
        g_time = datetime.datetime.utcnow()
        c_time = data.xpath('//span[@class="xdate"]').text().strip()
        c_time = parse_time(c_time)
        if c_time < self.c_time:
            return []
        channel = data.xpathall('//span[@itemprop="title"]')
        if channel:
            channel = channel[-1].text().strip()
        else:
            channel = self.siteName
        source = data.xpath('//meta[@name="author"]/@content').text().strip()
        if not source:
            source = self.siteName

        retweeted_source = self.siteName
        retweeted_status_url = ''

        list_page_url = self.page_url.get(base_url, '')

        content_items = data.xpathall('//h2[@class="xsubject"] | //div[@class="xcontents"]')
        content = content_xml = ""
        for item in content_items:
            content_str = self.clear_special_xp(item,'//img/ancestor::div[@style] | //span[@class="clgt"]/parent::div '
                                                     '| //br')
            content += content_str.text().strip()
            content_xml += content_str.data.encode("utf-8")

        content = title if not content else content
        pic_list = data.xpathall('//div[@class="xcontents"]//img/@src')
        pic_urls = [urljoin(base_url, pic_url.text().strip()) for pic_url in pic_list]

        post = {
            'title': title,
            'gtime': g_time,
            'ctime': c_time,
            'source': source,
            'retweeted_source': retweeted_source,
            'channel': channel,
            'list_page_url': list_page_url,
            'siteName': self.siteName + '-' + channel,
            'url': base_url,
            'content': content,
            'content_xml': content_xml,
        }

        if pic_urls:
            post.update({"pic_urls": pic_urls})
        if channel == self.siteName:
            post.update({"siteName": self.siteName})
        if retweeted_status_url:
            post.update({'retweeted_status_url': retweeted_status_url})
        result.append(post)
        return result

if __name__ == '__main__':
    spider = MySpider()
    spider.proxy_enable = False
    spider.init_debup()
    spider.init_downloader()

    url = spider.start_urls[0]

    response = spider.download(url)