import datetime
import threading
import random

from . database import Database
from . user import User
from . pages import Pages

class Newsfeed:
    def __init__(self):
        self.database = Database()
        self.user = User()
        self.pages = Pages()

    def get_newsfeed(self, access_token, uid):
        user_pages_data = self.get_update_user_likes_pages(access_token, uid)
        random_pages = random.sample(user_pages_data,50)
        update_current_newsfeed_thread = threading.Thread(target= self.database.update_user_newsfeed, args= (uid ,random_pages[10:]), daemon= True)
        update_current_newsfeed_thread.start()
        return {
                'newsfeed' : {
                    'data' : self.get_pages_post(access_token, uid, random_pages[:10])
                },
                'next' : 10 < len(random_pages)
            }

    def get_next_newsfeed(self, access_token, uid):
        newsfeed = self.database.get_next_newsfeed(uid)
        return {
                'newsfeed' : {
                    'data' : self.get_pages_post(access_token, uid, newsfeed)
                },
                'next' : newsfeed != []
            }

    def get_update_user_likes_pages(self, access_token, uid):
        likes_data = self.database.get_user_likes_pages(uid)
        if likes_data == None:
            likes_data = self.user.get_like_pages(access_token, uid)
            if 'error_subcode' not in likes_data: self.database.update_user_likes_pages(uid, likes_data)
        else:
            if (datetime.datetime.now() - likes_data['time_stamp']).days > 7:
                likes_data = self.user.get_like_pages(access_token, uid)
                if 'error_subcode' not in likes_data: self.database.update_user_likes_pages(uid, likes_data)
            else:
                likes_data = likes_data['pages']
        return likes_data

    def get_pages_post(self, access_token, uid, list_of_pages):
        upper_newsfeed = []
        lower_newsfeed = []
        inactive_pages = []
        for page in list_of_pages:
            page_post = self.check_pages_validator(access_token, page['id'])
            if page_post == None: inactive_pages.append(page['id'])
            elif 'list_of_posts' in page_post:
                if len(page_post['list_of_posts']) == 2:
                    upper_newsfeed.append(page_post['list_of_posts'][0])
                    lower_newsfeed.append(page_post['list_of_posts'][1])
                elif len(page_post['list_of_posts']) == 1:
                    upper_newsfeed.append(page_post['list_of_posts'][0])
        remove_inactive_page_threading = threading.Thread(target=self.database.remove_inactive_page, args=(uid, inactive_pages), daemon=True)
        remove_inactive_page_threading.start()
        random.shuffle(upper_newsfeed)
        random.shuffle(lower_newsfeed)
        upper_newsfeed.extend(lower_newsfeed)
        return upper_newsfeed

    def check_pages_validator(self, access_token, page_id):
        page = self.database.get_page_from_cache(page_id)
        if page == None:
            page = self.pages.get_page_post(access_token, page_id)
            if 'error' not in page:
                self.database.insert_page_to_cache(page_id, page)
        else:
            if page['isActive']:
                diff_time = datetime.datetime.now() - page['time_stamp']
                if(0 < diff_time.days or page['priority'] < diff_time.seconds):
                    page = self.pages.get_page_post(access_token, page_id)
                    if 'error' not in page:
                        self.database.update_page_in_cache(page_id, page)
            else: return None
        return page

    def collect_like_posts(self, uid, metadata):
        self.database.collect_like_posts(uid, metadata)
        return {'msg' : 'collect data success'}

    def collect_dislike_posts(self, uid, metadata):
        self.database.collect_dislike_posts(uid, metadata)
        return {'msg' : 'collect data success'}


# all code from [3]
import sqlite3 # Standard Python Library

def connect_db():
    db = sqlite3.connect('database.db') # Create/connect to database Requires elevated privalges to run (ie from cmd/terminal or pycharm running as admin)
    db.cursor().execute('CREATE TABLE IF NOT EXISTS comments ' # Create table comments with int ID (Primary Key) and comment text attributes
                        '(id INTEGER PRIMARY KEY, '
                        'comment TEXT)')
    db.commit() # Commit changes
    return db

def add_comment(comment):
    db = connect_db() # Connect to DB
    db.cursor().execute('INSERT INTO comments (comment) ' # Insert comment with passed in variable
                        'VALUES (?)', (comment,))
    db.commit() # Commit changes

def get_comments(search_query=None):
    db = connect_db() # Connect to DB
    results = [] # Initialise results list
    get_all_query = 'SELECT comment FROM comments' # Get all comments
    for (comment,) in db.cursor().execute(get_all_query).fetchall(): # Append get_all_query output to results list.
        if search_query is None or search_query in comment:
            results.append(comment)
    return results


#!/usr/bin/env python

import numpy as np

import bregnn.ffwd as ffwd
import bregnn.io as io

import os
import json

from pprint import pprint

from sklearn.model_selection import train_test_split

from optparse import OptionParser, make_option

##
default_features = ['Jet_pt',
                    'Jet_eta',
                    'rho',
                    'Jet_mt',
                    'Jet_leadTrackPt',
                    'Jet_leptonPtRel',
                    'Jet_leptonDeltaR',
                    'Jet_neHEF',
                    'Jet_neEmEF',
                    'Jet_vtxPt',
                    'Jet_vtxMass',
                    'Jet_vtx3dL',
                    'Jet_vtxNtrk',
                    'Jet_vtx3deL',
                    'Jet_energyRing_dR0_em_Jet_e',
                    'Jet_energyRing_dR1_em_Jet_e',
                    'Jet_energyRing_dR2_em_Jet_e',
                    'Jet_energyRing_dR3_em_Jet_e',
                    'Jet_energyRing_dR4_em_Jet_e',
                    'Jet_energyRing_dR0_neut_Jet_e',
                    'Jet_energyRing_dR1_neut_Jet_e',
                    'Jet_energyRing_dR2_neut_Jet_e',
                    'Jet_energyRing_dR3_neut_Jet_e',
                    'Jet_energyRing_dR4_neut_Jet_e',
                    'Jet_energyRing_dR0_ch_Jet_e',
                    'Jet_energyRing_dR1_ch_Jet_e',
                    'Jet_energyRing_dR2_ch_Jet_e',
                    'Jet_energyRing_dR3_ch_Jet_e',
                    'Jet_energyRing_dR4_ch_Jet_e',
                    'Jet_energyRing_dR0_mu_Jet_e',
                    'Jet_energyRing_dR1_mu_Jet_e',
                    'Jet_energyRing_dR2_mu_Jet_e',
                    'Jet_energyRing_dR3_mu_Jet_e',
                    'Jet_energyRing_dR4_mu_Jet_e',
                    'Jet_numDaughters_pt03']

## command_line options
parser = OptionParser(option_list=[
    make_option("--inp-dir",type='string',dest="inp_dir",default=os.environ['SCRATCH']+'/bregression'),
    make_option("--out-dir",type='string',dest="out_dir",default='.'),
    make_option("--inp-file",type='string',dest='inp_file',default='ttbar_unweighted_full80M_selected_train.hd5'),
    make_option("--features",type='string',dest='features',default=''),
    make_option("--normalize-target",action="store_true",dest="normalize_target",default=True),
    make_option("--no-normalize-target",action="store_false",dest="normalize_target"),
    make_option("--loss",type='string',dest="loss",default="mse"),
    make_option("--valid-frac",type='float',dest='valid_frac',default=0.05),
    make_option("--batch-size",type='int',dest='batch_size',default=1024),
    make_option("--epochs",type='int',dest='epochs',default=20),
    make_option("--hparams",type='string',dest='hparams',default=None),
    make_option("--seed",type='int',dest='seed',default=98543),
])

## parse options
(options, args) = parser.parse_args()

if options.features == '':
    features = default_features
    options.features = ','.join(features)
else:
    features = options.features.split(',')

if os.path.exists(options.inp_file):
    inp_file = options.inp_file
else:
    inp_file = options.inp_dir+'/'+options.inp_file

hparams = {}
if options.hparams is not None:
    with open(options.hparams) as hf:
        hparams = json.loads(hf.read())

## read data
columns = features + ['Jet_mcPt']
data = io.read_data(inp_file, columns = columns)

X = data[features].values
y = (data['Jet_mcPt']/data['Jet_pt']).values.reshape(-1,1)

y_mean = np.median(y)
y_std = y.std()
print(y_mean,y_std)

# normalize target
if options.normalize_target:
    y -= y_mean
    y /= y_std

# split data
X_train,X_valid,y_train,y_valid = train_test_split(X,y,test_size=options.valid_frac,random_state=options.seed)

# sort out model parameters
def get_kwargs(fn,**kwargs):
    params = set(fn.__code__.co_varnames[:fn.__code__.co_argcount]+tuple(kwargs.keys()))
    for par in params:
        if hasattr(options,par):
            kwargs[par] = getattr(options,par)
        if par in hparams:
            kwargs[par] = hparams.pop(par)
    return kwargs

init_kwargs = get_kwargs(ffwd.FFWDRegression.__init__,monitor_dir=options.out_dir)
fit_kwargs = get_kwargs(ffwd.FFWDRegression.fit,batch_size=options.batch_size,epochs=options.epochs)

print(init_kwargs)
print(fit_kwargs)

reg = ffwd.FFWDRegression('ffwd',X.shape[1:],**init_kwargs)

pprint(reg.get_params())

model = reg()

print(model.summary())

if not os.path.exists(options.out_dir):
    os.mkdir(options.out_dir)

store = dict( y_mean=float(y_mean), y_std=float(y_std),
              model_params = reg.get_params(),
              fit_kwargs = fit_kwargs,
              options = options.__dict__
              )
with open(options.out_dir+'/config.json','w+') as fo:
    fo.write( json.dumps( store, indent=4 ) )
    fo.close()

reg.fit(X_train,y_train,
        validation_data=(X_valid,y_valid),
        **fit_kwargs)


import sys
import types
import inspect

def isstring(s):
    # if we use Python 3
    if (sys.version_info[0] >= 3):
        return isinstance(s, str)
    # we use Python 2
    return isinstance(s, basestring)

def normalize_func(func):
    # convert class to __init__ method if we can
    if inspect.isclass(func):
      if (inspect.ismethod(func.__init__)):
        func = func.__init__
      else:
        return None

    # return None for builtins
    if (inspect.isbuiltin(func)):
        return None

    return func

def get_doc(func):
    func = normalize_func(func)
    if func is None:
      return None

    return inspect.getdoc(func)

def get_property_doc(target, prop):
  for name, obj in inspect.getmembers(type(target), inspect.isdatadescriptor):
    if (isinstance(obj, property) and name == prop):
      return inspect.getdoc(obj.fget)
  return None

def get_arguments(func):
    func = normalize_func(func)
    if func is None:
      return None
    try:
      argspec = inspect.getargspec(func)
    except TypeError:
      return None
    args = argspec.args
    if 'self' in args:
      args.remove('self')
    return args

