import time
from typing import Optional

__author__ = "Bojan Potočnik"

t0: float
"""Time reference point for this module."""

def reset() -> None:
    """
    The reference point of the value returned by `time.perf_counter()` is undefined,
    so that only the difference between the results of consecutive calls is valid.
    When this module is imported, the initial reference time point is marked and can
    be reset using this function.
    """
    global t0

    t0 = time.perf_counter()

def time_string(start_time: float = None, end_time: Optional[float] = None) -> str:
    """
    Get string with elapsed time information in form of:
        "x.xxx[xxx] Y"
    where `Y` is time unit decided based on amount of time passed and `x.xxx` is the calculated time
    which have 6 decimal places in case of nanoseconds.

    :param start_time: Starting point of the time which will be subtracted. If not provided, the initial starting
                       reference time saved when this module was imported will be used instead.
    :param end_time:   End point of the time from which `start_time` will be subtracted. If not provided,
                       the current time will be used.

    :return: Formatted time.
    """
    if end_time is None:
        end_time = time.perf_counter()
    if start_time is None:
        start_time = t0
    delta = end_time - start_time

    # Decide which unit to use. Usually operations timed with this function use fraction
    # of a second, that is why check from the lowest unit to the higher ones.
    if delta < 1e-6:
        # (1 µs, -∞ ns)
        unit = "ns"
        delta *= 1e9
        decimals = 6
    elif delta < 1e-3:
        # (1 ms, 1 µs]
        unit = "µs"
        delta *= 1e6
        decimals = 3
    elif delta < 1:
        # (1 s, 1 ms]
        unit = "ms"
        delta *= 1e3
        decimals = 3
    elif delta < 60:
        # (60 s, 1 s]
        unit = "s"
        # `time.perf_counter()` base unit is seconds.
        decimals = 3
    elif delta < 3600:
        # (1 h, 60 s]
        unit = "m"
        delta /= 60
        decimals = 3
    else:
        # (∞, 1 h]
        unit = "h"
        delta /= 3600
        decimals = 3

    return f"{delta:.{decimals}f} {unit}"

reset()


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

import numpy as np
import struct

class write_feats():
    def __init__(self, file_name):
        try:
            idx_f = open(file_name + '.idx', 'w')
            self.idx_file = idx_f
            ark_f = open(file_name + '.ark', 'wb')
            self.ark_file = ark_f
            self.ark_file_name = file_name + '.ark'
        except:
            return

    def write(self, key, feat, label):
        pos = self.ark_file.tell()
        self.idx_file.write('%s %s:%d\n' % (key, self.ark_file_name, pos))

        feat = np.array(feat, dtype='float64')
        self.ark_file.write(struct.pack('I', feat.shape[0]))  # rows
        self.ark_file.write(struct.pack('I', feat.shape[1]))  # cols
        feat.tofile(self.ark_file, sep="")  # binary data
        self.ark_file.write(struct.pack('I', label))  # label

    def __del__(self):
        if self.idx_file:
            self.idx_file.close()
        if self.ark_file:
            self.ark_file.close()

def read_feats(feats_descriptor):
    """
    generator(key, feat, label) = read_feats(feats_descriptor)
    file_or_fd: opened file descriptor

    Example:
    f = open('feats_1.idx', 'r')
    for line in f:
        key, feat, label = read_feats(line)
        ....
    """
    (file_name, pos) = feats_descriptor.split(':')

    with open(file_name, 'rb') as fd:
        fd.seek(int(pos))
        rows = struct.unpack('I', fd.read(4))[0]
        cols = struct.unpack('I', fd.read(4))[0]
        buf = fd.read(rows * cols * 8)
        vec = np.frombuffer(buf, dtype='float64')
        mat = np.reshape(vec,(rows,cols))
        label = struct.unpack('I', fd.read(4))[0]

    return mat, label


# -*- coding: utf-8 -*-
"""
Created on Mon Sep 17 06:28:21 2018

@author: LALIT ARORA
"""

import sqlite3
import openpyxl

def create_connection(db_file):
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except:
        print("Database connection Error!")
        return None