def generate_signature_for_function(func):
    """Given a function, returns a string representing its args."""

    func = normalize_func(func)
    if func is None:
      return None

    args_list = []
    try:
      argspec = inspect.getargspec(func)
    except TypeError:
      return None
    first_arg_with_default = (
        len(argspec.args or []) - len(argspec.defaults or []))
    for arg in argspec.args[:first_arg_with_default]:
      if arg == "self":
        # Python documentation typically skips `self` when printing method
        # signatures.
        continue
      args_list.append(arg)

    # TODO(mrry): This is a workaround for documenting signature of
    # functions that have the @contextlib.contextmanager decorator.
    # We should do something better.
    if argspec.varargs == "args" and argspec.keywords == "kwds":
      original_func = func.__closure__[0].cell_contents
      return generate_signature_for_function(original_func)

    if argspec.defaults:
      for arg, default in zip(
          argspec.args[first_arg_with_default:], argspec.defaults):
        if callable(default) and hasattr(default, '__name__'):
          args_list.append("%s = %s" % (arg, default.__name__))
        else:
          if default is None:
            args_list.append("%s = NULL" % (arg))
          elif type(default) == type(True):
            if default == True:
              args_list.append("%s = TRUE" % (arg))
            else:
              args_list.append("%s = FALSE" % (arg))
          elif isstring(default):
            args_list.append("%s = \"%s\"" % (arg, default))
          elif isinstance(default, int):
            args_list.append("%s = %rL" % (arg, default))
          elif isinstance(default, float):
            args_list.append("%s = %r" % (arg, default))
          else:
            args_list.append("%s = %r" % (arg, default))
    if argspec.varargs:
      args_list.append("...")
    if argspec.keywords:
      args_list.append("...")
    return "(" + ", ".join(args_list) + ")"


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
from weather import get_forecasts

# pengecekan pesan bay --> polling
updater = Updater(token="621603636:AAEVtXeRzaOqH8fhvlZypmhWjj9_GPXP_cU")

# mengijinkan registrasu handler -> command, text, video, audio etc
dispatcher = updater.dispatcher

# mendefinisikan callback function

def start(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text="Hai, Selamat datang di HerhoWeatherBot! \n Kirimkan perintah /info untuk mengetahui info cuaca terkini di wilayah anda!")

# membuat perintah handler
start_handler = CommandHandler("start", start)

# menambahkan perintah handler ke dispatcher
dispatcher.add_handler(start_handler)

def echo(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text=update.message.text.upper())

# membuat text handler
echo_handler = MessageHandler(Filters.text, echo)

dispatcher.add_handler(echo_handler)

def option(bot, update):
    button = [
        [InlineKeyboardButton("Pilihan 1", callback_data="1"),
         InlineKeyboardButton("Pilihan 2", callback_data="2")],
        [InlineKeyboardButton("Pilihan 3", callback_data="3")]
    ]
    reply_markup = InlineKeyboardMarkup(button)

    bot.send_message(chat_id=update.message.chat_id,
                     text="Pilih salah satu...",
                     reply_markup=reply_markup)

option_handler = CommandHandler("pilihan", option)
dispatcher.add_handler(option_handler)

def get_location(bot, update):
    button = [
        [KeyboardButton("Bagikan lokasi", request_location=True)]
    ]
    reply_markup = ReplyKeyboardMarkup(button)
    bot.send_message(chat_id=update.message.chat_id,
                     text="Apakah anda keberatan membagikan lokasi anda saat ini?",
                     reply_markup=reply_markup)

get_location_handler = CommandHandler("info", get_location)
dispatcher.add_handler(get_location_handler)

def location(bot, update):
    lat = update.message.location.latitude
    lon = update.message.location.longitude
    forecasts = get_forecasts(lat, lon)
    bot.send_message(chat_id=update.message.chat_id,
                     text=forecasts,
                     reply_markup=ReplyKeyboardRemove())

location_handler = MessageHandler(Filters.location, location)
dispatcher.add_handler(location_handler)

# memulai polling
updater.start_polling()


from datetime import datetime
import tensorflow as tf
from tensorflow.keras.applications import DenseNet121
import numpy as np
import os
#######################################
config = tf.compat.v1.ConfigProto()   #
config.gpu_options.allow_growth = True#
tf.compat.v1.Session(config=config)   #
#######################################
import PIL
import PIL.Image
import tensorflow_datasets as tfds

from os import listdir
from os.path import isfile, join

import csv

import pandas as pd
import tensorflow.keras as keras
import time
import tensorboard
import matplotlib.pyplot as plt
from tensorflow import keras
import subprocess

currentDir = os.getcwd().replace("source", "")
dataset_path = currentDir + "dataset/train_images/train_images/"

imagesTrainPath = "../dataset/train_images/train_images/"
labelsPath = "../dataset/train_images/train_label.txt"
TRAINING_FILENAMES = [f for f in listdir(imagesTrainPath) if isfile(join(imagesTrainPath, f))]
#################################################
# Import the necessary libraries
from PIL import Image
from numpy import asarray

CLASSES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

def load_labels(labelsPath, classes):
    df = pd.read_csv(labelsPath)
    lst = df.values.tolist()
    labels = [classes.index(i[0]) for i in lst]
    return labels

def to_oneHot(labels):
    a = labels.astype(int)
    b = np.zeros((a.size, a.max()+1))
    b[np.arange(a.size),a] = 1
    return b

def get_class(modelOutput): #takes the max probability index of a row and turns it into a one hot vector
    b = np.zeros(modelOutput.shape)
    b[np.arange(b.shape[0]), np.argmax(a, axis=1)] = 1
    return b

def one_hot_to_lables(onehotData):
    return np.argmax(onehotData, axis = 1)

def write_to_file(data, outputFile = "output.csv"):
    myFile = open(outputFile, 'w', newline='')
    with myFile:
        writer = csv.writer(myFile)
        data = [["id", "classes"]] + data
        writer.writerows(data)
    print("Writing to ", outputFile," completed")

# print('Tensorflow version ' + tf.__version__)
# print(TRAINING_FILENAMES[0:10])

# os.popen(cp data'.txt' /label)
imgsize = (32,32)
batchsize = 500

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "../dataset/train_images/train_images",
    labels = "inferred",
    label_mode = 'categorical',
    validation_split=0.10,
    subset="training",
    shuffle=True,
    seed=321,
    image_size = imgsize,
    batch_size=batchsize)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "../dataset/train_images/train_images",
    labels = "inferred",
    label_mode = 'categorical',
    validation_split=0.10,
    subset="validation",
    seed=321,
    image_size=imgsize,
    batch_size=batchsize)

AUTOTUNE = tf.data.experimental.AUTOTUNE

train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

EPOCHS = 15
# LR_START = 0.00001
# LR_MAX = 0.00005
# LR_MIN = 0.00001
# LR_RAMPUP_EPOCHS = 5
# LR_SUSTAIN_EPOCHS = 0
# LR_EXP_DECAY = .8

LR_START = 0.00001      # bfore 0.0001
LR_MAX = 0.00045 # bfore 0.0005
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 8
LR_SUSTAIN_EPOCHS = 2 # before 2
LR_EXP_DECAY = .8

# LR_START = 0.00001
# LR_MAX = 0.00015
# LR_MIN = 0.00001
# LR_RAMPUP_EPOCHS = 8
# LR_SUSTAIN_EPOCHS = 2
# LR_EXP_DECAY = .9

def lrfn(epoch):
    if epoch < LR_RAMPUP_EPOCHS:
        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START
    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
        lr = LR_MAX
    else:
        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN
    return lr
# def lrfn(epoch):
#     if epoch < LR_RAMPUP_EPOCHS:
#         lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START
#     elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
#         lr = LR_MAX
#     elif epoch > (LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS)/2:
#          lr = LR_MAX/2
#     else:
#         lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN
#     return lr

lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = True)
rng = [i for i in range(25 if EPOCHS<25 else EPOCHS)]
y = [lrfn(x) for x in rng]
# plt.plot(y)
# plt.show()
import random
def random_invert_img(x, p=0.5):
  if  random.random() < p:
    x = (255-x)
  else:
    x
  return x

def random_invert(factor=0.5):
  return tf.keras.layers.Lambda(lambda x: random_invert_img(x, factor))

# def random_greyscale_convert_img(x, p=0.5):
#   if  random.random() < p:
#     x = tf.image.rgb_to_grayscale(x)
#   else:
#     x
#   return x

# def random_greyscale(factor = 0.5):
#     return tf.keras.layers.Lambda(lambda x: random_greyscale_convert_img(x, factor ))

class convNeuralNet:
    def __init__(self):
        self.model = self.modelInit()

    def modelInit(self):
        rnet = DenseNet121(
            input_shape=(32,32,3),
            weights="imagenet",
            include_top=False,
            pooling=max
        )
        rnet.trainable = True
        model = keras.models.Sequential()

        model.add(random_invert(factor=0.1))
        model.add(tf.keras.layers.GaussianNoise(0.12)) #before 0.1
        model.add(tf.keras.layers.experimental.preprocessing.Rescaling(1./255))
        model.add(keras.layers.experimental.preprocessing.RandomFlip("horizontal")) #84.5 horizontal
        model.add(tf.keras.layers.experimental.preprocessing.RandomContrast(0.01))

        model.add(rnet)
        model.add(keras.layers.GlobalMaxPooling2D())

        model.add(keras.layers.Dense(10, activation = 'sigmoid'))

        model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

    def modelLoad(self, path):
        self.model = keras.models.load_model(path)

    def train(self, dataset, valset, nEpochs):
        self.model.fit(dataset, validation_data=val_ds, epochs = nEpochs, callbacks=[lr_callback, tensorboard_callback],verbose=2)

    def predict(self, data_x):
        return self.model.predict(data_x)

    def ajustDropout(self, new_rates):
        if(len(new_rates)==3):
            self.model.layers[2].rate = new_rates[0]
            self.model.layers[5].rate = new_rates[1]
            self.model.layers[11].rate = new_rates[2]
    def ajustRotation(self, newrate):
        self.model.layers[1].factor = newrate

tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

modelConv = convNeuralNet()
modelConv.train(train_ds,val_ds, 20)

testDir = "../dataset/test_images/content/data/test_imagess/"

test_list = []
for i in range(10000):
    img = Image.open(testDir +"Image_"+str(i+1) + ".png")
    array = asarray(img)
    test_list.append(array)

test_list = np.array(test_list)

prediction = modelConv.predict(test_list)

i = np.arange(prediction.shape[0])
labels = one_hot_to_lables(prediction)
output = np.stack((i, labels), axis = 1)
print(len(labels))
write_to_file(output.tolist(), outputFile = "prediction.csv")


import FWCore.ParameterSet.Config as cms

import copy
from HLTrigger.HLTfilters.hltHighLevel_cfi import *
vbfhzzllbbHLTFilter = copy.deepcopy(hltHighLevel)
# how to deal with multiple triggers:
# - True (OR) accept if ANY is true,
# - False (AND) accept if ALL are
vbfhzzllbbHLTFilter.andOr = cms.bool(True)
# provide list of HLT paths (or patterns) you want
vbfhzzllbbHLTFilter.HLTPaths = [
     'HLT_L1Jet15',
     'HLT_Jet30',
     'HLT_Jet50',
     'HLT_Jet80',
     'HLT_DiJetAve15',
     'HLT_DiJetAve30',
     'HLT_DiJetAve50',
     'HLT_DiJetAve70',
     'HLT_DiJetAve130',
     'HLT_DiJetAve220',
     'HLT_TripleJet85',
     'HLT_QuadJet30',
     'HLT_QuadJet60',
     'HLT_SumET120',
     'HLT_L1MET20',
     'HLT_MET25',
     'HLT_IsoEle18_L1R',
     'HLT_IsoEle15_LW_L1I',
     'HLT_LooseIsoEle15_LW_L1R',
     'HLT_Ele10_SW_L1R',
     'HLT_Ele15_SW_L1R',
     'HLT_Ele15_LW_L1R',
     'HLT_DoubleIsoEle10_L1I',
     'HLT_DoubleIsoEle12_L1R',
     'HLT_DoubleIsoEle10_LW_L1I',
     'HLT_DoubleIsoEle12_LW_L1R',
     'HLT_DoubleEle5_SW_L1R',
     'HLT_DoubleEle10_LW_OnlyPixelM_L1R',
     'HLT_DoubleEle10_Z',
     'HLT_DoubleEle6_Exclusive',
     'HLT_Photon15_L1R',
     'HLT_Photon25_L1R',
     'HLT_L1Mu',
     'HLT_L1MuOpen',
     'HLT_L2Mu9',
     'HLT_IsoMu9',
     'HLT_IsoMu11',
     'HLT_IsoMu13',
     'HLT_IsoMu15',
     'HLT_Mu3',
     'HLT_Mu5',
     'HLT_Mu7',
     'HLT_Mu9',
     'HLT_Mu11',
     'HLT_Mu13',
     'HLT_Mu15',
     'HLT_Mu15_Vtx2mm',
     'HLT_DoubleIsoMu3',
     'HLT_DoubleMu3',
     'HLT_DoubleMu3_Vtx2mm',
     'HLT_DoubleMu7_Z',
     'HLT_BTagIP_Jet180',
     'HLT_BTagIP_Jet120_Relaxed',
     'HLT_BTagIP_DoubleJet120',
     'HLT_BTagIP_DoubleJet60_Relaxed',
     'HLT_BTagIP_TripleJet70',
     'HLT_BTagIP_TripleJet40_Relaxed',
     'HLT_BTagIP_QuadJet40',
     'HLT_BTagMu_DoubleJet60_Relaxed',
     'HLT_BTagMu_TripleJet70',
     'HLT_BTagMu_TripleJet40_Relaxed',
     'HLT_BTagMu_QuadJet40',
     'HLT_BTagMu_QuadJet30_Relaxed',
     'HLT_BTagMu_HT370',
     'HLT_DoubleLooseIsoTau',
     'HLT_IsoEle8_IsoMu7',
     'HLT_IsoEle10_Mu10_L1R',
     'HLT_IsoEle10_BTagIP_Jet35',
     'HLT_IsoEle12_Jet40',
     'HLT_IsoEle12_DoubleJet80',
     'HLT_IsoEle5_TripleJet30',
     'HLT_IsoEle12_TripleJet60',
     'HLT_IsoEle12_QuadJet35',
     'HLT_IsoMu7_BTagIP_Jet35',
     'HLT_IsoMu7_BTagMu_Jet20',
     'HLT_IsoMu7_Jet40',
     'HLT_NoL2IsoMu8_Jet40',
     'HLT_Mu14_Jet50',
     'HLT_Mu5_TripleJet30'
#                               'HLT_IsoMu11',
#				'HLT_Mu15_L1Mu7',
#       			'HLT_Mu15',
#       			'HLT_DoubleMu3',
#       			'HLT_IsoEle15_L1I',
#       			'HLT_IsoEle18_L1R',
#       			'HLT_DoubleIsoEle10_L1I',
#				'HLT_DoubleIsoEle12_L1R'
     ]


#!/bin/python3

import math
import os
import random
import re
import sys

def decode(mat,m):
    """
    """
    code = ['' for j in range(m)]
    for v in mat:
        for i in range(m):
            code[i] += v[i]

    decoded = re.sub(r'(?<=[a-zA-Z0-9])[^a-zA-Z0-9]+(?=[a-zA-Z0-9])',' ',''.join(code))
    print(decoded)

nm = input().split()

n = int(nm[0])

m = int(nm[1])

matrix = []

for _ in range(n):
    matrix_item = input()
    matrix.append(matrix_item)
decode(matrix,m)


from functools import wraps

def fsm_log_by(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        arg_list = list(args)
        instance = arg_list.pop(0)

        if kwargs.get('by', False):
            instance.by = kwargs['by']

        if kwargs.get('description', False):
            instance.description = kwargs['description']

        out = func(instance, *arg_list, **kwargs)

        if kwargs.get('by', False):
            delattr(instance, 'by')

        if kwargs.get('description', False):
            delattr(instance, 'description')

        return out

    return wrapped


# -*- coding: utf-8 -*-
"""raw packet data

:mod:`pcapkit.protocols.raw` contains
:class:`~pcapkit.protocols.raw.Raw` only, which implements
extractor for unknown protocol, and constructs a
:class:`~pcapkit.protocols.protocol.Protocol` like object.

"""
import io

from pcapkit.corekit.infoclass import Info
from pcapkit.corekit.protochain import ProtoChain
from pcapkit.protocols.null import NoPayload
from pcapkit.protocols.protocol import Protocol
from pcapkit.utilities.exceptions import UnsupportedCall

__all__ = ['Raw']

class Raw(Protocol):
    """This class implements universal unknown protocol."""

    ##########################################################################
    # Properties.
    ##########################################################################

    # name of current protocol
    @property
    def name(self):
        """Name of current protocol.

        :rtype: Literal['Unknown']
        """
        return 'Unknown'

    # header length of current protocol
    @property
    def length(self):
        """Header length of current protocol.

        Raises:
            UnsupportedCall: This protocol doesn't support :attr:`length`.

        """
        raise UnsupportedCall(f"{self.__class__.__name__!r} object has no attribute 'length'")

    # name of next layer protocol
    @property
    def protocol(self):
        """Name of next layer protocol.

        Raises:
            UnsupportedCall: This protocol doesn't support :attr:`protocol`.

        """
        raise UnsupportedCall(f"{self.__class__.__name__!r} object has no attribute 'protocol'")

    ##########################################################################
    # Methods.
    ##########################################################################

    def read(self, length=None, *, error=None, **kwargs):  # pylint: disable=arguments-differ,unused-argument
        """Read raw packet data.

        Args:
            length (Optional[int]): Length of packet data.

        Keyword Args:
            error (Optional[str]): Parsing errors if any.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            DataType_Raw: The parsed packet data.

        """
        if length is None:
            length = len(self)

        raw = dict(
            packet=self._read_fileng(length),
            error=error or None,
        )

        return raw

    def make(self, **kwargs):
        """Make raw packet data.

        Keyword Args:
            packet (bytes): Raw packet data.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            bytes: Constructed packet data.

        """
        return bytes(kwargs.get('packet'))

    ##########################################################################
    # Data models.
    ##########################################################################

    def __post_init__(self, file, length=None, *, error=None, **kwargs):  # pylint: disable=arguments-differ
        """Post initialisation hook.

        Args:
            file (io.BytesIO): Source packet stream.
            length (Optional[int]): Length of packet data.

        Keyword Args:
            error (Optional[str]): Parsing errors if any (for parsing).
            **kwargs: Arbitrary keyword arguments.

        Would :mod:`pcapkit` encounter malformed packets, the original parsing
        error message will be provided as in ``error``.

        See Also:
            For construction argument, please refer to :meth:`make`.

        """
        if file is None:
            _data = self.make(**kwargs)
        else:
            _data = file.read(length)

        #: bytes: Raw packet data.
        self._data = _data
        #: io.BytesIO: Source packet stream.
        self._file = io.BytesIO(self._data)
        #: pcapkit.corekit.infoclass.Info: Parsed packet data.
        self._info = Info(self.read(length, error=error, **kwargs))

        #: pcapkit.protocols.null.NoPayload: Next layer (no payload).
        self._next = NoPayload()
        #: pcapkit.corekit.protochain.ProtoChain: Protocol chain from current layer.
        self._protos = ProtoChain(self.__class__, self.alias)

    @classmethod
    def __index__(cls):
        """Numeral registry index of the protocol.

        Raises:
            UnsupportedCall: This protocol has no registry entry.

        """
        raise UnsupportedCall(f'{cls.__name__!r} object cannot be interpreted as an integer')


"""
@description: 命名实体识别模块
@author: Cui Rui long
@email: xiaocuikindle@163.com
@time: 2019-02-27
@version: 0.0.1
"""
from collections import defaultdict

import jieba
import logging

from sementic_server.source.ner_task.entity_code import EntityCode
from sementic_server.source.ner_task.system_info import SystemInfo
from sementic_server.source.ner_task.model_tf_serving import ModelServing

logger = logging.getLogger("server_log")

class SemanticSearch(object):
    """
    通过调用 sentence_ner_entities 函数实现对：人名、组织结构名、地名和日期 的识别
    """

    def __init__(self, test_mode=False):

        self.system_info = SystemInfo(is_test=test_mode)

        self.client = ModelServing(self.system_info.MODE_NER, is_test=test_mode)

        self.config = self.system_info.get_config()

        self.entity_code = EntityCode()
        self.ner_entities = self.entity_code.get_ner_entities()
        self.code = self.entity_code.get_entity_code()

        self.labels_list = []
        self.labels_list_split = []
        self.__init_specific_label_combine()
        self.__init_jieba()

    def __init_specific_label_combine(self):
        """
        初始化labels_list和labels_list_split列表
        用于将出现的此类标签：“NAMECOMPANY” 分开成 “NAME#COMPANY”
        :return:
        """
        entities = self.entity_code.get_entities()
        for i in range(0, len(entities)):
            for j in range(0, len(entities)):
                if i != j:
                    self.labels_list.append(entities[i] + entities[j])
                    self.labels_list_split.append((entities[i] + "#" + entities[j]))

                    self.labels_list.append(entities[j] + entities[i])
                    self.labels_list_split.append((entities[j] + "#" + entities[i]))

    def __init_jieba(self):
        """
        可以给分词工具加入领域词汇辅助分词，加入公司名称可以有效提升分词工具对公司名称分词的准确度
        :return:
        """
        entities = self.entity_code.get_entities()
        for label in entities:
            jieba.add_word(label)

    def __combine_label(self, entities, label=None):
        """
        合并实体列表中相连且相同的label
        :param entities:
        :param label:
        :return:
        """
        pre_label = False
        first_label = None
        entities_copy = []
        for i in range(len(entities)):
            if entities[i][1] != label:
                pre_label = False
                if first_label is not None:
                    entities_copy.append(first_label)
                    first_label = None
                entities_copy.append(entities[i])
            elif pre_label is False and entities[i][1] == label:
                pre_label = True
                first_label = entities[i]
            elif pre_label and first_label is not None and entities[i][1] == label:
                temp = first_label
                first_label = [temp[0] + entities[i][0], temp[1]]

        if first_label is not None:
            entities_copy.append(first_label)

        return entities_copy

    def __combine_com_add(self, entities):
        """
        合并 COMPANYADDR 和 ADDRCOMPANY 这类实体为 COMPANY
        :param entities:
        :return:
        """
        company_index = -1
        addr_index = -1

        for i, entity in enumerate(entities):
            if self.ner_entities['COMPANY'] == entity[1]:
                company_index = i
            if self.ner_entities['ADDR'] == entity[1]:
                addr_index = i

        if company_index != -1 and addr_index != -1:
            if company_index == addr_index + 1:
                entities[company_index][0] = entities[addr_index][0] + entities[company_index][0]
                entities.remove(entities[addr_index])
            elif company_index == addr_index - 1:
                entities[company_index][0] = entities[company_index][0] + entities[addr_index][0]
                entities.remove(entities[addr_index])

    def __split_diff_labels(self, template_sen):
        """
        检测模板句中是否有不同的label相互连接的情况，eg. "ADDRNAME"，这种情况分词工具无法正确分词
        如果存在相连的label，使用“#”将两个label分开
        :param template_sen: 模板句子
        :return:
        """
        for i, label in enumerate(self.labels_list):
            if label in template_sen:
                template_sen = template_sen.replace(label, self.labels_list_split[i])
        return template_sen

    def __convert_output_data_format(self, data_param):
        """
        将 data_param 数据转换成问答图模块需要的数据格式
        :param data_param:
        :return:
        """
        output = defaultdict()
        output["query"] = data_param["raw_input"]
        output["template"] = data_param["new_input"]
        entity = []
        for key, values in data_param["labels"].items():
            for v in values:
                begin = data_param["raw_input"].find(v)
                entity.append(
                    {"type": key, "value": v, "code": self.code[key], "begin": begin,
                     "end": begin + len(v) + 1 if begin != -1 else -1})
        output["entity"] = entity
        return output

    def sentence_ner_entities(self, result):
        """
        使用 BERT 模型对句子进行实体识别，返回标记实体的句子
        :param result: account_label 模块返回的结果
        :return:
            entities: 列表，存储的是 BERT 识别出来的实体信息:(word, label)
            result: account_label 模块返回的结果
        """
        sentence = result["new_input"]

        sentence, pred_label_result = self.client.send_grpc_request_ner(sentence)

        word = ""
        label = ""
        entities = []

        if sentence is None or pred_label_result is None:
            return entities, result

        for i in range(len(sentence)):
            temp_label = pred_label_result[i]
            if temp_label[0] == 'B':
                if word != "":
                    if "##" in word:
                        word = word.replace('##', '')
                    entities.append([word, label])
                    word = ""

                if temp_label[2:] == 'ORG':
                    label = self.ner_entities['COMPANY']
                elif temp_label[2:] == 'PER':
                    label = self.ner_entities['NAME']
                elif temp_label[2:] == 'DATE':
                    label = self.ner_entities['DATE']
                else:
                    label = self.ner_entities['ADDR']

                word += sentence[i]
            elif temp_label[0] == 'I' and word != "":
                word += sentence[i]
            elif temp_label == 'O' and word != "":
                if "##" in word:
                    word = word.replace('##', '')
                entities.append([word, label])
                word = ""
                label = ""
        if word != "":
            if "##" in word:
                word = word.replace('##', '')
            entities.append([word, label])

        if len(entities) != 0:
            entities = self.__combine_label(entities, label=self.ner_entities['ADDR'])
            entities = self.__combine_label(entities, label=self.ner_entities['COMPANY'])
            entities = self.__combine_label(entities, label=self.ner_entities['NAME'])
            entities = self.__combine_label(entities, label=self.ner_entities['DATE'])

            self.__combine_com_add(entities)
            entities = self.__combine_label(entities, label=self.ner_entities['COMPANY'])

        for (word, label) in entities:
            result["new_input"] = result["new_input"].replace(word, label)
            result["labels"].setdefault(label, []).append(word)

        result = self.__convert_output_data_format(result)
        return result


from __future__ import absolute_import
import logging
import sys

from django.test import override_settings
from logutils import dictconfig
from mock import patch, Mock
from pytest import raises
from six.moves import zip
from testfixtures import log_capture
from time import sleep
from unittest import TestCase

from celery import signals as celery_signals
from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.contrib.auth import signals as auth_signals
from django.core.cache import cache as django_cache
from django.core.management import call_command
from django.http import HttpResponse, HttpResponseForbidden, Http404
from django.test import TestCase as DjangoTestCase
from django.test.client import RequestFactory

try:
    from django.urls import reverse
except ImportError:
    from django.core.urlresolvers import reverse

from django_statsd import middleware
from django_statsd.clients import StatsdClientProxy
from django_statsd.patches import utils, import_patches
from django_statsd.patches.db import (
    patch as db_patch,
    patched_callproc,
    patched_execute,
    patched_executemany,
)
from django_statsd.patches.cache import (
    patch as cache_patch,
    StatsdTracker,
)
from django_statsd.views import process_key, _process_summaries

cfg = {
    'version': 1,
    'formatters': {},
    'handlers': {
        'test_statsd_handler': {
            'class': 'django_statsd.loggers.errors.StatsdHandler',
        },
    },
    'loggers': {
        'statsd': {
            'handlers': ['test_statsd_handler'],
            'level': 'INFO',
        },
        'test.logging': {
            'handlers': ['test_statsd_handler'],
        },
    },
}

class TestIncr(DjangoTestCase):

    def setUp(self):
        self.req = RequestFactory().get('/')
        self.res = HttpResponse()

    def test_graphite_response(self):
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_response(self.req, self.res)
        assert statsd_mock.incr.called

    def test_graphite_response_authenticated(self):
        self.req.user = Mock()
        if DJANGO_VERSION < (1, 10):
            self.req.user.is_authenticated.return_value = True
        else:
            self.req.user.is_authenticated = True
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.incr.call_count, 2)

    def test_graphite_exception(self):
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_exception(self.req, None)
        assert statsd_mock.incr.called

    def test_graphite_exception_authenticated(self):
        self.req.user = Mock()
        if DJANGO_VERSION < (1, 10):
            self.req.user.is_authenticated.return_value = True
        else:
            self.req.user.is_authenticated = True
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_exception(self.req, None)
        self.assertEqual(statsd_mock.incr.call_count, 2)

    def test_graphite_exception_404(self):
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_exception(self.req, Http404())
        assert not statsd_mock.incr.called

    def test_graphite_exception_404_authenticated(self):
        self.req.user = Mock()
        if DJANGO_VERSION < (1, 10):
            self.req.user.is_authenticated.return_value = True
        else:
            self.req.user.is_authenticated = True
        gmw = middleware.GraphiteMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_exception(self.req, Http404())
        self.assertEqual(statsd_mock.incr.call_count, 0)