def u_create_table(wbk,db_file):
    wb=openpyxl.load_workbook(wbk)
    sheets=wb.sheetnames
    for i in range(len(sheets)):
        columns=[]
        ac_sheet=wb[sheets[i]]
        cols = ac_sheet.max_column
        for j in range(1,cols+1):
            temp=(ac_sheet.cell(row=1, column=j).value)
            if temp is not None:
                columns.append("%r"%str(temp))
        print(columns)
        create_table(columns,sheets[i],db_file)
        transfer_data(columns,sheets[i],db_file,wbk)

def create_table(cols,sheetname,dbname):
    if len(cols)==0:
        return
    data="CREATE TABLE IF NOT EXISTS "+str(sheetname)+" ( "
    for i in range(len(cols)):
        data=data+str(cols[i])+" text, "
    data=data[:len(data)-2]
    data=data+" );"
    conn=create_connection(dbname)
    c = conn.cursor()
    c.execute(data)
    conn.commit()
    conn.close()

def transfer_data(cols,sheetname,dbname,wbk):
    if len(cols)==0:
        return
    wb=openpyxl.load_workbook(wbk)
    ac_sheet=wb[sheetname]
    rows=ac_sheet.max_row
    var=",".join(cols)
    var="INSERT INTO "+str(sheetname)+" ("+var+") VALUES ("

    conn=create_connection(dbname)
    c=conn.cursor()
    for i in range(2,rows+1):
        vals=[]
        for j in range(1,len(cols)+1):
            vals.append('"'+str(ac_sheet.cell(row=i, column=j).value)+'"')
        e_vals=",".join(vals)
        e_vals=e_vals+")"
        c.execute(var+e_vals)
        conn.commit()
    conn.close()

if __name__ == '__main__':
    import sys
    arguments=sys.argv
    wbk=arguments[1]
    db_file = arguments[2]
    print("Working..")
    u_create_table(wbk,db_file)
    print ("Complete..")


from .similarity import fetch_MEN, fetch_WS353, fetch_SimLex999, fetch_SimVerb3500, fetch_MTurk, fetch_RG65, fetch_SCWS
from .embedding import Embedding
import numpy as np
import scipy

def count_missing_words(w, X):
    missing_words = 0
    words = w.vocabulary.word_id
    for query in X:
        for query_word in query:
            if query_word not in words:
                missing_words += 1
    return missing_words

def cosine_similarity(v1, v2, model=None):
    """
    compute cosine similarity: regular between 2 vectors or max of pairwise or
    average of pairwise

    v1: ndarray of dimension 1 (dim,) or 2 (n_embeddings, dim)
    v2: ndarray of dimension 1 (dim,) or 2 (n_embeddings, dim)
    model: None if v1 and v2 have dimension 1
           else, 'AvgSim' or 'MaxSim'
    """
    if len(v1.shape) == 1:
        v1 = v1.reshape((1,-1))
    if len(v2.shape) == 1:
        v2 = v2.reshape((1,-1))
    prod_norm = np.outer(np.linalg.norm(v1, axis=1),np.linalg.norm(v2, axis=1))
    pairwise_cosine = np.dot(v1, v2.T)/prod_norm
    if not model:
        return pairwise_cosine[0][0]
    elif model == 'AvgSim':
        return np.mean(pairwise_cosine)
    elif model == 'MaxSim':
        return np.max(pairwise_cosine)
    else:
        return ValueError('Unknown model {}'.format(model))

def evaluate_similarity(w, X, y, missing_words = 'filter_out'):
    if isinstance(w, dict):
        w = Embedding.from_dict(w)

    n_missing_words = count_missing_words(w, X)
    # if n_missing_words > 0:
    #     print("Missing {} words.".format(n_missing_words))

    mean_vector = np.mean(w.vectors, axis=0, keepdims=True)
    A, B = [], []
    if missing_words == 'mean' or n_missing_words == 0:
        A = [w.get(word, mean_vector) for word in X[:, 0]]
        B = [w.get(word, mean_vector) for word in X[:, 1]]
    elif missing_words == 'filter_out':
        # logger.info("Will ignore them")
        y_filtered = []
        for x, gt in zip(X, y):
            a, b = x
            if a not in w or b not in w:
                continue
            A.append(w.get(a, mean_vector))
            B.append(w.get(b, mean_vector))
            y_filtered.append(gt)
        y = np.asarray(y_filtered)

    #A = np.asarray([w.get(word, mean_vector) for word in X[:, 0]])
    #B = np.asarray([w.get(word, mean_vector) for word in X[:, 1]])
    scores = np.array([cosine_similarity(v1, v2) for v1, v2 in zip(A, B)])
    #scores = np.array([v1.dot(v2.T)/(np.linalg.norm(v1)*np.linalg.norm(v2)) for v1, v2 in zip(A, B)])
    return scipy.stats.spearmanr(scores, y).correlation