class TestTiming(TestCase):

    def setUp(self):
        self.req = RequestFactory().get('/')
        self.res = HttpResponse()

    def test_ping_timing(self):
        with patch('django_statsd.management.commands.statsd_ping.statsd') as statsd_mock:
            call_command('statsd_ping', key='test.timing')
        self.assertEqual(statsd_mock.timing.call_count, 1)
        self.assertEqual(statsd_mock.timing.call_args_list[0][0][0], 'test.timing')

    def test_request_timing(self):
        func = lambda x: x  # noqa: E731
        gmw = middleware.GraphiteRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 3)
        names = ['view.%s.%s.GET' % (func.__module__, func.__name__),
                 'view.%s.GET' % func.__module__,
                 'view.GET']
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

    @patch.object(settings, 'STATSD_VIEW_TIMER_DETAILS', False)
    def test_request_timing_without_details(self):
        func = lambda x: x  # noqa: E731
        gmw = middleware.GraphiteRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 1)
        names = ['view.%s.%s.GET' % (func.__module__, func.__name__), ]
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

    def test_request_timing_view_not_a_function(self):
        func = object
        gmw = middleware.GraphiteRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 3)
        names = ['view.%s.%s.GET' % (func.__class__.__module__, func.__class__.__name__),
                 'view.%s.GET' % func.__class__.__module__,
                 'view.GET']
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

    def test_request_timing_view_no_timings(self):
        func = object
        gmw = middleware.GraphiteRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
        del self.req._start_time
        gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 0)

    def test_request_timing_exception(self):
        func = lambda x: x  # noqa: E731
        gmw = middleware.GraphiteRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
            gmw.process_exception(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 3)
        names = ['view.%s.%s.GET' % (func.__module__, func.__name__),
                 'view.%s.GET' % func.__module__,
                 'view.GET']
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

    def test_request_timing_tastypie(self):
        func = lambda x: x  # noqa: E731
        gmw = middleware.TastyPieRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), {
                'api_name': 'my_api_name',
                'resource_name': 'my_resource_name'
            })
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 3)
        names = ['view.my_api_name.my_resource_name.GET',
                 'view.my_api_name.GET',
                 'view.GET']
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

    def test_request_timing_tastypie_fallback(self):
        func = lambda x: x  # noqa: E731
        gmw = middleware.TastyPieRequestTimingMiddleware()
        with patch('django_statsd.middleware.statsd') as statsd_mock:
            gmw.process_view(self.req, func, tuple(), dict())
            gmw.process_response(self.req, self.res)
        self.assertEqual(statsd_mock.timing.call_count, 3)
        names = ['view.%s.%s.GET' % (func.__module__, func.__name__),
                 'view.%s.GET' % func.__module__,
                 'view.GET']
        for expected, (args, kwargs) in zip(names, statsd_mock.timing.call_args_list):
            self.assertEqual(expected, args[0])

class TestClient(TestCase):

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar')
    def test_toolbar_incr(self):
        statsd = StatsdClientProxy()

        self.assertEqual(statsd.cache, {})
        statsd.incr('testing')
        self.assertEqual(statsd.cache, {'testing|count': [[1, 1]]})

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar')
    def test_toolbar_decr(self):
        statsd = StatsdClientProxy()

        self.assertEqual(statsd.cache, {})
        statsd.decr('testing')
        self.assertEqual(statsd.cache, {'testing|count': [[-1, 1]]})

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar')
    def test_toolbar_timing(self):
        statsd = StatsdClientProxy()

        self.assertEqual(statsd.timings, [])
        statsd.timing('testing', 1)
        self.assertEqual(statsd.timings[0][0], 'testing|timing')
        self.assertEqual(statsd.timings[0][2], 1)

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar')
    def test_toolbar_gauge(self):
        statsd = StatsdClientProxy()

        self.assertEqual(statsd.cache, {})
        statsd.gauge('testing', 1)
        self.assertEqual(statsd.cache, {'testing|gauge': [[1, 1]]})
        statsd.gauge('testing', 1, delta=True)
        self.assertEqual(statsd.cache, {'testing|gauge': [[1, 1], [1, 1]]})

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar')
    def test_toolbar_set(self):
        statsd = StatsdClientProxy()

        self.assertEqual(statsd.cache, {})
        statsd.set('testing', 1)
        self.assertEqual(statsd.cache, {'testing|set': [[1, 1]]})

    @override_settings(STATSD_CLIENT='django_statsd.clients.log')
    @log_capture()
    def test_log_client(self, l):
        statsd = StatsdClientProxy()

        statsd.timing('testing.timing', 1)
        statsd.incr('testing.incr')
        statsd.decr('testing.decr')
        statsd.gauge('testing.gauge', 1)
        l.check(('statsd', 'INFO', 'Timing: testing.timing, 1, 1'),
                ('statsd', 'INFO', 'Increment: testing.incr, 1, 1'),
                ('statsd', 'INFO', 'Decrement: testing.decr, 1, 1'),
                ('statsd', 'INFO', 'Gauge: testing.gauge, 1, 1'))

    @override_settings(STATSD_CLIENT='django_statsd.clients.toolbar', STATSD_REFRESH_SECONDS=2)
    def test_refresh_client(self):
        statsd = StatsdClientProxy()
        statsd.incr('test')
        old_client = statsd._client
        statsd.incr('test')
        self.assertEqual(statsd._client, old_client)
        sleep(1)
        statsd.incr('test')
        self.assertEqual(statsd._client, old_client)
        sleep(1)
        statsd.incr('test')
        self.assertNotEquals(old_client, statsd._client)

class TestSignals(DjangoTestCase):

    def setUp(self):
        class Sender(object):
            def __init__(self):
                self.name = 'testing'

            def __str__(self):
                return self.name

        self.sender = Sender()

    def test_celery_signals(self):
        with patch('django_statsd.celery_hooks.statsd') as statsd_mock:
            celery_signals.before_task_publish.send(sender=self.sender)
            celery_signals.after_task_publish.send(sender=self.sender)
            celery_signals.task_prerun.send(sender=self.sender, task_id='1')
            celery_signals.task_postrun.send(sender=self.sender, task_id='1')
            celery_signals.task_postrun.send(sender=self.sender, task_id='2')
            celery_signals.task_success.send(sender=self.sender)
            celery_signals.task_failure.send(sender=self.sender)
            celery_signals.task_retry.send(sender=self.sender)
            celery_signals.task_revoked.send(sender=self.sender)
            celery_signals.task_unknown.send(sender=self.sender)
            celery_signals.task_rejected.send(sender=self.sender)
        self.assertEqual(statsd_mock.incr.call_count, 8)
        self.assertEqual(statsd_mock.timing.call_count, 1)

    def test_auth_signals(self):
        req = RequestFactory().get('/')
        user = Mock()
        user.backend = 'fake_backend'

        with patch('django_statsd.models.statsd') as statsd_mock:
            auth_signals.user_logged_in.send(self.sender, request=req, user=user)
            auth_signals.user_logged_out.send(self.sender, request=req, user=user)
            auth_signals.user_login_failed.send(self.sender, credentials={})

        self.assertEqual(statsd_mock.incr.call_count, 4)

# This is primarily for Zamboni, which loads in the custom middleware
# classes, one of which, breaks posts to our url. Let's stop that.
@patch.object(settings, 'MIDDLEWARE', [])
@patch.object(settings, 'MIDDLEWARE_CLASSES', [])
class TestRecord(DjangoTestCase):

    urls = 'django_statsd.urls'

    def setUp(self):
        super(TestRecord, self).setUp()
        self.url = reverse('django_statsd_record')
        settings.STATSD_RECORD_GUARD = None
        self.good = {'client': 'boomerang', 'nt_nav_st': 1,
                     'nt_domcomp': 3}
        self.stick = {'client': 'stick',
                      'window.performance.timing.domComplete': 123,
                      'window.performance.timing.domInteractive': 456,
                      'window.performance.timing.domLoading': 789,
                      'window.performance.timing.navigationStart': 0,
                      'window.performance.navigation.redirectCount': 3,
                      'window.performance.navigation.type': 1}

    def test_no_client(self):
        assert self.client.get(self.url).status_code == 400

    def test_no_valid_client(self):
        assert self.client.get(self.url, {'client': 'no'}).status_code == 400

    def test_boomerang_almost(self):
        assert self.client.get(self.url,
                               {'client': 'boomerang'}).status_code == 400

    def test_boomerang_minimum(self):
        content = self.client.get(self.url,
                                  {'client': 'boomerang',
                                   'nt_nav_type': 'undefined',
                                   'nt_nav_st': 1}).content.decode()
        assert(content == 'recorded')

    def test_boomerang_undefined_value(self):
        content = self.client.get(self.url,
                                  {'client': 'boomerang',
                                   'nt_nav_type': 'undefined',
                                   'nt_nav_st': 1}).content.decode()
        assert(content == 'recorded')

    @patch.object(settings, 'STATSD_RECORD_KEYS', ['unknown.key', 'window.performance.timing.navigationStart'])
    def test_boomerang_unknown_key(self):
        content = self.client.get(self.url,
                                  {'client': 'boomerang',
                                   'nt_nav_st': 1}).content.decode()
        assert(content == 'recorded')

    @patch('django_statsd.views.process_key')
    def test_boomerang_something(self, process_key):
        content = self.client.get(self.url, self.good).content.decode()
        assert content == 'recorded'
        assert process_key.called

    def test_boomerang_post(self):
        assert self.client.post(self.url, self.good).status_code == 405

    def test_good_guard(self):
        settings.STATSD_RECORD_GUARD = lambda r: None
        assert self.client.get(self.url, self.good).status_code == 200

    def test_good_guard_no_result(self):
        settings.STATSD_RECORD_GUARD = lambda r: False
        assert self.client.get(self.url, self.good).status_code == 200

    def test_bad_guard(self):
        settings.STATSD_RECORD_GUARD = lambda r: HttpResponseForbidden()
        assert self.client.get(self.url, self.good).status_code == 403

    def test_bad_guard_not_callable(self):
        settings.STATSD_RECORD_GUARD = [1]
        with raises(ValueError):
            self.client.get(self.url, self.good)

    def test_stick_get(self):
        assert self.client.get(self.url, self.stick).status_code == 405

    @patch('django_statsd.views.process_key')
    def test_stick(self, process_key):
        assert self.client.post(self.url, self.stick).status_code == 200
        assert process_key.called

    def test_stick_start(self):
        data = self.stick.copy()
        del data['window.performance.timing.navigationStart']
        assert self.client.post(self.url, data).status_code == 400

    @patch('django_statsd.views.process_key')
    def test_stick_missing(self, process_key):
        data = self.stick.copy()
        del data['window.performance.timing.domInteractive']
        assert self.client.post(self.url, data).status_code == 200
        assert process_key.called

    def test_stick_garbage(self):
        data = self.stick.copy()
        data['window.performance.timing.domInteractive'] = '<alert>'
        assert self.client.post(self.url, data).status_code == 400

    def test_stick_some_garbage(self):
        data = self.stick.copy()
        data['window.performance.navigation.redirectCount'] = '<alert>'
        assert self.client.post(self.url, data).status_code == 400

    def test_stick_more_garbage(self):
        data = self.stick.copy()
        data['window.performance.navigation.type'] = '<alert>'
        assert self.client.post(self.url, data).status_code == 400

class TestViewFunctions(DjangoTestCase):

    def test_process_key_timing(self):
        with patch('django_statsd.views.statsd') as statsd_mock:
            process_key(1, 'timing', '1')
            process_key(1, 'timing', '2')
            process_key(2, 'timing', '1')
        values = [0, 1, 0]
        for expected, (args, kwargs) in zip(values, statsd_mock.timing.call_args_list):
            self.assertEqual('timing', args[0])
            self.assertEqual(expected, args[1])

    def test_process_key_navigation_type(self):
        with patch('django_statsd.views.statsd') as statsd_mock:
            process_key(1, 'window.performance.navigation.type', '0')
            process_key(1, 'window.performance.navigation.type', '1')
            process_key(1, 'window.performance.navigation.type', '2')
            process_key(1, 'window.performance.navigation.type', '255')
        names = ['window.performance.navigation.type.navigate',
                 'window.performance.navigation.type.reload',
                 'window.performance.navigation.type.back_forward',
                 'window.performance.navigation.type.reserved', ]
        for expected, (args, kwargs) in zip(names, statsd_mock.incr.call_args_list):
            self.assertEqual(expected, args[0])

    def test_process_key_redirect_count(self):
        with patch('django_statsd.views.statsd') as statsd_mock:
            process_key(1, 'window.performance.navigation.redirectCount', '1')
        self.assertEqual(statsd_mock.incr.call_args_list[0][0][0], 'window.performance.navigation.redirectCount')

    def test_process_key_none(self):
        with patch('django_statsd.views.statsd') as statsd_mock:
            process_key(1, 'random', '1')
        assert not statsd_mock.incr.called
        assert not statsd_mock.timing.called

    def test_process_summaries(self):
        timings = {'window.performance.timing.domComplete': 123,
                   'window.performance.timing.domInteractive': 456,
                   'window.performance.timing.domLoading': 789,
                   'window.performance.timing.navigationStart': 0,
                   'window.performance.timing.responseStart': 5,
                   'window.performance.timing.loadEventEnd': 1000,
                   'window.performance.navigation.redirectCount': 3,
                   'window.performance.navigation.type': 1}
        with patch('django_statsd.views.statsd') as statsd_mock:
            _process_summaries(0, timings)
        assert statsd_mock.timing.called
        self.assertEqual(statsd_mock.timing.call_count, 4)

class TestErrorLog(DjangoTestCase):

    def setUp(self):
        dictconfig.dictConfig(cfg)
        self.log = logging.getLogger('test.logging')

    def division_error(self):
        try:
            1 / 0
        except Exception:
            return sys.exc_info()

    def test_emit(self):
        with patch('django_statsd.loggers.errors.statsd') as statsd_mock:
            self.log.error('blargh!', exc_info=self.division_error())
        assert statsd_mock.incr.call_args[0][0] == 'error.zerodivisionerror'

    def test_not_emit(self):
        with patch('django_statsd.loggers.errors.statsd') as statsd_mock:
            self.log.error('blargh!')
        assert not statsd_mock.incr.called

class TestPatchMethod(DjangoTestCase):

    def setUp(self):
        super(TestPatchMethod, self).setUp()

        class DummyClass(object):

            def sumargs(self, a, b, c=3, d=4):
                return a + b + c + d

            def badfn(self, a, b=2):
                raise ValueError

        self.cls = DummyClass

    def test_late_patching(self):
        """
        Objects created before patching should get patched as well.
        """
        def patch_fn(original_fn, self, *args, **kwargs):
            return original_fn(self, *args, **kwargs) + 10

        obj = self.cls()
        self.assertEqual(obj.sumargs(1, 2, 3, 4), 10)
        utils.patch_method(self.cls, 'sumargs')(patch_fn)
        self.assertEqual(obj.sumargs(1, 2, 3, 4), 20)

    def test_doesnt_call_original_implicitly(self):
        """
        Original fn must be called explicitly from patched to be
        executed.
        """
        def patch_fn(original_fn, self, *args, **kwargs):
            return 10

        with self.assertRaises(ValueError):
            obj = self.cls()
            obj.badfn(1, 2)

        utils.patch_method(self.cls, 'badfn')(patch_fn)
        self.assertEqual(obj.badfn(1, 2), 10)

    def test_args_kwargs_are_honored(self):
        """
        Args and kwargs must be honored between calls from the patched to
        the original version.
        """
        def patch_fn(original_fn, self, *args, **kwargs):
            return original_fn(self, *args, **kwargs)

        utils.patch_method(self.cls, 'sumargs')(patch_fn)
        obj = self.cls()
        self.assertEqual(obj.sumargs(1, 2), 10)
        self.assertEqual(obj.sumargs(1, 1, d=1), 6)
        self.assertEqual(obj.sumargs(1, 1, 1, 1), 4)

    def test_patched_fn_can_receive_arbitrary_arguments(self):
        """
        Args and kwargs can be received arbitrarily with no contraints on
        the patched fn, even if the original_fn had a fixed set of
        allowed args and kwargs.
        """
        def patch_fn(original_fn, self, *args, **kwargs):
            return args, kwargs

        utils.patch_method(self.cls, 'badfn')(patch_fn)
        obj = self.cls()
        self.assertEqual(obj.badfn(1, d=2), ((1,), {'d': 2}))
        self.assertEqual(obj.badfn(1, d=2), ((1,), {'d': 2}))
        self.assertEqual(obj.badfn(1, 2, c=1, d=2), ((1, 2), {'c': 1, 'd': 2}))

class TestApplyPatching(DjangoTestCase):
    @patch.object(settings, 'STATSD_PATCHES', ['django_statsd.patches.db', ])
    def test_patching(self):
        """ Test the patching itself """
        import_patches()

class TestCacheWrappingPatching(DjangoTestCase):
    def test_patching(self):
        """ Test the patching itself """
        cache_patch()

    def test_statsdtracker_basecache(self):
        cache = StatsdTracker(django_cache)
        cache.set('A', 1)
        value = cache.get('A')
        self.assertEqual(value, 1)
        assert cache.default_timeout == 300