def evaluate_on_all(w):
    if isinstance(w, dict):
        w = Embedding.from_dict(w)

    # Calculate results on similarity
    # print("Calculating similarity benchmarks")
    similarity_tasks = [
        ["SV-d", fetch_SimVerb3500(which='dev')],
        ["MEN-d", fetch_MEN(which='dev')],
        ["SL999", fetch_SimLex999()],
        ["SL333", fetch_SimLex999(which='333')],
        ["SV-t", fetch_SimVerb3500(which='test')],
        ["RG", fetch_RG65()],
        ["SCWS", fetch_SCWS()],
        ["MEN-t", fetch_MEN(which='test')],
        ["MT", fetch_MTurk()],
        ["WS353", fetch_WS353()],
    ]
    similarity_results = {}
    print_result = ''
    for name, data in similarity_tasks:
        similarity_results[name] = evaluate_similarity(w, data.X, data.y)
        print_result += "%s:%0.4f "%(name, similarity_results[name])
    return print_result

def get_validation(w):
    if isinstance(w, dict):
        w = Embedding.from_dict(w)
    data = fetch_SimVerb3500(which='dev')
    SV_score = evaluate_similarity(w, data.X, data.y)
    data =  fetch_MEN(which='dev')
    MEN_score = evaluate_similarity(w, data.X, data.y)
    data = fetch_SimLex999()
    # SL999 = evaluate_similarity(w, data.X, data.y)
    # same as CPAE paper, give more weight in similarity task
    return SV_score
    # return SL999


from django.shortcuts import render,redirect
from random import shuffle
VALUES = ['flipper','pyragravure','overage','sanguicolous','convulsed','ectopic','archegonium','wrinkleless','paraments','rgenise']
def index(request):
    return render(request, 'first_app/index.html')
def form_process(request):
    request.session['pick'] = request.POST['pick']
    global VALUES
    shuffle(VALUES)
    return redirect('/surprises')
def surprises(request):
    num = int(request.session['pick'])
    context = {
      'surprises': VALUES[0: num]
    }
    return render(request, 'first_app/surprises.html', context)


import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

INF = float('inf')
MOD = 10**9+7
#MOD = 998244353

def main():
    x = input()[:-1]
    ch_flag = False
    for i, c in enumerate(x):
        if ch_flag:
            ch_flag = False
            continue
        if c in {'o', 'k', 'u'}:
            continue
        if c == 'c':
            if i < len(x)-1 and x[i+1] == 'h':
                ch_flag = True
                continue

        break

    else:
        print("YES")
        exit()

    print('NO')

if __name__ == '__main__':
    main()


from pathlib import Path
import numpy as np
import pandas as pd
from collections import defaultdict

if __name__=="__main__":
    N2V_ROOT = Path('scripts/node2vec/emb')
    Ks = [4,6,9]

    for k in Ks:
        DATA_ROOT = N2V_ROOT / f'{k}'
        files = DATA_ROOT.glob("**/*.emb")
        out = defaultdict(list)
        dim = None
        for f in files:
            identifier = f.stem
            label = f"{f.parent.parts[4]}/{f.parent.parts[5]}"
            arr = np.loadtxt(f,delimiter=',')
            out[label].append([int(identifier)] + list(arr))
            if not dim:
                dim = len(arr)
        cols = ['idx'] + [f"x_{i}" for i in range(dim)]

        for label,df in out.items():
            df = pd.DataFrame(df, columns=cols)
            df = df.sort_values(["idx"])
            print(f"k {k} label :{label} len {len(df)}")
            output_path = DATA_ROOT / Path(label+".csv")

            Path(output_path).parent.mkdir(exist_ok=True,parents=True)
            df.to_csv(output_path, index=None)


import FWCore.ParameterSet.Config as cms

###
# Defaults
###
from FWCore.ParameterSet.VarParsing import VarParsing
options = VarParsing ('analysis')