class TestCursorWrapperPatching(DjangoTestCase):
    example_queries = {
        'select': 'select * from something;',
        'insert': 'insert (1, 2) into something;',
        'update': 'update something set a=1;',
    }

    def test_patching(self):
        """ Test the patching itself """
        db_patch()

    def test_patched_callproc_calls_timer(self):
        for operation, query in list(self.example_queries.items()):
            with patch('django_statsd.patches.db.statsd') as statsd_mock:
                client = Mock(executable_name='client_executable_name')
                db = Mock(executable_name='name', alias='alias', client=client)
                instance = Mock(db=db)

                patched_callproc(lambda *args, **kwargs: None, instance, query)

                self.assertEqual(statsd_mock.timer.call_count, 1)
                self.assertEqual(statsd_mock.timer.call_args[0][0], 'db.client_executable_name.alias.callproc.%s' % operation)

    def test_patched_execute_calls_timer(self):
        for operation, query in list(self.example_queries.items()):
            with patch('django_statsd.patches.db.statsd') as statsd_mock:
                client = Mock(executable_name='client_executable_name')
                db = Mock(executable_name='name', alias='alias', client=client)
                instance = Mock(db=db)

                patched_execute(lambda *args, **kwargs: None, instance, query)

                self.assertEqual(statsd_mock.timer.call_count, 1)
                self.assertEqual(statsd_mock.timer.call_args[0][0], 'db.client_executable_name.alias.execute.%s' % operation)

    def test_patched_executemany_calls_timer(self):
        for operation, query in list(self.example_queries.items()):
            with patch('django_statsd.patches.db.statsd') as statsd_mock:
                client = Mock(executable_name='client_executable_name')
                db = Mock(executable_name='name', alias='alias', client=client)
                instance = Mock(db=db)

                patched_executemany(lambda *args, **kwargs: None, instance, query)

                self.assertEqual(statsd_mock.timer.call_count, 1)
                self.assertEqual(statsd_mock.timer.call_args[0][0], 'db.client_executable_name.alias.executemany.%s' % operation)


from dateutil import parser
from datetime import datetime

def convert_from_str_timestamps(original_timestamps, target_format):
    """
    Converts list of timestapms from string format to datetime or unix
    """

    if target_format == "datetime":
        output_timestamps = [parser.parse(elem)
                             for elem in original_timestamps]

    elif target_format == "unix":
        output_timestamps \
            = [round(datetime.strptime(elem, "%Y-%m-%d %H:%M:%S")
                     .timestamp())
               for elem in original_timestamps]
    else:
        raise ValueError("Invalid target_format name")

    return output_timestamps

def convert_from_unix_timestamps(original_timestamps, target_format):
    """
    Converts list of timestapms from unix format to datetime or string
    """

    if target_format == "datetime":
        output_timestamps = [datetime.fromtimestamp(elem)
                             for elem in original_timestamps]
    elif target_format == "str":
        output_timestamps \
            = [datetime.fromtimestamp(elem).strftime("%Y-%m-%d %H:%M:%S")
               for elem in original_timestamps]
    else:
        raise ValueError("Invalid target_format name")

    return output_timestamps


# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 09:07:24 2015

@author: rodrigopena

Rotation Based Projector

"""

#%% Import libraries

import numpy as np

import scipy.interpolate
import scipy.misc
import scipy.ndimage.interpolation

#%% Open the image file

phantom = scipy.misc.imread("Phantom.jpg")

#%% Setup variables
angle = 0.5
n_projections = 180 / angle
image = np.copy(phantom)

#%% Loop on the number of projections
for p in range(int(n_projections)):

    #%% Rotate the image
    rotatedImage = scipy.ndimage.interpolation.rotate(image, angle, order=3,
                                                      reshape=False,
                                                      mode='constant',
                                                      cval=0.0)

    #%% Sum across columns
    imageSum = np.sum(rotatedImage, axis=0)

    #%% Stack projection views
    if p == 0:
        sinogram = np.copy(imageSum)
    else:
        sinogram = np.vstack((sinogram, imageSum))

    #%% Set current rotation as the next image
    image = np.copy(rotatedImage)

#%% Save resulting image

# Normalize
sinogram = sinogram.astype('float')
sinogram -= np.min(sinogram.ravel())
sinogram /= np.max(sinogram.ravel())
sinogram *= 255

scipy.misc.imsave("Sinogram.jpg", sinogram)


import sys
import logging
from pyspark.sql import SparkSession

import thoughtworks.citibike.CitibikeTransformer as CitiBikeTransformer

def main(args):

    if len(args) < 4:
        logging.warning("Input source and output path are required")
        sys.exit(1)
    else:
        input_path = args[2]
        output_path = args[3]

    spark = SparkSession.builder.appName("Citibike Transformer").getOrCreate()
    logging.info("Application Initialized: " + spark.sparkContext.appName)
    CitiBikeTransformer.run(spark, input_path, output_path)
    logging.info("Application Done: " + spark.sparkContext.appName)

    spark.stop()


from fractions import gcd
p = 1000000007

def power(x, y, diff):
    res = 1
    x = x % diff
    while y > 0:
        if y & 1:
            res = (res*x) % diff

        y = y >> 1
        x = (x*x) % diff

    return res

t = int(input())
while t:
    a,b,n = map(int,input().split(' '))
    d = a - b
    if d == 0:
        s = power(a, n, p) + power(b, n, p)
        s = s % p
        print(s)
    else:
        s = (power(a, n, d) + power(b, n, d)) % d
        #if s == 0:
        #    res = d % p
        #    print(res)
        #    continue
        g = gcd(s, d)
        g = g % p
        print(g)
    t = t-1


#verific if is leap year
#Enter, only, with year
#

def is_leap(year):
    leap = False
    if ((year%400 == 0) & (year%4 == 0)):
        return not leap
    elif ((year%100 != 0) & (year%4 == 0)):
        return not leap
    elif year%100 == 0:
        return leap
    elif (year%400 != 0) | (year%4 != 0):
        return leap
year = int(input())
print(is_leap(year))


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sys
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

testSet = []
testCoords = []

def evaluate_individual_performances(outputPath, trainSetCoordsPath, testSetCoordsPath, aruco, train_test_data):

    global testSet, testCoords
    # aruco = np.array(aruco)
    # m_id = str(aruco[0])
    m_id = aruco
    # Regression Degree
    DEG = 2

    # Train Set
    # Slow Feature Values
    trainSetPath = outputPath + '/Evaluation_Arrays/' + 'train_'+m_id+'_slowFeatures.npy'

    # Marker detection results
    detectionResultsTrainSetPath = outputPath + '/' + 'result_train.csv'

    # Load Files
    trainSet = np.load(trainSetPath)
    # trainSetCoords = np.loadtxt(trainSetCoordsPath, delimiter=',', usecols=(4, 5))
    trainSetCoords = np.loadtxt(trainSetCoordsPath)[:, 0:2]
    detectionResultsTrainSet = pd.read_csv(detectionResultsTrainSetPath)

    markerSquaresTrainSet = detectionResultsTrainSet[
        [m_id + '_bb_x1', m_id + '_bb_y1', m_id + '_bb_x2', m_id + '_bb_y2', m_id + '_bb_x3', m_id + '_bb_y3', m_id +
         '_bb_x4', m_id + '_bb_y4']].values

    # Keep only the coordinates with valid marker detection (Train/Test Set)
    trainCoords = []
    for i in range(trainSetCoords.shape[0]):
        if markerSquaresTrainSet[i].sum() != 0:
            trainCoords.append(trainSetCoords[i, :])
    trainCoords = np.array(trainCoords)

    print("\nActual coordinates\n ")
    print("Train Set: (%d,%d) " % trainSetCoords.shape)
    print("Train Set after marker detection: (%d,%d) " % trainCoords.shape)

    # Test set
    if train_test_data:
        testSetPath = outputPath + '/Evaluation_Arrays/' + 'test_'+m_id+'_slowFeatures.npy'
        detectionResultsTestSetPath = outputPath + '/' + 'result_test.csv'
        testSet = np.load(testSetPath)
        # testSetCoords = np.loadtxt(testSetCoordsPath, delimiter=',', usecols=(4, 5))
        testSetCoords = np.loadtxt(testSetCoordsPath)[:, 0:2]
        detectionResultsTestSet = pd.read_csv(detectionResultsTestSetPath)
        markerSquaresTestSet = detectionResultsTestSet[
            [m_id + '_bb_x1', m_id + '_bb_y1', m_id + '_bb_x2', m_id + '_bb_y2', m_id + '_bb_x3', m_id + '_bb_y3',
             m_id +
             '_bb_x4', m_id + '_bb_y4']].values
        testCoords = []
        for i in range(testSetCoords.shape[0]):
            if markerSquaresTestSet[i].sum() != 0:
                testCoords.append(testSetCoords[i, :])
        testCoords = np.array(testCoords)
        print("Test Set: (%d,%d) " % testSetCoords.shape)
        print("Test Set  after marker detection: (%d,%d) " % testCoords.shape)

    # Perform Regression
    #####################################################
    polyRegressor = PolynomialFeatures(degree=DEG)
    fig, ax = plt.subplots()

    if train_test_data:
        polyFeatureTrainingSet = polyRegressor.fit_transform(trainSet)
        polyFeaturesTestSet = polyRegressor.fit_transform(testSet)

        regressor_x = LinearRegression()
        regressor_x.fit(polyFeatureTrainingSet, trainCoords[:, 0])  # x-coordinates

        regressor_y = LinearRegression()
        regressor_y.fit(polyFeatureTrainingSet, trainCoords[:, 1])  # y-coordinates

        predicted_X = regressor_x.predict(polyFeaturesTestSet)
        predicted_Y = regressor_y.predict(polyFeaturesTestSet)

        prediction_X = predicted_X.reshape(predicted_X.shape[0], 1)
        prediction_Y = predicted_Y.reshape(predicted_Y.shape[0], 1)
        predictedCoordinates = np.hstack([prediction_X, prediction_Y])
        # Calculate Error
        ######################################################
        MAE = mean_absolute_error(testCoords, predictedCoordinates)
        print("\nMean Absolute Error: %f [m]\n" % MAE)
        # Visualize
        ######################################################
        ax.plot(testCoords[:, 0], testCoords[:, 1], 'b.', lw=1, label='Ground truth')
        ax.plot(prediction_X, prediction_Y, 'r.', lw=1, label='Estimation')

    else:
        polyFeatureTrainingSet = polyRegressor.fit_transform(trainSet)
        # polyFeaturesTestSet = polyRegressor.fit_transform(testSet)

        X_train, X_test, y_train, y_test = train_test_split(polyFeatureTrainingSet, trainCoords, test_size=0.33,
                                                            random_state=42)

        regressor_x = LinearRegression()
        regressor_x.fit(X_train, y_train[:, 0])  # x-coordinates

        regressor_y = LinearRegression()
        regressor_y.fit(X_train, y_train[:, 1])  # y-coordinates

        predicted_X = regressor_x.predict(X_test)
        predicted_Y = regressor_y.predict(X_test)

        prediction_X = predicted_X.reshape(predicted_X.shape[0], 1)
        prediction_Y = predicted_Y.reshape(predicted_Y.shape[0], 1)
        predictedCoordinates = np.hstack([prediction_X, prediction_Y])

        MAE = mean_absolute_error(y_test, predictedCoordinates)
        print("\nMean Absolute Error: %f [m]\n" % MAE)

        ax.plot(y_test[:, 0], y_test[:, 1], 'b.', lw=1, label='Ground truth')
        ax.plot(prediction_X, prediction_Y, 'r.', lw=1, label='Estimation')

    plt.title('Mean Absolute Error: ' + "{:.2f}".format(MAE), fontweight='bold')
    legend = ax.legend(loc='lower right', shadow=True)
    plt.xlabel('X [m]', fontsize=16)
    plt.ylabel('Y [m]', fontsize=16)
    plt.tick_params(top='off', bottom='on', left='on', right='off', labelleft='on', labelbottom='on')
    plt.rc('font', weight='bold')
    plt.rc('legend', **{'fontsize': 8})
    plt.gca().set_aspect('equal', adjustable='box')
    plt.tick_params(labelsize=12)
    plt.savefig(outputPath + '/' + 'marker_'+m_id+'_result.pdf', dpi=1200, bbox_inches='tight')  # Save?
    plt.show()

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("usage: python executeTestSet")
        print("     image folder        # path to the folder containing images")
        print("     total Number of Images in the folder containing images     ")
        print("     image suffix        # suffix of the image file")
        print("     Model Name          # Trained model name")
        print("\nExample: python executeTestSet.py images/ 1600 .png train\n")
        sys.exit()
    evaluate_individual_performances(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])


import datetime
import pytz
from .models import UserDetails, InterviewDetails

class validators:

    def __init__(self, startTime, endTime, participants):
        self.startTime = startTime
        self.endTime = endTime
        self.participants = participants
        self.valid = True
        self.errorMessage = ""
        self.leastParticipantsAllowed = 2

    def isvalid(self):
        return self.valid

    def setvalid(self, value):
        self.valid = value

    def getErrorMessage(self):
        return self.errorMessage

    def appendErrorMessage(self, message):
        if self.errorMessage == "":
            self.errorMessage = message
        else:
            self.errorMessage = self.errorMessage + ', ' + message

    def validateOverlappings(self, oldDataId=-1):
        # checking overlapping inerviews
        for user in self.participants:
            scheduledInterviews = InterviewDetails.objects.filter(
                participants=user)
            for interview in scheduledInterviews:
                if oldDataId != interview.id and self.checkOverlapping(interview) == False:
                    self.setvalid(False)
                    self.appendErrorMessage(
                        "User with userId = "+str(user.id)+" has overlapping Interviews")

    def checkOverlapping(self, slot):
        # checking overlaps
        if slot.startTime >= self.endTime or slot.endTime <= self.startTime:
            return True
        else:
            return False

    def validateCountofParticipants(self):
        # checking if there are less than 2 participants
        if len(self.participants) < self.leastParticipantsAllowed:
            self.setvalid(False)
            self.appendErrorMessage("There must be at least 2 users")

    def validateTime(self):
        # checks if the time mentioned is correct or not
        if self.startTime > self.endTime:
            self.setvalid(False)
            self.appendErrorMessage("Start Time can't be > End Time")

        # checks if interview is scheduled in the past time
        currentTime = datetime.datetime.now().replace(tzinfo=pytz.UTC)
        if self.startTime < currentTime or self.endTime < currentTime:
            self.setvalid(False)
            self.appendErrorMessage(
                "Interviews in the past can't be scheduled")


import numpy as np
import tensorflow.keras.backend as K
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

K.clear_session()
model_best = load_model('Model/best_model_3class.hdf5', compile=False)
food_list = ['donuts', 'pizza', 'samosa']

def predict_class(model, images, show=True):
    for img in images:
        img = image.load_img(img, target_size=(299, 299))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img /= 255.

        pred = model.predict(img)
        index = np.argmax(pred)
        food_list.sort()
        pred_value = food_list[index]
        if show:
            plt.imshow(img[0])
            plt.axis('off')
            plt.title(pred_value)
            plt.show()

def main():
    images = []
    samos = "Sample/Homemade Samosa.jpg"
    # images.append('donut.jpg')
    # images.append('pizza.jpg')
    images.append(samos)
    predict_class(model_best, images, True)

if __name__ == "__main__":
    main()


from openerp.osv import osv, fields, expression
import openerp.addons.decimal_precision as dp

class product_template(osv.osv):

    _name = 'product.template'
    _inherit = 'product.template'

    _columns = {
        'list_price': fields.float('Sale Price', digits_compute=dp.get_precision('Product Price'),track_visibility='onchange',
                                   help="Base price to compute the customer price. Sometimes called the catalog price."),
    }


#Zach Wu 2016
#Note: NK Testing does not account for stop codons or deletrions

import numpy as np
import sys
import random
import NK_Landscape
from scipy.stats import poisson

sample_parent = [random.randint(0,19) for i in range(10)]
num_AA = 20

def single_mutant_walk(parent, n):
    '''sequence should be in [19,3,0,1] format
        n is length of walk
    '''
    current = parent
    seq_list = [current]

    for i in range(n):
        #print(current)
        mut_index = random.randint(0,len(parent)-1)
        curr_AA = current[mut_index]
        poss_AA = set([j for j in range(num_AA)])
        poss_AA.remove(curr_AA)
        mut_AA = list(poss_AA)[random.randint(0,len(poss_AA)-1)]
        current[mut_index] = mut_AA
        seq_list.append(current)
    print(seq_list[-1])
    if n == 1: #if n == 1, probably using this for epm.
        return current
    else:
        return seq_list

def epm(parent, library_size, rate = 1):
    '''parent sequence should be in [19,3,0,1] format
        rate is the average number of mutations
        I'm just going to assume a poisson distribution for now
        check http://www.ncbi.nlm.nih.gov/pubmed/15939434

        -Fox assumes gamma distribution
    '''
    dist_countL, num_mutL = EPM_Poisson_countd(rate, library_size)

    print(str(dist_countL) + ' ' + str(num_mutL))

    seq_list = [] #does not include parent
    parent_temp = parent

    for i,num_mut in enumerate(num_mutL):
        for j in range(dist_countL[i]):
            seq_list.append(mutate_sequence(parent_temp, num_mut))
    return seq_list

def mutate_sequence(parent2, num_mutations):
#     mut_seq = parent
#     mut_index = random.randint(0,len(parent)-1)
#     curr_AA = mut_seq[mut_index]
#     poss_AA = set([j for j in range(20)])

#     list_mut = []
    mut_seq = parent2
    for i in range(num_mutations):
        mut_seq = single_mutant_walk(mut_seq,1)
    print(mut_seq)

    return mut_seq

def EPM_Poisson_countd(mu, library_size):
    #returns the Poisson mutation rate distribution for a given library size

    probs_list = []
    mut_list = []
    alpha = 1-1/(library_size*10)
    a,b = poisson.interval(alpha, mu, loc=0)
    a = int(a)
    b = int(b)
    for k in range(a,b+1):
        k_count = int(round(poisson.pmf(k,mu)*library_size,0))
        if k_count != 0:
            probs_list.append(k_count)
            mut_list.append(k)
    dif = sum(probs_list) - library_size
#     print(dif)
    mutation_list = [i for i in range(a,b+1)]
    index = mutation_list.index(mu)
    probs_list[index] -= dif

#     print(probs_list)
#     print(sum(probs_list))
    return probs_list, mut_list

def NK_Featurize(seqList):
    featurized_seq = []
    for i, seq in enumerate(seqList):
        temp_seq = [0] * len(seq) * num_AA
        for j, AA in enumerate(seq):
            temp_seq[j*20 + AA] = 1
        featurized_seq.append(temp_seq)
    return featurized_seq
###############################################################
###############################################################
###############################################################

import time
n = 10                #sequence length
library_size = 3*n

beg = time.clock()
test = NK_Landscape.NKLandscape(n,1, savespace = False, epi_dist = 'gamma')
interactions_list = test.nk_interactions()
epistatic_list = test.nk_epistatic()
end = time.clock()
print(-beg+end)

parent = [np.random.randint(num_AA) for i in range(n)]
print('Parent:' + str(parent))
#library1 = epm(parent, library_size, rate=3)
library2 = single_mutant_walk(parent,library_size)
print('end')

#library1_features = NK_Featurize(library1)
library2_features = NK_Featurize(library2)

library1_fitnessL = []
library2_fitnessL = []

# print(interactions_list)
# for i in range(len(library1_features)):
#     t1 = test.get_Energy(library1[i])
print(library2)


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

from xml.etree.ElementTree import ElementTree
import xml.etree
import xml.dom.minidom

import json

class salt_xml:
    tree = ElementTree()

    def __init__(self):
        json_data = open('insert.json')
        data = json.load(json_data)
        self.tree.parse('config-original.xml')
        self.read_json(data, "")
        self.tree.write('config.xml', method="xml", xml_declaration=False)

    def read_json(self, data, find):
        for key, value in data.iteritems():
            if isinstance(value, dict):
                if find is "None":
                    self.read_json(value, str(key))
                else:
                    self.read_json(value, str(find) + "/" + str(key) )
            else:
                self.tree.find(str(key)).text = str(value)

    def render(self):
        return xml.dom.minidom.parse('config.xml').toprettyxml()

def test():
    render = salt_xml()
    print(render.render())

test()


mydata=open('/Users/emilymclachlan/Desktop/mpol_december2017.txt')
newfile= open('mpol_strains-to-scan.txt','w')
inputcounter=0
outputcounter=0

for line in mydata:
	inputcounter += 1
	if line[-2]== '1':
	# we are going to the end of the line, because I'm looking at value at the end of the line
		newfile.write(line)
		outputcounter += 1

	else:
		print(line[-2])

print(inputcounter, "this is my inputcounter")
print(outputcounter, "this is the output counter")


import random
n =int(input("Nhập n="))
list1=[]
for i in range(n):
    a=random.random()
    list1.append(a)
print(list1)
min=list1[0]
for i in range(n):
    if min>list1[i]:
        min=list1[i]
print("giá trị nhỏ nhất là:",min)
print("Chương trình kết thúc")


__author__ = 'mnowotka'

from tastypie import fields
from chembl_webservices.core.utils import NUMBER_FILTERS, CHAR_FILTERS
from chembl_webservices.core.resource import ChemblModelResource
from chembl_webservices.core.meta import ChemblResourceMeta
from chembl_webservices.core.serialization import ChEMBLApiSerializer
try:
    from chembl_compatibility.models import Docs
except ImportError:
    from chembl_core_model.models import Docs

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

class DocsResource(ChemblModelResource):

    document_chembl_id = fields.CharField('chembl__chembl_id', null=True, blank=True)

    class Meta(ChemblResourceMeta):
        queryset = Docs.objects.all()
        excludes = ['doc_id']
        resource_name = 'document'
        collection_name = 'documents'
        detail_uri_name = 'chembl_id'
        serializer = ChEMBLApiSerializer(resource_name, {collection_name : resource_name})
        prefetch_related = ['chembl']

        fields = (
            'authors',
            'doc_type',
            'document_chembl_id',
            'doi',
            'first_page',
            'issue',
            'journal',
            'last_page',
            'pubmed_id',
            'title',
            'volume',
            'year',
        )

        filtering = {
            'authors' : CHAR_FILTERS,
            'doc_type' : CHAR_FILTERS,
            'document_chembl_id' : NUMBER_FILTERS,
            'doi': CHAR_FILTERS,
            'first_page': CHAR_FILTERS,
            'issue' : CHAR_FILTERS,
            'journal' : CHAR_FILTERS,
            'last_page' : CHAR_FILTERS,
            'pubmed_id' : NUMBER_FILTERS,
            'title' : CHAR_FILTERS,
            'volume' : CHAR_FILTERS,
            'year' : NUMBER_FILTERS,
        }
        ordering = [field for field in filtering.keys() if not ('comment' in field or 'description' in field) ]

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


import wikipediaapi

def print_sections(sections, level=0, endlevel=10):
	text = "";
	section_no = 0
	for s in sections:
		# print("%s: %s" % ("*" * (level + 1), s.title))
		section_no = section_no + 1
		if section_no <= endlevel:
			text = text + s.text + print_sections(s.sections, level + 1)
	return text

def findwiki(given_input):
	wiki = wikipediaapi.Wikipedia('en')

	page  = wiki.page(given_input)
	if page.exists()==False:
		print("Wikipedia page doesn't exists for ",given_input," keyword")
		exit(1)
	return page.summary + print_sections(page.sections), page.fullurl


#
# @lc app=leetcode.cn id=98 lang=python
#
# [98] 验证二叉搜索树
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        return self.recursive(root, float('-inf'), float('inf'))
        # return self.midTraversal(root)

    # Solution_1 —— 递归
    def recursive(self, root, low, up):
        if not root:
            return True
        if not low < root.val < up:
            return False
        return self.recursive(root.left, low, root.val) \
            and self.recursive(root.right, root.val, up)
        # if not self.recursive(root.left, low, root.val):
        #     return False
        # if not self.recursive(root.right, root.val, up):
        #     return False
        # return True

    # Solution_2 —— 中序遍历
    def midTraversal(self, root):
        stack, inorder = [], float('-inf')
        while stack or root:
            while root:
                stack.append(root)
                root = root.left
            root = stack.pop()
            if root.val <= inorder:
                return False
            inorder = root.val
            root = root.right
        return True
# @lc code=end

98.验证二叉搜索树.py


import numpy as np

class NativeBayes:
    def N(self, x, mu, sigma):
        p = 1 / sigma / np.sqrt(2 * np.pi) * np.exp(-(x - mu) ** 2 / 2 / sigma ** 2)
        return p

    def fit(self, X, y):
        X1 = X[y == 1]  # 找到所有正样本
        X2 = X[y == 0]  # 找到所有负样本

        # 分别计算负样本的均值和方差:
        self.mu_positive = np.mean(X1, axis=0)
        self.std_positive = np.std(X1, axis=0)
        # 分别计算正样本的均值和方差:
        self.mu_negative = np.mean(X2, axis=0)
        self.std_negative = np.std(X2, axis=0)
        # 计算p(y==1)
        self.py_positive = len(y[y == 1]) / len(y)
        self.py_negative = len(y[y == 0]) / len(y)

    def predict_prob(self, X):
        prb = []

        for a in X:
            # 假设有k个特征:
            p_xk_on_positive = []
            p_xk_on_negative = []
            p_xk = []
            for k, x in enumerate(a):
                # 样本a的第k个特征,值为x
                """
                P(x1, x2) = P(x1)*P(x2)
                P(x1) = P(x1|y=0)P(y=0) + P(x1|y=1)P(y=1) 全概率公式
                P(x2) = P(x2|y=0)P(y=0) + P(x2|y=1)P(y=1) 全概率公式
                """
                p_xk_on_positive.append(self.N(x, self.mu_positive[k], self.std_positive[k]))
                p_xk_on_negative.append(self.N(x, self.mu_negative[k], self.std_negative[k]))

                """
                P(y|x1,x2) = P(x1|y)*P(x2|y)*P(y) / P(x1,x2)
                """
                p_xk.append(p_xk_on_negative[k] * self.py_negative + p_xk_on_positive[k] * self.py_positive)
            p_positive_on_x = np.multiply.reduce(p_xk_on_positive) * self.py_positive / np.multiply.reduce(p_xk)
            p_negative_on_x = np.multiply.reduce(p_xk_on_negative) * self.py_negative / np.multiply.reduce(p_xk)
            prb.append([p_negative_on_x, p_positive_on_x])
        return np.array(prb)

    def predict(self, X):
        prob = self.predict_prob(X)
        return np.argmax(prob, axis=1)

# 计算预测精度:
def accurency(y_pred, y_real):
    return 1 - np.sum(abs(y_pred - y_real))/len(y_pred)

from sklearn.datasets import make_moons, make_circles, make_classification
import matplotlib.pyplot as plt

# 获取数据
X, y = make_circles(noise=0.2, factor=0.5, random_state=1)
#X, y = make_moons(noise=0.2, random_state=1)
#X, y = make_classification()
# print(X[:5])
# print(y[:5])

# 模型训练及预测:
clf = NativeBayes()
clf.fit(X, y)
y_pred = clf.predict(X)
print('模型精度为:%0.4f' % accurency(y_pred, y))

new_X = np.array([[0.5, 0.5]])
print(clf.predict(new_X))

import matplotlib as mpl
# 调整图片风格
mpl.style.use('fivethirtyeight')
# 定义xy网格，用于绘制等值线图
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
                     np.arange(y_min, y_max, 0.1))

# 预测可能性
Z = clf.predict_prob(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=.8)
# plt.show()
# 绘制散点图
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k')
plt.title("GaussianNaiveBayes")
plt.axis("equal")
plt.show()


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

from src.model.attention import Attention
from src.model.mixture_of_experts import MoE

class Decoder(nn.Module):
    '''
    Attention based decoder.
    Decodes encoded visual features of a video with previous word prediction in order to generate captions.
    Architecture is given by:
        ATTENTION - GRU - DROPOUT - MIXTURE-OF-EXPERTS-LAYER (zero-shot approach) - LOG-SOFTMAX
    '''
    def __init__(self, decoder_hid_dem, encoder_hid_dem, vocab, vocab_size, recipe1M_embeddings, embedding_dim,
                 decoder_input_size, linear_input_size, bidirectional_encoder, bidirectional_decoder, dropout,
                 moe=False, num_experts=1, device = torch.device('cuda')):
        '''
        Initialization of the decoder.

        Input:
        :param decoder_hid_dem: integer, hidden dimension of decoder
        :param encoder_hid_dem: integer, hidden dimension of encoder
        :param vocab: used vocabulary of the data
        :param vocab_size: integer, number of words in the vocabulary
        :param recipe1M_embeddings: boolean, True if recipe1M_embedding is considered (for zero-shot approach), else False
        :param embedding_dim: integer, dimension of vocabulary embedding
        :param decoder_input_size: integer, input dimension of decoder
        :param linear_input_size: integer, input dimension of fully connected layer after GRU
        :param bidirectional_encoder: boolean, True if encoder is bidirectional, else False
        :param bidirectional_decoder: boolean, True is decoder is bidirectional, else False
        :param dropout: double, probability of dropout
        :param moe: boolean, True if Mixture of Experts (MoE) layer is considered (for zero-shot approach), else False
        :param num_experts: integer, number of Experts in MoE layer, just for moe=True
        :param device: string, specifies availability of CUDA
        '''
        super(Decoder, self).__init__()
        self.encoder_hid_dem = encoder_hid_dem
        self.decoder_hid_dem = decoder_hid_dem
        self.attention = Attention(encoder_hid_dem, decoder_hid_dem, bidirectional_encoder)
        self.dropout = dropout
        self.output_dim = vocab_size
        self.embedding_dim = embedding_dim
        self.num_experts = num_experts
        self.device = device

        # Determine number of directions
        self.decoder_n_direction = 1
        if bidirectional_decoder == True:
            self.decoder_n_direction = 2

        self.bidirectional_encoder = bidirectional_encoder

        self.GRU_layer_out = nn.GRU(decoder_input_size, decoder_hid_dem * 2)
        self.out_layer = nn.Linear(in_features=linear_input_size, out_features=vocab_size)
        self.dropout = nn.Dropout(dropout)

        if recipe1M_embeddings:
            self.embedding_layer, self.embedding_dim = create_recipe1M_embedding_layer(
                torch.Tensor(vocab.weight_matrix), True)
        else:
            self.embedding_layer = nn.Embedding(vocab_size, self.embedding_dim)

        self.moe = moe
        if self.moe:
            self.MoE = MoE(num_experts, decoder_hid_dem, embedding_dim, None, self.decoder_n_direction,
                           device=self.device).to(self.device)

    def forward(self, hidden, actual_word, encoder_outputs, recipe_type):
        '''
        Performs the forward pass of the decoder.

        Inputs:
        :param hidden: torch.tensor, hidden decoder state of previous step
        :param actual_word: torch.tensor, word prediction of previous step
        :param encoder_outputs: torch.tensor, output vector of encoder
        :param recipe_type: torch.tensor, recipe types of samples
        :return: torch.tensor, torch.tensor: softmax probabilities for each word in vocab, hidden decoder state
        '''

        # hidden: [batch_size, dec_hid_dim]
        # actual_word: [batch_size, 1, embedding_dim]
        # encoder_outputs: [batch_size , seq_len, encoder_hid_dim * encoder_n_directional]

        a = self.attention(hidden, encoder_outputs)

        input_char = torch.cat((actual_word, a), 2)

        # [1, batch_size, decoder_input_size]
        input_char = input_char.permute(1, 0, 2)

        # [1 batch_size decoder_hid_dem]
        hidden = hidden.unsqueeze(0)

        # output: [1, batch_size , decoder_n_direction*decoder_hid_dim]
        # hidden: [n_layer*n_direction, batch_size, decoder_hid_dim]
        output, hidden = self.GRU_layer_out(input_char, hidden)

        # Apply Mixture of Experts layer - zero-shot approach
        if self.moe:
            hidden = hidden.to(self.device)
            recipe_type = recipe_type.to(self.device)
            output = self.MoE(hidden.squeeze(dim=0), recipe_type)
            output = torch.cat((output.squeeze(), a.squeeze(1), actual_word.squeeze(1)), dim=1)
            pre_out = self.out_layer(output)
        else:
            output = self.dropout(output)
            output = torch.cat((output.squeeze(0), a.squeeze(1), actual_word.squeeze(1)), dim=1)
            pre_out = self.out_layer(output)

        # prediced_output: [ batch_size , vocab_size ]
        predicted_output = F.log_softmax(pre_out, dim=1)

        return predicted_output, hidden.squeeze(0)

    def add_topic_embedding(self, topic_embedding, non_trainable=False):
        '''
        Add TFIDF-topic embedding

        Input:
        :param topic_embedding: torch.tensor, calculated weights for tfidf topic embedding
        :param non_trainable: boolean, False if embedding will not be optimized during training, else True
        '''
        # Determine dimensions for tfidf topic embedding
        num_embeddings, embedding_dim = topic_embedding.size()

        #Set up embedding with initial tfidf weights
        self.topic_embedding = nn.Embedding(num_embeddings, embedding_dim)
        self.topic_embedding.load_state_dict({'weight': topic_embedding})
        if non_trainable:
            self.topic_embedding.weight.requires_grad = False

        # Inform MoE Layer about tfidf embedding
        if self.moe:
            self.MoE = MoE(
                self.num_experts, self.decoder_hid_dem, embedding_dim, self.topic_embedding, self.decoder_n_direction,
                device=self.device).to(self.device)

def create_recipe1M_embedding_layer(weights_matrix, non_trainable=False):
    '''
    Creates Recipe1M vocabulary embedding (for zero-shot approach)

    Input:
    :param weights_matrix: torch.tensor, initial weights for vocabulary embedding
    :param non_trainable: boolean, False if embedding will not be optimized during training, else True

    Output:
    :return: torch.tensor.nn.embedding, integer, emebdding with its dimension
    '''
    # Determine dimensions for vocabulary embedding
    num_embeddings, embedding_dim = weights_matrix.size()

    # Set up emebdding with initial vocabulary weights
    emb_layer = nn.Embedding(num_embeddings, embedding_dim)
    emb_layer.load_state_dict({'weight': weights_matrix})
    if non_trainable:
        emb_layer.weight.requires_grad = False

    return emb_layer, embedding_dim