#options.inputFiles = "file:GEM2019Upg14_CFEBBufferOverload_0p01.root"
options.inputFiles = "file:/hdfs/store/user/dntaylor/BufferOverload_HZZ_0p10_CFEB_v8-CFEBBufferOverload_cfg/CFEBBufferOverload_cfg-GEM2019Upg14_9.root"
options.outputFile = "CFEBBufferOverload_StandAloneMuon.root"
options.parseArguments()

###
# Run Skim
###
process = cms.Process("StandAloneMuonSkim")

process.load('Configuration.StandardSequences.Services_cff')
process.load('FWCore.MessageService.MessageLogger_cfi')
#process.load('Configuration.Geometry.GeometryExtended2019Reco_cff')
#process.load('Configuration.StandardSequences.MagneticField_38T_PostLS1_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')

from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, 'auto:upgrade2019', '')

process.options = cms.untracked.PSet(
    wantSummary = cms.untracked.bool(True),
#    SkipEvent = cms.untracked.vstring('ProductNotFound')
)

process.MessageLogger.cerr.FwkReport.reportEvery = 1000

process.source = cms.Source("PoolSource",
    fileNames = cms.untracked.vstring(
        options.inputFiles
    )
)

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )

process.TFileService = cms.Service("TFileService",
    fileName = cms.string(options.outputFile),
)

process.skim = cms.EDAnalyzer("NtupleProducer",
    muons = cms.InputTag("standAloneMuons","","ReRecoBuffer"),
    genParticles = cms.InputTag("genParticles"),
)

process.p = cms.Path(
    process.skim
)


from sqlalchemy import create_engine
from sqlalchemy import select

import argparse
import pandas as pd

__author__ = 'jaquinoa'

db_engine = create_engine('mysql+pymysql://user:password@localhost/pydb')

parser = argparse.ArgumentParser(description='Copiar Nexus en Excel a DB')
parser.add_argument('-e', '--excel', help='Archivo Excel', required=True)
parser.add_argument('-s', '--sheet', help='Hoja Excel data Nexus', required=False)
args = parser.parse_args()

print("-----------------------------------------------------")
print("")
print("*******  Copiar Excel Nexus a una Tabla MySQL *******")

print("Leyendo archivo Excel...")
xlsfile = args.excel
sheet   = args.sheet
df =  pd.read_excel(xlsfile, skiprows = 4, dtype = str)

# Set columns names
df.columns = ['region', 'distrito', 'ugel', 'provincia', 'distrito', 'tipo_ie', 'gestion', 'zona', 'codmod_ie', 'clave8', 'niveleducativo', 'nombre_ie', 'codigo_plaza', 'tipo_trabajador', 'subtipo_trabajador', 'cargo', 'situacion_laboral', 'motivo_vacante', 'paterno', 'materno', 'nombres', 'categoria_remunerativa', 'jornada_laboral', 'estado', 'codigo_modular', 'fecha_nacimiento', 'dni', 'fecha_inicio', 'fecha_termino', 'tipo_registro', 'ley', 'preventiva', 'referencia_preventiva']

print(df.info())

print("Copiar a Base de Datos...")
df.to_sql(con=db_engine, name='_nexus2018', if_exists='replace', index_label = 'id')

print('Done...')


import os
import re
from io_manager import convert_to_signal
from spectral_analysis import get_spectrogram, get_signature,\
     get_constellation_map, combinatorial_hashing

class Song:
    def __init__(self, title, artist, filePath, libPath, plot, signal=None, sample_rate=None):
        self.title = "unknown" if title is None else title
        self.artist = "various" if artist is None else artist
        self.filename = os.path.basename(filePath)
        self.plot = plot
        if signal is not None and sample_rate is not None:
            (self.sample_rate, self.signal) = (sample_rate, signal)
        else:
            (self.sample_rate, self.signal) = convert_to_signal(filePath, libPath)
        self.spectrogram = get_spectrogram(self.sample_rate, self.signal, self.sample_rate,
                                           int(self.sample_rate/2), self.filename, self.plot)
        self.constellation_map = get_constellation_map(self.spectrogram, self.filename, self.plot)
        self.hash_values = combinatorial_hashing(self.constellation_map)
        self.path = filePath
        # self.signature = get_signature(self.spectrogram, 10)