#!/usr/bin/env python3

"""
.out to .csv script.
"""

import argparse
import csv
import glob
import os
import shutil

import numpy as np
import pandas as pd

def out_2_csv(path_outputs, path_oracles):
    """ Convert .out files to .csv files.
    """
    # Get instances names
    instances = [instance for instance in os.listdir(path_outputs)]

    for instance in instances:
        # Check if there is a `reduction.out` file
        if os.path.isfile('{}/{}/reduction.out'.format(path_outputs, instance)):
            reduction_converter(path_outputs, instance)

        # Check if there is a `{RC,RF,RD}_with_reduction.out` file
        if os.path.isfile('{}/{}/RC_with_reduction.out'.format(path_outputs, instance)) or os.path.isfile('{}/{}/RF_with_reduction.out'.format(path_outputs, instance)) or os.path.isfile('{}/{}/RD_with_reduction.out'.format(path_outputs, instance)):
            for properties in ['RC', 'RD', 'RF']:
                properties_converter(path_outputs, instance, properties, path_oracles)

def reduction_converter(path_outputs, instance):
    """ Convert  `reduction.out` to `reduction.csv`.
    """
    # Open `reduction.out`
    with open('{}/{}/reduction.out'.format(path_outputs, instance)) as out_reduction:
        reduction_data = out_reduction.readlines()[0].rstrip().split(' ')

    # Get reduction data
    if len(reduction_data) >= 3:
        reduction_ratio = int(reduction_data[1].replace('RR~', '').replace('%', ''))
        reduction_time = reduction_data[2].replace('t~', '').replace('s', '')
    else:
        reduction_ratio = np.nan
        reduction_time = np.nan

    # Write reduction data in `reduction.csv`
    with open('{}/{}/reduction.csv'.format(path_outputs, instance), 'w') as csv_reduction:
        reduction_writer = csv.writer(csv_reduction)
        reduction_writer.writerow(['INSTANCE', 'TIME', 'RATIO'])

        reduction_data = [instance, reduction_time, reduction_ratio]
        reduction_writer.writerow(reduction_data)
        print(' '.join(map(str, reduction_data)))

def properties_converter(path_outputs, instance, properties, path_oracles):
    """ Convert `{RC,RF,RD}_with_reduction.csv` and `{RC,RF,RD}_without_reduction.out` to `{RC,RF,RD}.csv`.
    """
    # Check is the file exists with and without reduction
    if not (os.path.isfile('{}/{}/{}_with_reduction.out'.format(path_outputs, instance, properties)) and os.path.isfile('{}/{}/{}_without_reduction.out'.format(path_outputs, instance, properties))):
        return

    # Read oracles, if they do not exist do nothing
    try:
        with open('{}/{}-{}.out'.format(path_oracles, instance, properties)) as fp_oracles:
            oracles = fp_oracles.readlines()
        fp_oracles.close()
    except FileNotFoundError as e:
        return

    # Open `{properties}_with_reduction.out`
    with open('{}/{}/{}_with_reduction.out'.format(path_outputs, instance, properties)) as out_with_reduction:
        properties_with_reduction = out_with_reduction.readlines()

    # Open `{properties}_with_reduction.out`
    with open('{}/{}/{}_without_reduction.out'.format(path_outputs, instance, properties)) as out_without_reduction:
        properties_without_reduction = out_without_reduction.readlines()

    # Write properties information to `{RC,RF,RD}.csv`
    with open("{}/{}/{}.csv".format(path_outputs, instance, properties), 'w') as result_file:

        result_writer = csv.writer(result_file)
        result_writer.writerow(['INSTANCE', 'PROPERTY', 'MONOTONIC', 'TIME_WITH_REDUCTION', 'METHOD_WITH_REDUCTION', 'CORRECTNESS_WITH_REDUCTION', 'TIME_WITHOUT_REDUCTION', 'METHOD_WITHOUT_REDUCTION', 'CORRECTNESS_WITHOUT_REDUCTION'])

        for prop_with_reduction, prop_without_reduction, prop_oracle in zip(properties_with_reduction[1:], properties_without_reduction[1:], oracles[1:]):
            prop_with_reduction, prop_without_reduction, prop_oracle = prop_with_reduction.strip().split(' '), prop_without_reduction.strip().split(' '), prop_oracle.strip().split(' ')

            # Get property data
            property_data = [instance, prop_oracle[1], prop_with_reduction[1] != 'SKIPPED' and prop_with_reduction[-1] != '(IC3_auto-disabled)']

            # Get analysis with reduction dat
            if prop_with_reduction[1] == 'SKIPPED':
                analysis_with_reduction_data = [np.nan, 'None', 'Skipped']
            elif prop_with_reduction[1] == 'TIMEOUT':
                analysis_with_reduction_data = [np.nan, 'None', 'None']
            else:
                analysis_with_reduction_data = [prop_with_reduction[2], prop_with_reduction[3], prop_with_reduction[1] == prop_oracle[2]]

            # Get analysis without reduction data
            if prop_without_reduction[1] == 'SKIPPED':
                analysis_without_reduction_data = [np.nan, 'None', 'Skipped']
            elif prop_without_reduction[1] == 'TIMEOUT':
                analysis_without_reduction_data = [np.nan, 'None', 'None']
            else:
                analysis_without_reduction_data = [prop_without_reduction[2], prop_without_reduction[3], prop_without_reduction[1] == prop_oracle[2]]

            # Get full data
            property_data += analysis_with_reduction_data + analysis_without_reduction_data

            # Write and print property data
            result_writer.writerow(property_data)
            print(' '.join(map(str, property_data)))

def merge_files(path_outputs):
    """ Merge .csv file in common files.
    """
    merged_path = '{}/merged/'.format(path_outputs)

    # Delete `merged` directory
    if os.path.exists(merged_path):
        shutil.rmtree(merged_path)

    # Create `merged` folder
    os.makedirs(merged_path)

    # Merge files
    for csv_filename in ['reduction.csv', 'RC.csv', 'RF.csv', 'RD.csv']:
        print(csv_filename)
        csv_filenames = [filename for filename in glob.iglob('{}/**/*{}'.format(path_outputs, csv_filename), recursive=True)]
        if csv_filenames:
            merged_csv = pd.concat([pd.read_csv(filename) for filename in csv_filenames])
            merged_csv.to_csv(merged_path + csv_filename, index=False, encoding='utf-8')

def main():
    """ Main function.
    """
    # Arguments parser
    parser = argparse.ArgumentParser(description='SMPT Benchmark Script')

    parser.add_argument('path_outputs',
                        metavar='outputs',
                        type=str,
                        help='path to outputs directory')

    parser.add_argument('path_oracles',
                        metavar='oracles',
                        type=str,
                        help='path to oracles directory')

    parser.add_argument('--merge-files',
                        action='store_true',
                        help='path to oracles directory')

    results = parser.parse_args()

    out_2_csv(results.path_outputs, results.path_oracles)

    # Merge files
    if results.merge_files:
        merge_files(results.path_outputs)

if __name__ == "__main__":
    main()
    print("DONE")
    exit(0)


import librosa
import soundfile as sf

# this class is used to modify the sound files as the user specifies
class SoundModifier:
    __instance = None
    pitches = {'hawk1_pitch': 0, 'hawk2_pitch': 0, 'hawk3_pitch': 0, 'hawk4_pitch': 0}
    speeds = {'hawk1_speed': 100, 'hawk2_speed': 100, 'hawk3_speed': 100, 'hawk4_speed': 100}

    # this constructor ensures that it doesn't create another insance if not needed
    def __init__(self):
        if SoundModifier.__instance is not None:
            raise Exception("Error")
        else:
            SoundModifier.__instance = self

    """this function runs whenever the pitch slider moves.
    it stores the pitch for modifying"""

    def changePitch(self, val, name):
        attr = name + '_pitch'
        self.pitches[attr] = val

    """this function runs whenever the speed slider moves.
    it stores the speed for modifying"""

    def changeSpeed(self, val, name):
        attr = name + '_speed'
        self.speeds[attr] = val

    """this function applies the changes to speed and pitch
    and creates a new sound file"""

    def applySoundChanges(self, name):
        url = '../res/sounds/' + name + '.wav'
        self.sound, self.sampling_rate = librosa.load(url)
        attr = name + '_speed'
        speed = self.speeds[attr] / 100
        self.sound = librosa.effects.time_stretch(self.sound, speed)
        attr = name + '_pitch'
        pitch = self.pitches[attr]
        self.sound = librosa.effects.pitch_shift(self.sound, self.sampling_rate, n_steps=pitch)
        new_url = '../res/sounds/' + name + '_mod.wav'
        sf.write(new_url, self.sound, self.sampling_rate)

    # this function creates a new instance if needed, and returns an instance of SoundModifier
    @staticmethod
    def getInstance():
        if SoundModifier.__instance is None:
            SoundModifier()
        return SoundModifier.__instance


import os
import tensorflow as tf
import numpy as np
import argparse
import time
import sys
import pickle

from madnis.models.mcw import mcw_model, residual_mcw_model
from madnis.utils.train_utils import integrate
from madnis.plotting.plots import plot_alphas, plot_variances

from madnis.distributions.camel import Camel, CuttedCamel
from madnis.distributions.uniform import StandardUniform
from madnis.mappings.cauchy import CauchyDistribution
from madnis.models.mc_integrator import MultiChannelIntegrator
from madnis.models.mc_prior import WeightPrior

# Use double precision
tf.keras.backend.set_floatx("float64")
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

pickle_data = {}

#########
# Setup #
#########

parser = argparse.ArgumentParser()

# Data params
parser.add_argument("--train_batches", type=int, default=100)
parser.add_argument("--int_samples", type=int, default=10000)
parser.add_argument("--truncate", action="store_true")

# Model params
parser.add_argument("--use_prior_weights", action='store_true')
parser.add_argument("--prior", type=str, default="opt", choices={"flat", "opt"})
parser.add_argument("--units", type=int, default=16)
parser.add_argument("--layers", type=int, default=3)
parser.add_argument("--activation", type=str, default="leakyrelu",
                    choices={"relu", "elu", "leakyrelu", "tanh"})
parser.add_argument("--initializer", type=str, default="glorot_uniform",
                    choices={"glorot_uniform", "he_uniform"})

# Train params
parser.add_argument("--epochs", type=int, default=20)
parser.add_argument("--sample_capacity", type=int, default=2000)
parser.add_argument("--batch_size", type=int, default=128)
parser.add_argument("--lr", type=float, default=1e-3)
parser.add_argument("--loss", type=str, default="variance",
                    choices={"variance", "neyman_chi2", "kl_divergence"})
parser.add_argument("--uniform_channel_ratio", type=float, default=1.0)
parser.add_argument("--variance_history_length", type=int, default=100)

args = parser.parse_args()

################################
# Define distributions
################################

# Define peak positions and heights
MEAN1 = 2.0
STD1 = 0.5
MEAN2 = 5.0
STD2 = 0.1
CUT = 2.5

# Define truth distribution
if args.truncate:
    camel = CuttedCamel([MEAN1, MEAN2], [STD1, STD2], cut=CUT)
else:
    camel = Camel([MEAN1, MEAN2], [STD1, STD2], peak_ratios=[0.35, 0.65])

# Define the channel mappings
GAMMA1 = np.sqrt(2.) * STD1
GAMMA2 = np.sqrt(2.) * STD2

map_1 = CauchyDistribution(mean=MEAN1, gamma=GAMMA1)
map_2 = CauchyDistribution(mean=MEAN2, gamma=GAMMA2)

################################
# Naive integration
################################

INT_SAMPLES = args.int_samples

# Uniform sampling in range [0,6]
volume = 6
noise = tf.random.uniform((INT_SAMPLES, 1), dtype=tf.keras.backend.floatx()) * volume
phi = 1 / volume
integrand = camel.prob(noise) / phi  # divide by volume in this case

res, err = integrate(integrand)
relerr = err / res * 100

print(f"\n Naive integration ({INT_SAMPLES:.1e} samples):")
print("-------------------------------------------------------------")
print(f" Result: {res:.8f} +- {err:.8f} ( Rel error: {relerr:.4f} %)")
print("-----------------------------------------------------------\n")

pickle_data.update({
    "naive_integral": float(res),
    "naive_integral_err": float(err),
    "naive_integral_relerr": float(relerr),
})

################################
# Define the network
################################

DIMS_IN = 1  # dimensionality of data space (Camel = 1D)
N_CHANNELS = 2  # number of Mappings
PRIOR = args.use_prior_weights

META = {
    "units": args.units,
    "layers": args.layers,
    "initializer": args.initializer,
    "activation": args.activation,
}

if PRIOR:
    mcw_net = residual_mcw_model(
        dims_in=DIMS_IN, n_channels=N_CHANNELS, meta=META
    )
    PREFIX = "residual"
else:
    mcw_net = mcw_model(dims_in=DIMS_IN, n_channels=N_CHANNELS, meta=META)
    PREFIX = "scratch"
if args.truncate:
    PREFIX = "trunc_" + PREFIX

################################
# Define the prior
################################

if PRIOR and args.prior != "flat":
    # Define prior weight
    prior = WeightPrior([map_1.prob, map_2.prob], N_CHANNELS)
    madgraph_prior = prior.get_prior_weights
else:
    madgraph_prior = None

################################
# Define the integrator
################################

# Define training params
EPOCHS = args.epochs
BATCH_SIZE = args.batch_size
LR = args.lr
LOSS = args.loss
SAMPLE_CAPACITY = args.sample_capacity
UNIFORM_CHANNEL_RATIO = args.uniform_channel_ratio
VARIANCE_HISTORY_LENGTH = args.variance_history_length

# Number of samples
#TRAIN_SAMPLES = args.train_samples
ITERS = args.train_batches

# Decay of learning rate
DECAY_RATE = 0.01
DECAY_STEP = ITERS

# Prepare scheduler and optimzer
lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(LR, DECAY_STEP, DECAY_RATE)
opt = tf.keras.optimizers.Adam(lr_schedule)

base_dist = StandardUniform((DIMS_IN,))

integrator = MultiChannelIntegrator(
    camel,
    base_dist,
    opt,
    weight_prior=madgraph_prior,
    mappings=[map_1, map_2],
    mcw_model=mcw_net,
    use_weight_init=PRIOR,
    n_channels=N_CHANNELS,
    loss_func=LOSS,
    sample_capacity=SAMPLE_CAPACITY,
    uniform_channel_ratio=UNIFORM_CHANNEL_RATIO,
    variance_history_length=VARIANCE_HISTORY_LENGTH
)

################################
# Pre train - plot alphas
################################

def eval_alpha(eval_prefix):
    p = tf.cast(tf.linspace([0], [6], 1000, axis=0), tf.keras.backend.floatx())
    if PRIOR:
        if madgraph_prior is not None:
            res = madgraph_prior(p)
        else:
            res = 0.5 * tf.ones((p.shape[0], 2), dtype=tf.keras.backend.floatx())
        alphas = mcw_net([p, res])
    else:
        alphas = mcw_net(p)
    truth = camel.prob(p)
    m1 = map_1.prob(p)
    m2 = map_2.prob(p)
    plot_alphas(p, alphas, truth, [m1, m2], prefix=f"{eval_prefix}_{PREFIX}")

    pickle_data.update({
        f"{eval_prefix}_p": p.numpy(),
        f"{eval_prefix}_alphas": alphas.numpy(),
        f"{eval_prefix}_truth": truth.numpy(),
        f"{eval_prefix}_m1": m1.numpy(),
        f"{eval_prefix}_m2": m2.numpy()
    })
eval_alpha("pre")

################################
# Pre train - integration
################################

# Uniform sampling in range [0,1]
# integrand = integrator._get_integrand(INT_SAMPLES, weight_prior=madgraph_prior)
# print(integrand)
res, err = integrator.integrate(INT_SAMPLES)
relerr = err / res * 100

print(f"\n Pre Multi-Channel integration ({INT_SAMPLES:.1e} samples):")
print("--------------------------------------------------------------")
print(f" Result: {res:.8f} +- {err:.8f} ( Rel error: {relerr:.4f} %) ")
print("------------------------------------------------------------\n")

pickle_data.update({
    "pre_integral": float(res),
    "pre_integral_err": float(err),
    "pre_integral_relerr": float(relerr),
})

################################
# Train the network
################################

train_losses = []
start_time = time.time()
train_variance = []
for e in range(EPOCHS):

    train_variance.append([])
    for _ in range(25):
        _, err = integrator.integrate(INT_SAMPLES)
        var_int = err**2 * (INT_SAMPLES - 1.)
        train_variance[-1].append(var_int.numpy())

    batch_train_losses = []
    # do multiple iterations.
    for _ in range(ITERS):
        batch_loss = integrator.train_one_step(BATCH_SIZE)
        batch_train_losses.append(batch_loss)

    train_loss = tf.reduce_mean(batch_train_losses)
    train_losses.append(train_loss)

    if (e + 1) % 1 == 0:
        # Print metrics
        print(
            "Epoch #{}: Loss: {}, Learning_Rate: {}".format(
                e + 1, train_losses[-1], opt._decayed_lr(tf.float32)
            )
        )
train_time = time.time() - start_time
print("--- Run time: %s hour ---" % (train_time / 60 / 60))
print("--- Run time: %s mins ---" % (train_time / 60))
print("--- Run time: %s secs ---" % (train_time))

pickle_data["train_losses"] = np.array(train_losses)
pickle_data["train_time"] = train_time

################################
# After train - plot alphas
################################

eval_alpha("post")

################################
# After train - plot variances
################################

plot_variances(train_variance, prefix=PREFIX, log_axis=True)
pickle_data["train_variance"] = np.array(train_variance)

################################
# After train - integration
################################

res, err = integrator.integrate(INT_SAMPLES)
relerr = err / res * 100

print(f"\n Opt. Multi-Channel integration ({INT_SAMPLES:.1e} samples):")
print("---------------------------------------------------------------")
print(f" Result: {res:.8f} +- {err:.8f} ( Rel error: {relerr:.4f} %)  ")
print("-------------------------------------------------------------\n")

pickle_data.update({
    "post_integral": float(res),
    "post_integral_err": float(err),
    "post_integral_relerr": float(relerr),
})

with open("results.pkl", "wb") as f:
    pickle.dump(pickle_data, f)


#!/usr/bin/python
# This script takes the file path of a PointResTree root file, and a parameterized PDF.
# The output file has the name *_pdf_param.dat, and is placed in the current working directory.
from scipy.ndimage import gaussian_filter1d
from scipy.optimize import curve_fit

import ROOT
import numpy as np
import sys
import getopt
import os
from tqdm import tqdm, trange

# Custom binning
# AJ's binning + higher energy bins
# list(range(10, 80, 10))
energy_bins = list(range(2, 40, 2)) + [40, 50, 60, 70]

def main(argv):
    try:
        opts, args = getopt.getopt(argv, 'o:', ['clean', 'radio'])
    except getopt.GetoptError:
        print_help()
        sys.exit(2)
    assert (len(args) == 1), print_help()
    # print(opts)
    file_path = args[0]
    assert os.path.exists(file_path), "ROOTFile does not exist!"
    file_name = os.path.basename(file_path)
    print(f"Loading {file_path}.")

    # replace ROOT extension with _pdf.dat. filename.root -> filename_pdf.dat
    pdf_filename = ''.join(os.path.splitext(file_name)[:-1]) + '_pdf_param.dat'
    has_radio = False
    for opt in opts:
        if '--radio' in opt:
            has_radio = True
        if '--clean' in opt:
            has_radio = False
        if '-o' in opt:
            # print(opt)
            pdf_filename = opt[-1] + '/' + pdf_filename
    print(
        f"Assuming ROOTFile has {'no ' if not has_radio else ''}radiologicals")

    file = ROOT.TFile(file_path)
    tree = file.PointResTree.tr
    if has_radio:
        m = 248.111
        b = 2161.06
    else:
        m = 259.218
        b = 901.312

    def charge_to_energy(q): return (q-b)/m

    functype = 'exp'  # exponential distribution
    # e_binwidth = 10
    # e_min = 0
    # e_max = 70
    nbin_en = len(energy_bins) + 1
    nbin_cosangle = 100
    cosangle_binwidth = 2 / nbin_cosangle
    pdf = np.zeros([nbin_en, nbin_cosangle])
    for e in tqdm(tree, total=tree.GetEntries()):
        if e.NTrks == 0:
            continue
        en = charge_to_energy(e.charge_corrected)
        cosAngle = e.truth_nu_dir.Dot(e.reco_e_dir)
        en_binidx = np.searchsorted(energy_bins, en)
        cosangle_binidx = int((cosAngle + 1) // cosangle_binwidth)
        pdf[en_binidx, cosangle_binidx] += 1
    # normalize pdf for each energy bin
    for i in range(len(pdf)):
        # normalize first to improve fit performance. Normalized again after the parameters are determined.
        pdf[i] = pdf[i] / np.sum(pdf[i])
    # if np.min(pdf) <= 0:
    #     print("WARNING: PDF has zero bins. This may result in log(0) down the line...", file=sys.stderr)
    # np.where(pdf==0, pdf, 0.0001)
    # Do the fit
    fit_x = np.linspace(-1, 1, num=nbin_cosangle) + 2/nbin_cosangle/2 # X axis of fit
    NParams = 5
    params = np.zeros((nbin_en, NParams))
    for (i, pdf_slice) in enumerate(pdf):
        params[i] = fit_bipeak(fit_x, pdf_slice)

    # Format:
    # Parameterized. Generated from ...
    # Energy min, Energy max, Energy binwidth
    # CosAngle min, CosAngle max
    # functype
    # Main text

    print(f"Writing to {pdf_filename}...")
    # print(params)
    with open(pdf_filename, 'w') as f:
        f.write(f'# From {file_name}\n')
        f.write(f'# Parameterized {functype}\n')
        # f.write('# Energy min, Energy max\n')
        # f.write(f'{e_min} {e_max}\n')
        f.write(f'# Energy Bin Left Edges (there is one underflow and overflow bin)\n')
        np.savetxt(f, [energy_bins], fmt='%.4e')
        f.write('# CosAngle min, CosAngle max\n')
        f.write('-1 1\n\n')
        np.savetxt(f, params)

# fit function:
def bi_peak_distribution(x, g1, g2, sigma_1, sigma_2, c):
    return g1 * np.exp(- (x+1)/sigma_1) + g2 * np.exp((x-1)/sigma_2)+c

# integral from -1 to 1
def integral(g1, g2, sigma_1, sigma_2, c):
    return 2*c + g1 * (1 - np.exp(-2/sigma_1))*sigma_1 + g2 * (1 - np.exp(-2/sigma_2))* sigma_2

# noramzlie function so that integral = 1
def normalize(params):
    integral_result = integral(*params)
    return np.asarray(params) * np.array([1/integral_result, 1/integral_result, 1, 1, 1/integral_result])

def fit_bipeak(bin_right_edge, pdf_slice):
    def bi_peak_distribution(x, g1, g2, sigma_1, sigma_2, c):
        return g1 * np.exp(- (x+1)/sigma_1) + g2 * np.exp((x-1)/sigma_2)+c

    p0 = np.array([1, 1, 1, 1, 0]) # initial condition
    bounds = (np.zeros(p0.shape), np.array([np.inf, np.inf, np.inf, np.inf, 0.1]))
    (params, cov) = curve_fit(bi_peak_distribution, bin_right_edge, pdf_slice, p0, bounds=bounds)
    rms = np.mean((pdf_slice - bi_peak_distribution(bin_right_edge, *params))**2)
    # params = normalize(params)
    if rms > 0.1:
        print("WARNING: LARGE RMS Detected. Current RMS is {rms :.2e}")
    return params

def print_help():
    print("make_PDF_parameterized.py [-o outdir] [--radio|--clean] <filename>")

if __name__ == "__main__":
    main(sys.argv[1:])


class Solution:
    def sortArrayByParity(self, nums: List[int]) -> List[int]:
        left, right = 0, len(nums)-1
        while left < right:
            # Odd Number
            if nums[left] % 2 != 0:
                # Find even from right side
                while left < right and nums[right] % 2 != 0:
                    right -= 1
                nums[left], nums[right] = nums[right], nums[left]
            left += 1
        return nums


#Windows
# #! python3
#Mac
  #! /usr/bin/env python3
#Linux
# #! /usr/bin/python3

# backupToZip.py - フォルダ全体を連番付きZIPファイルにコピーする
# Usage:
# python backupToZip.py

import zipfile, os

def backup_to_zip(folder):
#	print('Input folder : ' + folder)
	# フォルダ全体をZIPファイルにバックアップする
	folder = os.path.abspath(folder)	# folderを絶対パスにする

#	print('Absolute folder path : ' + folder)
#	print('basename : ' + os.path.basename(folder))
	# 既存のファイル名からファイル名の連番を決める
	number = 1
	while True:
		zip_filename = os.path.basename(folder) + '_' + str(number) + '.zip'
		if not os.path.exists(zip_filename):
			break
		number = number + 1

	# ZIPファイルを作成する
	print('Creating {}...'.format(zip_filename))
	backup_zip = zipfile.ZipFile(zip_filename, 'w')

	# フォルダのツリーを渡り歩いてその中のファイルを圧縮する
	# os.walkを使えば、そのフォルダのファイルリストが入手できる（以下では、filenames）
	for foldername, subfolders, filenames in os.walk(folder):
		print('Adding files in {}...'.format(foldername))
		# 現在のフォルダをZIPファイルに追加する
		backup_zip.write(foldername)
		# 現在のフォルダの中の全ファイルをZIPファイルに追加する
		for filename in filenames:
			new_base = os.path.basename(folder) + '_'
			if filename.startswith(new_base) and filename.endswith('.zip'):
				continue		# バックアップ用ZIPファイル（foldername_**.zip）はバックアップしない
			backup_zip.write(os.path.join(foldername, filename))
	backup_zip.close()
	print('Done')

cur_dir = os.getcwd()
backup_to_zip(cur_dir)


from math import sqrt

first = int(input("First side -> "))
second = int(input("Second side -> "))
third = int(input("Third side -> "))

p = (first + second + third) / 2
s = sqrt(p * (p - first) * (p - second) * (p - third))

print("Square of this triangle is {}".format(s))


"""
"""

def fact(n):
    if n<0:
        return None
    elif n==1 or n==0:
        return 1
    else:
        return n*fact(n-1)

def zeros(n):
    z = 0
    while n>=5:
        z += (n//5)
        n = n//5
    return z

t = int(input())
for i in range(t):
    n = int(input())
    print(zeros(n))


import sys

V1 = float(str(sys.argv[1]))
V2 = float(str(sys.argv[2]))
V3 = float(str(sys.argv[3]))
V4 = float(str(sys.argv[4]))
V5 = str(sys.argv[5])
V6 = float(str(sys.argv[6]))
V7 = float(str(sys.argv[7]))

import numpy as np
import math
import time

class OptionPricing:

    def __init__(self,S0, E,T, rf, sigma, dividend, iterations):
        self.S0= S0
        self.E = E
        self.T = T
        self.rf = rf
        self.sigma = sigma
        self.dividend = dividend
        self.iterations = iterations

    def call_option_simulation(self):

        option_data = np.zeros([self.iterations, 2])

        rand = np.random.normal(0, 1, [1, self.iterations])

        stock_price = self.S0*np.exp(self.T*((self.rf - self.dividend) - 0.5*self.sigma**2) + self.sigma*np.sqrt(self.T)*rand)

        option_data[:,1] = stock_price - self.E

        average = np.sum(np.amax(option_data, axis=1))/float(self.iterations)

        return np.exp(-1.0*(self.rf - self.dividend)*self.T)*average

    def pull_option_simulation(self):

        option_data = np.zeros([self.iterations, 2])

        rand = np.random.normal(0, 1, [1, self.iterations])

        stock_price = self.S0*np.exp(self.T*((self.rf - self.dividend) - 0.5*self.sigma**2) + self.sigma*np.sqrt(self.T)*rand)

        option_data[:,1] = self.E - stock_price

        average = np.sum(np.amax(option_data, axis=1))/float(self.iterations)

        return np.exp(-1.0*(self.rf - self.dividend)*self.T)*average

if __name__ == "__main__":

    S0 = V1
    E = V2
    T = V3
    rf = (V6/100)
    sigma = (V4/100)
    dividend = (V7/100)
    iterations = 10000000

    model = OptionPricing(S0,E,T,rf,sigma,dividend,iterations)
    if V5 == "Call":
        print (model.call_option_simulation())
    else:
        print (model.pull_option_simulation())


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

import logging
import sys

#logging.basicConfig(filename='example.log',level=logging.DEBUG)

"""
|  %(name)s            Name of the logger (logging channel)
|  %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
|                      WARNING, ERROR, CRITICAL)
|  %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
|                      "WARNING", "ERROR", "CRITICAL")
|  %(pathname)s        Full pathname of the source file where the logging
|                      call was issued (if available)
|  %(filename)s        Filename portion of pathname
|  %(module)s          Module (name portion of filename)
|  %(lineno)d          Source line number where the logging call was issued
|                      (if available)
|  %(funcName)s        Function name
|  %(created)f         Time when the LogRecord was created (time.time()
|                      return value)
|  %(asctime)s         Textual time when the LogRecord was created
|  %(msecs)d           Millisecond portion of the creation time
|  %(relativeCreated)d Time in milliseconds when the LogRecord was created,
|                      relative to the time the logging module was loaded
|                      (typically at application startup time)
|  %(thread)d          Thread ID (if available)
|  %(threadName)s      Thread name (if available)
|  %(process)d         Process ID (if available)
|  %(message)s         The result of record.getMessage(), computed just as
|                      the record is emitted

"""

fmt = "%(name)s %(asctime)s- %(levelname).4s--[%(funcName)s: %(lineno)s]:\t %(message)s"

logging.basicConfig(stream=sys.stdout, format=fmt, level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

logging.warning("warning msg")
logging.warn("warning msg")
logging.info("info msg")

logger = logging.getLogger(__name__)

def update_logger():
	global logger
	logger.debug('This message should go to the log file')
	logger.info('So should this')
	logger.warning('And this, too')

update_logger()

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')


#!/usr/bin/env python

import argparse
import sys
import os
import urllib
import json

import requests

HOST = 'https://api.the-odds-api.com'
PATH = '/v3/odds'

def get_url(key):
    params = urllib.parse.urlencode({
        'sport': 'americanfootball_nfl',
        'region': 'us',
        'mkt': 'h2h',
        'apiKey': key,
    })
    return f'{HOST}{PATH}/?{params}'

def main():
    ap = argparse.ArgumentParser(description=f'gets odds data from {HOST}')
    ap.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
    ap.add_argument(
        '-k', '--key', help='api key for odds api', type=str,
        default=os.environ.get('ODDS_API_KEY', '')
    )

    args = ap.parse_args()
    if not args.key:
        sys.stderr.write('No apikey provided\n')
        return -1

    try:
        response = requests.get(get_url(args.key), timeout=2)
        response.raise_for_status()
    except Exception as err:
        sys.stderr.write(f'Failed to get data. Error{err}')
        return -1
    else:
        json.dump(response.json(), args.outfile)
        used, remain = response.headers['x-requests-used'], response.headers['x-requests-remaining']
        sys.stderr.write(f'{used} of {remain} api requests remain')
    return 0

if __name__ == '__main__':
    sys.exit(main())


# Phone letter combination list
# https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
def dfs_combine(m, idx, string, out):
    if idx == len(m):
        out.append(string)
        return out

    for char in m[idx]:
        dfs_combine(m, idx + 1, string + char, out)

def letterCombination(digits):
    if not digits:
        return []

    string = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
    m = [string[int(x)] for x in digits]
    out = []

    dfs_combine(m, 0, '', out)
    return out

# print letterCombination("246")


import base64
import logging
import os
from django.shortcuts import render, redirect
from django.conf import settings as config
from django.contrib import messages
from django.views import View
import io as BytesIO
from django.http import HttpResponse, JsonResponse
from myRequest.views import UserObjectMixins
from asgiref.sync import sync_to_async
import asyncio
import aiohttp

# Create your views here.
class Disposal(UserObjectMixins, View):
    async def get(self, request):
        try:
            userID = await sync_to_async(request.session.__getitem__)("UserID")
            LTR_Name = await sync_to_async(request.session.__getitem__)("LTR_Name")
            LTR_Email = await sync_to_async(request.session.__getitem__)("LTR_Email")

            async with aiohttp.ClientSession() as session:
                task = asyncio.ensure_future(
                    self.simple_one_filtered_data(
                        session, "/QyDisposalRequest", "UserCode", "eq", userID
                    )
                )
                response = await asyncio.gather(task)

                permits = [x for x in response[0]]
                Approved = [x for x in response[0] if x["Status"] == "Approved"]

        except Exception as e:
            messages.info(request, f"{e}")
            print(e)
            return redirect("dashboard")
        if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
            return JsonResponse(permits, safe=False)

        ctx = {
            "approved": Approved,
            "LTR_Name": LTR_Name,
            "LTR_Email": LTR_Email,
        }
        return render(request, "disposal.html", ctx)

    async def post(self, request):
        try:
            disposalNo = request.POST.get("disposalNo")
            nameOfTheDestructionCompany = request.POST.get(
                "nameOfTheDestructionCompany"
            )
            placeOfDisposal = request.POST.get("placeOfDisposal")
            iAgree = eval(request.POST.get("iAgree"))
            userCode = await sync_to_async(request.session.__getitem__)("UserID")
            myAction = request.POST.get("myAction")
            if not iAgree:
                iAgree = False

            response = self.make_soap_request(
                "FnDisposalRequestCard",
                disposalNo,
                iAgree,
                nameOfTheDestructionCompany,
                placeOfDisposal,
                userCode,
                myAction,
            )
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                if response != None and response != "" and response != 0:
                    return JsonResponse({"response": str(response)}, safe=False)
                return JsonResponse({"error": str(response)}, safe=False)
            else:
                if response != "0" and response is not None and response != "":
                    messages.success(request, "Request Successful")
                    return redirect("DisposalDetails", pk=response)
                else:
                    messages.error(request, f"{response}")
                    return redirect("Disposal")
        except Exception as e:
            logging.exception(e)
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                return JsonResponse({"error": str(e)}, safe=False)
            else:
                messages.error(request, f"{e}")
                return redirect("Disposal")

class DisposalDetails(UserObjectMixins, View):
    async def get(self, request, pk):
        try:
            userID = await sync_to_async(request.session.__getitem__)("UserID")
            LTR_Name = await sync_to_async(request.session.__getitem__)("LTR_Name")
            LTR_Email = await sync_to_async(request.session.__getitem__)("LTR_Email")
            res = {}

            async with aiohttp.ClientSession() as session:
                task = asyncio.ensure_future(
                    self.simple_one_filtered_data(
                        session,
                        "/QyDisposalRequest",
                        "DisposalNo",
                        "eq",
                        pk,
                    )
                )
                task2 = asyncio.ensure_future(
                    self.simple_one_filtered_data(
                        session, "/QYRegistration", "User_code", "eq", userID
                    )
                )
                response = await asyncio.gather(task, task2)
                for task in response[0]:
                    if task["UserCode"] == userID:
                        res = task
                product = [x for x in response[1] if x["Status"] == "Approved"]
        except Exception as e:
            messages.info(request, f"{e}")
            print(e)
            return redirect("dashboard")
        if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
            return JsonResponse(res, safe=False)

        ctx = {
            "permit": res,
            "LTR_Name": LTR_Name,
            "LTR_Email": LTR_Email,
            "product": product,
        }
        return render(request, "disposal-detail.html", ctx)

    async def post(self, request, pk):
        try:
            myAction = request.POST.get("myAction")
            lineNo = request.POST.get("lineNo")
            userCode = await sync_to_async(request.session.__getitem__)("UserID")
            batchNo = request.POST.get("batchNo")
            product = request.POST.get("product")
            quantity = request.POST.get("quantity")
            reasonForDestruction = int(request.POST.get("reasonForDestruction"))
            Other_Reason = request.POST.get("Other_Reason")
            product_disposal_status = eval(request.POST.get("product_disposal_status"))
            Waste_Description = request.POST.get("Waste_Description")

            if not product:
                product = ""

            if not batchNo:
                batchNo = "None"

            if not Other_Reason:
                Other_Reason = "None"

            if not Waste_Description:
                Waste_Description = "None"

            print(Waste_Description)

            response = self.make_soap_request(
                "FnDisposalRequestLines",
                pk,
                product,
                batchNo,
                quantity,
                reasonForDestruction,
                myAction,
                lineNo,
                userCode,
                Other_Reason,
                Waste_Description,
                product_disposal_status,
            )
            print(response)
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                if response == True:
                    return JsonResponse({"response": str(response)}, safe=False)
                return JsonResponse({"error": str(response)}, safe=False)
            else:
                if response == True:
                    messages.success(request, "Request Successful")
                    return redirect("DisposalDetails", pk=pk)
                else:
                    messages.error(request, f"{response}")
                    return redirect("DisposalDetails", pk=pk)
        except Exception as e:
            logging.exception(e)
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                return JsonResponse({"error": str(e)}, safe=False)
            else:
                messages.error(request, f"{e}")
                return redirect("DisposalDetails", pk=pk)

class DisposalLines(UserObjectMixins, View):
    def get(self, request, pk):
        try:
            PermitLines = self.one_filter(
                "/QyDisposalRequestLine",
                "DisposalNo",
                "eq",
                pk,
            )

            return JsonResponse(PermitLines, safe=False)
        except Exception as e:
            print(e)
            return JsonResponse({"error": str(e)}, safe=False)

class DisposalCustomer(UserObjectMixins, View):
    async def post(self, request):
        try:
            disposalNo = request.POST.get("disposalNo")
            userCode = await sync_to_async(request.session.__getitem__)("UserID")

            response = self.make_soap_request(
                "FnDisposalRequestPayment",
                disposalNo,
                userCode,
            )
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                if response == True:
                    return JsonResponse({"response": str(response)}, safe=False)
                return JsonResponse({"error": str(response)}, safe=False)
            else:
                if response == True:
                    messages.success(request, "Request Successful")
                    return redirect("DisposalDetails", pk=disposalNo)
                else:
                    messages.error(request, f"{response}")
                    return redirect("DisposalDetails", pk=disposalNo)
        except Exception as e:
            logging.exception(e)
            if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
                return JsonResponse({"error": str(e)}, safe=False)
            else:
                messages.error(request, f"{e}")
                return redirect("DisposalDetails", pk=disposalNo)

class DisposalAttachments(UserObjectMixins, View):
    async def get(self, request, pk):
        try:
            Attachments = []
            async with aiohttp.ClientSession() as session:
                task_get_attachments = asyncio.ensure_future(
                    self.simple_one_filtered_data(
                        session, "/QYDocumentAttachments", "No_", "eq", pk
                    )
                )
                response = await asyncio.gather(task_get_attachments)

                Attachments = [x for x in response[0]]
                return JsonResponse(Attachments, safe=False)

        except Exception as e:
            logging.exception(e)
            return JsonResponse({"error": str(e)}, safe=False)

    async def post(self, request, pk):
        try:
            attachment = request.FILES.get("attachment")
            tableID = 50053
            fileName = request.POST.get("attachmentCode")
            response = False

            _, file_extension = os.path.splitext(attachment.name)
            fileName_with_extension = f"{fileName}{file_extension}"
            attachment_data = base64.b64encode(attachment.read())

            response = self.make_soap_request(
                "FnAttachementDisposalRequest",
                pk,
                fileName_with_extension,
                attachment_data,
                tableID,
            )
            if response == True:
                message = "Attachment uploaded successfully"
                return JsonResponse({"success": True, "message": message})
            error = "Upload failed: {}".format(response)
            return JsonResponse({"success": False, "error": error})
        except Exception as e:
            error = "Upload failed: {}".format(e)
            logging.exception(e)
            return JsonResponse({"success": False, "error": error})

class DisposalInvoice(UserObjectMixins, View):
    def post(self, request):
        try:
            disposalNo = request.POST.get("disposalNo")
            filenameFromApp = "invoice_" + disposalNo + ".pdf"
            response = self.make_soap_request(
                "FNGenerateDisposalRequestInvoice", disposalNo
            )

            buffer = BytesIO.BytesIO()
            content = base64.b64decode(response)
            buffer.write(content)
            responses = HttpResponse(
                buffer.getvalue(),
                content_type="application/pdf",
            )
            responses["Content-Disposition"] = f"inline;filename={filenameFromApp}"
            return responses
        except Exception as e:
            messages.error(request, f"Failed, {e}")
            logging.exception(e)
            return redirect("DisposalDetails", pk=disposalNo)

class DisposalPharmacy(UserObjectMixins, View):
    def post(self, request, pk):
        try:
            userCode = request.session["UserID"]

            response = self.make_soap_request("SubmitDisposalRequest", pk, userCode)

            if response == True:
                return JsonResponse({"response": str(response)}, safe=False)
            return JsonResponse({"error": str(response)}, safe=False)
        except Exception as e:
            print(e)
            return JsonResponse({"error": str(e)}, safe=False)

class DisposalCert(UserObjectMixins, View):
    def post(self, request, pk):
        try:
            filenameFromApp = "Pharmacy_Cert_" + pk + ".pdf"
            response = self.make_soap_request("PrintDisposalRequestCertificate", pk)
            buffer = BytesIO.BytesIO()
            content = base64.b64decode(response)
            buffer.write(content)
            responses = HttpResponse(
                buffer.getvalue(),
                content_type="application/pdf",
            )
            responses["Content-Disposition"] = f"inline;filename={filenameFromApp}"
            return responses
        except Exception as e:
            messages.error(request, f"Failed, {e}")
            logging.exception(e)
            return redirect("DisposalDetails", pk=pk)

class TechnicalRequirements(UserObjectMixins, View):
    async def get(self, request, pk):
        try:
            required_files = []
            attached = []
            filter = request.GET.get("filter")
            async with aiohttp.ClientSession() as session:
                task = asyncio.ensure_future(
                    self.simple_one_filtered_data(
                        session,
                        "/QyInspectorateRequiredDocument",
                        "Class",
                        "eq",
                        filter,
                    )
                )
                task2 = asyncio.ensure_future(
                    self.simple_fetch_data(session, "/QYDocumentAttachments")
                )
                response = await asyncio.gather(task, task2)

                required_files = [x for x in response[0]]
                attached = [x for x in response[1] if x["No_"] == pk]

                for d in required_files:
                    d["Description"] = d["Description"].replace(" ", "_")
                required_files = [
                    d
                    for d in required_files
                    if not any(a["File_Name"] == d["Description"] for a in attached)
                ]
                return JsonResponse(required_files, safe=False)
        except Exception as e:
            return JsonResponse({"error": str(e)}, safe=False)


"""Experiments with GNNs on MUTAG dataset."""
import shutil
from pathlib import Path

from model import GNN
from torch_geometric.datasets import TUDataset
from torch_geometric.nn import GATv2Conv, GCNConv, GINConv  # noqa: F401
from train import train

import graphtester as gt

SCRIPT_PATH = Path(__file__).parent

# remove the pretransformed dataset from the folder
shutil.rmtree(SCRIPT_PATH / "data" / "MUTAG", ignore_errors=True)

dataset = TUDataset(
    root=SCRIPT_PATH / "data" / "MUTAG",
    name="MUTAG",
    pre_transform=gt.pretransform(
        features=["Closeness centrality", "Eigenvector centrality"],
        feature_names="x",
    ),
)

print(dataset[0].x)
print(dataset[0].x.shape)
node_feature_count = dataset[0].x.shape[1]
num_classes = dataset.num_classes

model = GNN(node_feature_count, num_classes, 20, 0.1, conv=GINConv)
train(dataset, model)

# remove the pretransformed dataset from the folder
shutil.rmtree(SCRIPT_PATH / "data" / "MUTAG", ignore_errors=True)


import numpy as np
from util.constdef import config_instance
# SLA暂定为10ms
def origin_reward_for_basic_env(response_time, cpu, sla=8.):
    """
        A-SARSA版本的reward，代表的是代价而非奖励
    """
    p = 1
    max_reward = (1-np.exp(-p)) / 1e-3
    if cpu >= 1.0:
        reward = max_reward
    else:
        if response_time < sla:
            reward =  (1-np.exp(-p)) / (1-cpu)
        else:
            res_penalty = 1 - response_time / sla
            reward =  (1-np.exp(-p) * res_penalty) / (1-cpu)

        if reward > max_reward:
            reward = max_reward
    return -1 * reward

def custom_reward(response_time, cpu_utilization, response_sla=8, alpha=0.5):
    if cpu_utilization > 100:
        cpu_utilization = 100
    response_value = 1
    if response_time > response_sla:
        response_value = response_sla / response_time
    return alpha * response_value + (1-alpha) * cpu_utilization / 100

def custom_reward_penalty(response_time, cpu_utilization, cpu_threshold = 90, response_sla=8, response_penalty=1,penalty_ratio=8):
    """
    response_sla是联系比较紧密的变量，需要由外部提供
        给定的CPU和res为原来的值，没有经过放缩处理
        penalty_ratio，放缩惩罚值的系数，来调节分配与SLA违约之间的关系
    """
    alpha = config_instance.reward_bias_ratio
    if cpu_utilization > 100:
        cpu_utilization = 100
    if response_time <= 0:
        response_time = 1e-3
    if cpu_utilization <= cpu_threshold: # 奖励线性增加
        cpu_value = cpu_utilization / cpu_threshold
    else: # 奖励指数下降
        cpu_acc = (cpu_utilization-cpu_threshold)/100/(1-cpu_threshold/100 + 1e-6)
        cpu_value = 2 - 1 / (-cpu_acc + 1)
        if cpu_value < 0:
            cpu_value *= penalty_ratio
            if cpu_value < -penalty_ratio: # 做截断
                cpu_value = -penalty_ratio
    response_value = 1
    if response_time > response_sla:
        response_value = (2 - response_sla / response_time) * response_penalty * -1
        response_value *= penalty_ratio
    return alpha * response_value + (1-alpha) * cpu_value


import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import csv
import shutil

def _create_task_file(name):
	"""Creates a taskfile

	Positional Arguments:
		name (str): the name of the taskfile
	Return:
		Nothing
	"""
	documents_folder_path = os.path.expanduser('~/Documents/cli_ws_1.0.0')

	with open(f'{documents_folder_path}/taskfile.csv', 'w', newline='') as taskfile:
		writer = csv.writer(taskfile, delimiter=',', quotechar='"',
			quoting=csv.QUOTE_MINIMAL
		)

		writer.writerow(['task_name', 'url', 'target',
			'target_type', 'tag', 'keep_tags', 'regex',
			'user_agent', 'filename', 'path', 'dont_save', 'task_group'
		])

def create_task(task_name, url, target,
	target_type, task_group='', tag='',
	keep_tags='', regex='', user_agent='', filename='', path='', dont_save=''):
	"""Append a task to the taskfile

	Positional Arguments:
		task_name (str): the name of the task
		url (str): a web address (http://example.com/)
		target (str): name of tag, id or class
		target_type (str): tag (html element such as <li>, <p>), id or class
	Keyword Arguments:
		task_group (str): the group of tasks this task should be added to
		tag (str): if target_type is id or class, this keyword argument
            can be used to only scrape targets with this html tag
		keep_tags (bool): don't remove the html tags
		regex (str): a regular expression (applied after html tags has been removed)
		user_agent(str): use this agent when sending get request
		filename (str): the name of the file that will contain the
		scraped data
		path (str): the path for the data file
		dont_save (bool): don't save the data to a file
	Return:
		Nothing
	"""
	documents_folder_path = os.path.expanduser('~/Documents/cli_ws_1.0.0')
	if not os.path.isfile(f'{documents_folder_path}/taskfile.csv'):
		_create_task_file('taskfile.csv')

	with open(f'{documents_folder_path}/taskfile.csv', 'a', newline='') as taskfile:
		writer = csv.writer(taskfile, delimiter=',', quotechar='"',
			quoting=csv.QUOTE_NONNUMERIC
		)
		writer.writerow([task_name, url, target,
			target_type, tag, keep_tags, regex,
			user_agent, filename, path, dont_save, task_group
		])

def return_task(task_name='', task_group='', all_tasks=False):
	"""Return task

	Keyword Arguments:
		task_name (str): name of the task
		task_group (str): group name of one or more tasks
		all_tasks (bool): return all tasks
	Return:
		OrderedDict (task_name is used): the task that matches
			with that name
		list (task_group is used): all of the tasks that
			is that specific task_group
		list (all_tasks is used): all of the tasks in the taskfile
	"""
	documents_folder_path = os.path.expanduser('~/Documents/cli_ws_1.0.0')
	with open(f'{documents_folder_path}/taskfile.csv', 'r') as taskfile:
		reader = csv.DictReader(taskfile)
		task_list = []

		if task_name:
			for task in reader:
				if task['task_name'] == task_name:
					return task
			return False
		elif task_group:
			for task in reader:
				if task['task_group'] == task_group:
					task_list.append(task)
			return task_list
		elif all_tasks:
			for task in reader:
				task_list.append(task)
			return task_list
		else:
			return False

def remove_task(task_name):
	"""Remove task

	Positional Argument:
		task_name (str): name of the task
	Return:
		Nothing
	"""
	fieldnames = ['task_name', 'url', 'target', 'target_type',
		'tag', 'keep_tags', 'regex', 'user_agent','filename',
		'path', 'dont_save', 'task_group'
	]
	documents_folder_path = os.path.expanduser('~/Documents/cli_ws_1.0.0')
	with open(f'{documents_folder_path}/taskfile.csv', 'r') as taskfile, open('output.csv', 'w') as outfile:
		reader = csv.DictReader(taskfile, fieldnames=fieldnames)
		writer = csv.writer(outfile)

		for row in reader:
			if not task_name in row['task_name']:
				writer.writerow([row['task_name'], row['url'], row['target'],
					row['target_type'], row['tag'], row['keep_tags'],
					row['regex'], row['user_agent'], row['filename'], row['path'],
					row['dont_save'], row['task_group']
				])
	shutil.move('output.csv',f'{documents_folder_path}/taskfile.csv')


from models.modules import *
from utils import helper
import numpy as np
import tensorflow as tf
import pprint
import pandas as pd

#=========================================================================
#
#  This file re-implements the adversarial autoencoder in the paper of
#  Sahu et al. 2017
#
#=========================================================================

class AAE:

    def __init__(self, X_train, y_train, X_test, y_test,
                 session_id, dim_g, dim_d,
                 save_path,
                 GPU_CONFIG,
                 label_incorporated=True,
                 learning_rate={'rec': 1e-4, 'd': 1e-4, 'g': 1e-4},
                 dropout={'rec': 0.5, 'd': 0.8, 'g': 0.8},
                 batch_size=64,
                 train_epochs=500):
        self.X_train = X_train
        self.y_train = y_train
        self.X_test = X_test
        self.y_test = y_test
        self.session_id = session_id
        self.dim_g = dim_g
        self.dim_d = dim_d
        self.save_path = save_path
        self.GPU_CONFIG = GPU_CONFIG
        self.label_incorporated = label_incorporated
        self.learning_rate = learning_rate
        self.dropout = dropout
        self.batch_size = batch_size
        self.train_epochs = train_epochs

    def get_placeholder(self):
        X = tf.placeholder("float", shape=[None, self.dim_g[0]])
        Z = tf.placeholder('float', shape=[None, self.dim_g[-1]])
        keep_prob = tf.placeholder(tf.float32)
        if self.label_incorporated:
            Labels = tf.placeholder('float', shape=[None, 4])
        else:
            Labels = None
        return X, Z, keep_prob, Labels

    def generate(self, X, encoder_id, decoder_id, keep_prob):
        encoding = encoder(X, self.dim_g, encoder_id, keep_prob)
        decoding = decoder(encoding, self.dim_g, decoder_id, keep_prob)
        return encoding, decoding

    def discriminate(self, encoding, Z, discriminator_id, keep_prob, Labels):
        if Labels is not None:
            D_fake = discriminator(tf.concat([encoding, Labels], axis=1),
                                   self.dim_d, discriminator_id,
                                   keep_prob, C=4)
            D_real = discriminator(tf.concat([Z, Labels], axis=1),
                                   self.dim_d, discriminator_id,
                                   keep_prob, C=4)
        else:
            D_fake = discriminator(encoding, self.dim_d, discriminator_id,
                                   keep_prob)
            D_real = discriminator(Z, self.dim_d, discriminator_id,
                                   keep_prob)
        return D_fake, D_real

    def get_loss(self, X, decoding, D_fake, D_real):
        REC_loss = tf.reduce_mean(tf.pow(X - decoding, 2))
        D_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
                                  logits=D_fake,
                                  labels=tf.zeros_like(D_fake))
                               )
        D_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
                                  logits=D_real,
                                  labels=tf.ones_like(D_real))
                               )
        D_loss = D_loss_real + D_loss_fake
        G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
                                logits=D_fake,
                                labels=tf.ones_like(D_fake))
                               )
        return REC_loss, D_loss, G_loss

    def get_optimizer(self, REC_loss, D_loss, G_loss):
        t_vars = tf.trainable_variables()
        self.saver = tf.train.Saver(t_vars)
#         pprint.pprint(t_vars)
        rec_vars = [var for var in t_vars
                    if 'encoder' in var.name or 'decoder' in var.name]
        d_vars = [var for var in t_vars
                  if 'discriminator' in var.name or 'encoder' in var.name]
        g_vars = [var for var in t_vars if 'encoder' in var.name]
        REC_optimizer = tf.train.AdamOptimizer(
            self.learning_rate['rec']).minimize(REC_loss, var_list=rec_vars)
        D_optimizer = tf.train.AdamOptimizer(
            self.learning_rate['d']).minimize(D_loss, var_list=d_vars)
        G_optimizer = tf.train.AdamOptimizer(
            self.learning_rate['g']).minimize(G_loss, var_list=g_vars)
        return REC_optimizer, D_optimizer, G_optimizer

    def feed_code_vector(self, sess, encoding, X, keep_prob, for_test=False):
        if for_test:
            x = self.X_test
            y = self.y_test
            name = 'test'
        else:
            x = self.X_train
            y = self.y_train
            name = 'train'
        enc = sess.run(encoding, feed_dict={X: x, keep_prob: 1.0})
        enc_df = pd.DataFrame(enc, index=x.index.values)
        enc_df.to_csv(self.save_path+'code_vectors/'+name+'/'+ \
                      self.session_id+'.csv', sep='\t')

    def feed_losses(self, sess, REC_loss, D_loss, G_loss, losses, X, Z,
                    keep_prob, epoch, Labels, for_test=False):
        if for_test:
            x = self.X_test
            y = self.y_test
        else:
            x = self.X_train
            y = self.y_train
        feed_dict = {X: x,
                     Z: helper.gaussian_mixture(y),
                     keep_prob: 1.0}
        if Labels is not None:
            feed_dict[Labels] = helper.onehot(y)
        rec_l, d_l, g_l = sess.run([REC_loss, D_loss, G_loss],
                                   feed_dict=feed_dict)
        if epoch % 10 == 0 and for_test==False:
            print("Epoch %i: Reconstruction Loss: %f, Discrimnator Loss: "
                  "%f, Generator Loss: %f" % (epoch, rec_l, d_l, g_l))
        losses['rec'].append(rec_l)
        losses['d'].append(d_l)
        losses['g'].append(g_l)
        return losses

    def learn_representation(self):
        tf.reset_default_graph()
        X, Z, keep_prob, Labels = self.get_placeholder()
        encoding, decoding = self.generate(X, 'encoder', 'decoder',
                                           keep_prob)
        D_fake, D_real = self.discriminate(encoding, Z, 'discriminator',
                                           keep_prob, Labels)
        REC_loss, D_loss, G_loss = self.get_loss(X, decoding, D_fake, D_real)
        REC_optimizer, D_optimizer, G_optimizer = self.get_optimizer(
            REC_loss, D_loss, G_loss)
        # start training
        train_losses = {'rec': [], 'd': [], 'g': []}
        test_losses = {'rec': [], 'd': [], 'g': []}
        init = tf.global_variables_initializer()
        with tf.Session(config=self.GPU_CONFIG) as sess:
            sess.run(init)
            for epoch in range(self.train_epochs+1):
                X_shuffle = self.X_train.sample(frac=1)
                y_shuffle = self.y_train.reindex(X_shuffle.index)
                for i in range(int(len(self.X_train) / self.batch_size)):
                    X_batch = X_shuffle.iloc[i*self.batch_size :
                                             (i+1)*self.batch_size]
                    y_batch = y_shuffle.iloc[i*self.batch_size :
                                             (i+1)*self.batch_size]
                    z_samples = helper.gaussian_mixture(y_batch)

                    feed_dict_rec = {X: X_batch,
                                     keep_prob: self.dropout['rec']}
                    feed_dict_d = {X: X_batch, Z: z_samples,
                                   keep_prob: self.dropout['d']}
                    feed_dict_g = {X: X_batch, keep_prob: self.dropout['g']}

                    if self.label_incorporated:
                        labels = helper.onehot(y_batch)
                        feed_dict_d[Labels] = labels
                        feed_dict_g[Labels] = labels

                    sess.run(REC_optimizer, feed_dict=feed_dict_rec)
#                     for _ in range(2):
                    sess.run(D_optimizer, feed_dict=feed_dict_d)
                    for _ in range(10):
                        sess.run(G_optimizer, feed_dict=feed_dict_g)

                train_losses = self.feed_losses(
                    sess, REC_loss, D_loss, G_loss, train_losses,
                    X, Z, keep_prob, epoch, Labels, for_test=False)
                test_losses = self.feed_losses(
                    sess, REC_loss, D_loss, G_loss, test_losses,
                    X, Z, keep_prob, epoch, Labels, for_test=True)
            # save training and test losses
            pd.DataFrame(train_losses).to_csv(
                self.save_path+'losses/train/losses_'+self.session_id+'.csv',
                sep='\t')
            pd.DataFrame(test_losses).to_csv(
                self.save_path+'losses/test/losses_'+self.session_id+'.csv',
                sep='\t')
            # feed encoding with training data
            self.feed_code_vector(sess, encoding, X, keep_prob)
             # feed encoding with test data
            self.feed_code_vector(sess, encoding, X, keep_prob,
                                  for_test=True)
            # save model parameters
            self.saver.save(
                sess, self.save_path+'gan_params/'+self.session_id+'.ckpt')

    def generate_synthetic_samples(self, code_vectors):
        # select 4*100 code vectors randomly
        samples = []
        labels = []
        for j in range(4):
            s = code_vectors[self.y_train==j].sample(100)
            samples.append(s)
            labels.append(self.y_train.loc[s.index.values])
        X_samples = pd.concat(samples)
        # restore model parameters
        with tf.Session(config=self.GPU_CONFIG) as sess:
            self.saver.restore(
                sess, self.save_path+'gan_params/'+self.session_id+'.ckpt')
            t_vars = tf.trainable_variables()
#             pprint.pprint(t_vars)
            # extract decoder parameters
            dec_vars = [var for var in t_vars if 'decoder' in var.name]
            dec_params = {}
            for var in dec_vars:
                dec_params[var.name] = var.eval()
            # pass through the decoder part
            layer = tf.convert_to_tensor(X_samples.values,
                                         dtype=np.float32)
            for i in range(1, len(self.dim_g)):
                weight = tf.convert_to_tensor(
                    dec_params['decoder/W'+str(i)+':0'])
                bias = tf.convert_to_tensor(
                    dec_params['decoder/b'+str(i)+':0']
                )
                layer = tf.nn.xw_plus_b(layer, weight, bias)
                if i < len(self.dim_g)-1:
                    layer = tf.nn.leaky_relu(layer)
            X_syn = pd.DataFrame(
                layer.eval(),
                index=[v+"_syn" for v in X_samples.index.values]
            )
            X_syn.columns = self.X_train.columns.values
            # save synthetic samples
            X_syn.to_csv(
                self.save_path+'syn_samples/syn_'+self.session_id+'.csv',
                sep='\t')


from django import forms
from django.forms import ModelForm
from Employee.models import Employee, Address, EmpAddress, HRPaymentTable, LeaveTransaction

class EmployeeForm(forms.ModelForm):
    class Meta:
        model = Employee
        fields = "__all__"

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = "__all__"

class EmpAddressForm(forms.ModelForm):
    class Meta:
        model = EmpAddress
        fields = "__all__"

class HRPaymentTableForm(forms.ModelForm):
    class Meta:
        model = HRPaymentTable
        fields = "__all__"

class DateInput(forms.DateInput):
    input_type = 'date'

class LeaveTransactionForm(forms.ModelForm):
    class Meta:
        LEAVE_TYPE_CHOICES = (
            ('PL', 'Privilege Leave'),
            ('CL', 'Casual Leave'),
            ('SL', 'Sick Leave'),
        )
        LEAVE_REASON_CHOICES = (
            ('Personal', 'Personal'),
            ('Vacation', 'Vacation'),
            ('Marriage', 'Marriage'),
            ('Sick Leave', 'Sick Leave'),
            ('Family', 'Family'),
            ('Others', 'Others'),
        )
        model = LeaveTransaction
        exclude = ('eid', 'created_by', 'creation_timestamp', 'no_of_days')
        widgets = {
            'from_date': DateInput(),
            'to_date': DateInput(),
            'leave_reason': forms.Select(choices=LEAVE_REASON_CHOICES),
            'leave_type': forms.Select(choices=LEAVE_TYPE_CHOICES)
        }


import datetime

import mock
import msgpack
from oslo.config import fixture as fixture_config
from oslo.utils import netutils
from oslotest import base

from ceilometer.publisher import udp
from ceilometer.publisher import utils
from ceilometer import sample

COUNTER_SOURCE = 'testsource'

class TestUDPPublisher(base.BaseTestCase):
    test_data = [
        sample.Sample(
            name='test',
            type=sample.TYPE_CUMULATIVE,
            unit='',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'TestPublish'},
            source=COUNTER_SOURCE,
        ),
        sample.Sample(
            name='test',
            type=sample.TYPE_CUMULATIVE,
            unit='',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'TestPublish'},
            source=COUNTER_SOURCE,
        ),
        sample.Sample(
            name='test2',
            type=sample.TYPE_CUMULATIVE,
            unit='',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'TestPublish'},
            source=COUNTER_SOURCE,
        ),
        sample.Sample(
            name='test2',
            type=sample.TYPE_CUMULATIVE,
            unit='',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'TestPublish'},
            source=COUNTER_SOURCE,
        ),
        sample.Sample(
            name='test3',
            type=sample.TYPE_CUMULATIVE,
            unit='',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'TestPublish'},
            source=COUNTER_SOURCE,
        ),
    ]

    def _make_fake_socket(self, published):
        def _fake_socket_socket(family, type):
            def record_data(msg, dest):
                published.append((msg, dest))

            udp_socket = mock.Mock()
            udp_socket.sendto = record_data
            return udp_socket

        return _fake_socket_socket

    def setUp(self):
        super(TestUDPPublisher, self).setUp()
        self.CONF = self.useFixture(fixture_config.Config()).conf
        self.CONF.publisher.metering_secret = 'not-so-secret'

    def test_published(self):
        self.data_sent = []
        with mock.patch('socket.socket',
                        self._make_fake_socket(self.data_sent)):
            publisher = udp.UDPPublisher(
                netutils.urlsplit('udp://somehost'))
        publisher.publish_samples(None,
                                  self.test_data)

        self.assertEqual(5, len(self.data_sent))

        sent_counters = []

        for data, dest in self.data_sent:
            counter = msgpack.loads(data)
            sent_counters.append(counter)

            # Check destination
            self.assertEqual(('somehost',
                              self.CONF.collector.udp_port), dest)

        # Check that counters are equal
        self.assertEqual(sorted(
            [utils.meter_message_from_counter(d, "not-so-secret")
             for d in self.test_data]), sorted(sent_counters))

    @staticmethod
    def _raise_ioerror(*args):
        raise IOError

    def _make_broken_socket(self, family, type):
        udp_socket = mock.Mock()
        udp_socket.sendto = self._raise_ioerror
        return udp_socket

    def test_publish_error(self):
        with mock.patch('socket.socket',
                        self._make_broken_socket):
            publisher = udp.UDPPublisher(
                netutils.urlsplit('udp://localhost'))
        publisher.publish_samples(None,
                                  self.test_data)


#!/usr/bin/python3

"""
This module divides a matrix (List of Lists) by a given number
and then returns a new matrix (List of Lists) with the answers
to the division
"""

def matrix_divided(matrix, div):
    """
    This function calculates the division of a matrix by div.
    Div must be a number greater than 0 and must be an int
    else an error is thrown

    Matrix cannont be any other thing except an int
    """
    verify_len = []
    newlist = list()
    index = 0
    if matrix is None:
        raise TypeError("matrix must be a matrix (list of lists) \
of integers/floats")
    if type(div) != int:
        raise TypeError("div must be a number")
    elif div == 0:
        raise ZeroDivisionError("division by zero")
    elif type(matrix[0]) != list:
        raise TypeError("matrix must be a matrix (list of lists) \
of integers/floats")
    elif len(matrix) < 1:
        raise TypeError("matrix must be a matrix (list of lists) \
of integers/floats")
    else:
        for val in matrix:
            verify_len.append(len(val))
        if len(set(verify_len)) != 1:
            raise TypeError("Each row of the matrix must have the same size")
        else:
            for arr in matrix:
                newlist.append(list())
                for val in arr:
                    if type(val) != int:
                        raise TypeError("matrix must be a matrix \
(list of lists) of integers/floats")
                    else:
                        newlist[index].append(round(val/div, 2))
                index += 1
        return newlist


import time

import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

@pytest.fixture(scope="module")
def driver():
    # Initialize Chrome driver
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    yield driver
    driver.quit()

def test_home_page(driver):
    # Navigate to the URL of the web application
    driver.get("http://automationexercise.com")

    # Verify that the home page is displayed
    assert "Automation Exercise" in driver.title

def test_add_to_cart(driver):
    # Add products to the cart
    # Click 'Products' button
    click_products = driver.find_element(By.CSS_SELECTOR,
                                         "#header > div > div > div > div.col-sm-8 > div > ul > li:nth-child(2) > a")
    click_products.click()
    time.sleep(2)

    # Hover over first product and click 'Add to cart'
    first_product = driver.find_element(By.CSS_SELECTOR,
                                        "body > section:nth-child(3) > div > div > div.col-sm-9.padding-right > div.features_items > div:nth-child(3) > div > div.single-products > div.productinfo.text-center > a")
    actions = ActionChains(first_product)
    actions.move_to_element(first_product)

    # Verify that the products have been added to the cart
    #assert driver.find_element_by_css_selector(".cart-count").text == "1"

def test_checkout(driver):
    # Click the Cart button
    print("Checkout Done")

    # Verify that the cart page is displayed

    # Click the Proceed to Checkout button

    # Verify that the checkout page is displayed


from .base import TSFBaseTransformer
import numpy as np
import warnings

class StatAnalysis(TSFBaseTransformer):
    """
    StatAnalysis Model using variable window sizes.

    The StatAnalysis transformers creates windows limited by a `stat` ratio
    over the total of the time serie. The window samples are summarize in some `metrics`.

    Parameters
    ----------
    stat : str, 'variance' or callable, default: 'variance'
        Statistical measure to limit the window size. If callable,
        it should receive an array of numbers and return a single value.
    ratio : float, default: 0.1
        Ratio over the total time serie `stat` to limit the window size.
    metrics : array_like, 'variance' or 'mean' or callable, default: ['mean', 'variance']
        Array indicating the metrics to use to summarize the window content. Predefined
        metrics are 'mean' and 'variance', but callable receiving an array of samples and
        returning a single number can also be used as element of the array.
    indices : array_like, optional, default: None
        Indices of series to consider in the transformation (multivariate only). If None,
        all series will be considered.
    n_jobs : int, optional, default: 1
        Number of parallel CPU cores to use.
    horizon : str, optional, default: 1
        Distance for forecasting.

    """
    def __init__(self, stat='variance', ratio=0.1, metrics=None, n_jobs=1, indices=None, horizon=1):
        # Init superclass
        super(StatAnalysis, self).__init__(indices=indices, n_jobs=n_jobs, metrics=metrics, horizon=horizon)

        self.stat = stat
        self.ratio = ratio

        # Fit attributes
        if callable(stat):
            self._handler = stat
        else:
            self._handler = None

    def fit(self, X, y=None):
        """
        Set the stat function for time serie analysis.

        Parameters
        ----------
        X : array-like
            Previous data before transformation is appended to inputs matrix.
        y : array-like
            Time series array. If matrix, first row is consider endogenous and the rest as exogenous.

        Returns
        -------
        self : object
        """
        # User-defined handler?
        if not callable(self._handler):
            self._handler = self._get_stat_handler(y)

        return self

    def fit_transform(self, X, y=None, **fit_params):
        """
        Apply the transformation.

        Parameters
        ----------
        X : array-like
            Previous data before transformation is appended to inputs matrix.
        y : array-like
            Time series array. If matrix, first row is consider endogenous and the rest as exogenous.

        Returns
        -------
        X : array-like
            Previous data with the transformation appended for each sample of the time serie.
        """
        # User-defined handler?
        if not callable(self._handler):
            self._handler = self._get_stat_handler(y)

        return super(StatAnalysis, self).fit_transform(X, y)

    def run_preprocessing(self, serie):
        if self.ratio >= 1:
            warnings.warn("Ratio param is too big (%d). Consider using a ratio <1" %
                          self.ratio, Warning)

        if self._handler.__name__ == "incremental_variance":
            limit = np.var(serie) * self.ratio
        else:
            limit = self._handler(serie) * self.ratio

        partial_X = []

        for index, output in enumerate(serie[self.horizon:]):
            index = index + 1

            # First previous samples
            pivot = index - 1
            samples = serie[pivot:index]
            if self._handler.__name__ == "incremental_variance":
                n = len(samples)
                previous_var = np.var(samples)
                previous_mean = np.mean(samples)
                while pivot - 1 >= 0 and previous_var < limit:
                    n = n + 1
                    previous_var, previous_mean = self._handler(n, previous_mean, previous_var, serie[pivot - 1])
                    if previous_var < limit:
                        pivot = pivot - 1
                        samples = serie[pivot:index]
            else:
                while pivot - 1 >= 0 and self._handler(serie[pivot - 1:index]) < limit:
                    pivot = pivot - 1
                    samples = serie[pivot:index]

            # Once we have the samples, gather info about them
            samples_info = []
            for metric in self.metrics:
                samples_info.append(_get_samples_info(samples, metric))
            partial_X.append(samples_info)

        return np.array(partial_X)

    def _get_stat_handler(self, y):
        if self.stat == 'variance':
            return incremental_variance
        else:
            raise ValueError("Invalid stat argument for StatAnalysis. Please use ['variance'] or own stat function.")

# Incremental variance stat handler
# Source: http://datagenetics.com/blog/november22017/index.html
def incremental_variance(n_data, previous_mean, previous_var, new_value):
    # We need mean
    def incremental_mean(n, previous, new):
        mean = previous + (new - previous) / float(n)
        return mean

    new_mean = incremental_mean(n_data, previous_mean, new_value)
    previous_sn = previous_var * (n_data-1)
    new_sn = previous_sn + (new_value - previous_mean) * (new_value - new_mean)

    return new_sn/float(n_data), new_mean

class ClassChange(TSFBaseTransformer):
    """
    ClassChange Model using variable window sizes.

    The ClassChange model is a classification and multivariate oriented transformer to create
    variable window sizes based on a class change detection. It creates windows from the
    endogenous serie, and then summarize the content of the windows for every exogenous.
    At least one exogenous serie is required to use ClassChange transformer.

    IMPORTANT: ClassChange use is intended for categorical endogenous time series.

    Parameters
    ----------
    metrics : array_like, 'variance' or 'mean' or callable, default: ['mean', 'variance']
        Array indicating the metrics to use to summarize the windows content. Predefined
        metrics are 'mean' and 'variance', but callable receiving an array of samples and
        returning a single number can also be used as element of the array.
    indices : array_like, optional, default: None
        Indices of series to consider in the transformation (multivariate only). If None,
        all series will be considered.
    n_jobs : int, optional, default: 1
        Number of parallel CPU cores to use.
    horizon : str, optional, default: 1
        Distance for forecasting.

    """
    def __init__(self, metrics=None, n_jobs=1, indices=None, horizon=1):
        # Init superclass
        super(ClassChange, self).__init__(indices=indices, n_jobs=n_jobs, metrics=metrics, horizon=horizon)

        self.umbralized_serie = None

    def fit(self, X, y=None):
        """
        Umbralize serie if needed.

        Parameters
        ----------
        X : array-like
            Previous data before transformation is appended to inputs matrix.
        y : array-like
            Time series array. If matrix, first row is consider endogenous and the rest as exogenous.

        Returns
        -------
        self : object
        """
        # Involved series
        self.set_involved_series(y)

        self.umbralized_serie = self.series[0]
        self.series = np.delete(self.series, 0, axis=0)

        return self

    def transform(self, X, y=None):
        """
        Apply the transformation.

        Parameters
        ----------
        X : array-like
            Previous data before transformation is appended to inputs matrix.
        y : array-like
            Time series array. If matrix, first row is consider endogenous and the rest as exogenous.

        Returns
        -------
        X : array-like
            Previous data with the transformation appended for each sample of the time serie.
        """
        # We need al least one exog serie
        y = self.check_consistent_y(y)

        if len(y.shape) == 1 or y.shape[0] < 2:
            raise ValueError("ClassChange need tor receive one exogenous serie at least. Please use"
                             "an 'y' 2D array with at least 2 rows.")
        X = self.fit_transform(X, y)

        return X

    def fit_transform(self, X, y=None, **fit_params):
        """
        Apply the transformation.

        Parameters
        ----------
        X : array-like
            Previous data before transformation is appended to inputs matrix.
        y : array-like
            Time series array. If matrix, first row is consider endogenous and the rest as exogenous.

        Returns
        -------
        X : array-like
            Previous data with the transformation appended for each sample of the time serie.
        """
        # Involved series
        self.set_involved_series(y)

        self.umbralized_serie = self.series[0]
        self.series = np.delete(self.series, 0, axis=0)

        y = self.check_consistent_y(y)
        # Consistent params
        X, self.series = self.check_consistent_params(X, self.series)

        # We need al least one exog serie
        if len(y.shape) == 1 or y.shape[0] < 2:
            raise ValueError("ClassChange need to receive one exogenous serie at least. Please use"
                             "an 'y' 2D array with at least 2 rows.")

        # Get exogs series info
        partial_X = self.run_preprocessing()

        X = self.append_inputs(X, partial_X)

        return X

    def run_preprocessing(self):
        partial_X = []

        # Info from every output from all exogs series
        for index, output in enumerate(self.umbralized_serie[self.horizon:]):
            index = index + 1
            output_info = []
            pivot = index - 1
            previous = self.umbralized_serie[pivot]

            # Window size
            while pivot > 0 and previous == self.umbralized_serie[pivot - 1]:
                pivot = pivot - 1
            start = pivot
            end = index

            # Info from exog series
            for exog in self.series:
                samples = exog[start:end]
                for metric in self.metrics:
                    output_info.append(_get_samples_info(samples, metric))

            partial_X.append(output_info)

        return np.array(partial_X)

# Auxiliary function for gathering window values information
def _get_samples_info(samples, metric):
    # Valid metric?
    valid_metrics = ['mean', 'variance']
    if metric not in valid_metrics and not callable(metric):
        raise ValueError("Unkown '%s' metric" % metric)

    if callable(metric):
        return metric(samples)
    else:
        return {
            'mean': np.mean(samples),
            'variance': np.var(samples)
        }.get(metric)


import ROOT
from plotMass import file_names, get_tree

#infile = ROOT.TFile("GluGlu_HToMuMu_M125_GEN_test.root","READ")
#infile = ROOT.TFile("/eos/cms/store/group/phys_higgs/HiggsExo/H2Mu/UF/ntuples/data_2017_and_mc_fall17/VBFHToMuMu_M125_13TeV_amcatnlo_pythia8/H2Mu_VBF/181003_121220/tuple_all.root","READ")
#tree = infile.Get("dimuons/tree")

samp = "ggh18bsr_hadded"
file_name = file_names[samp]
tree = get_tree(file_name)

variable = "massRes"
#variable = "ptRes"

#positive muons

#mass viariables
#variable_kinfit_string = "(muPairs.mass_kinfit - 125.):muons.d0_PV"
variable_kinfit_string = "(muPairs.mass_bs - 125.):muons.d0_PV"
variable_pf_string = "(muPairs.mass - 125.):muons.d0_PV"

#pt variable
#variable_kinfit_string = "(muons.pt_kinfit - muons.GEN_pt):muons.d0_PV"
#variable_kinfit_string = "(muons.pt_bs - muons.GEN_pt):muons.d0_PV"
#variable_pf_string = "(muons.pt - muons.GEN_pt):muons.d0_PV"

muon_selection = "(muons.pt > 20 & abs(muons.eta)<2.4 & muons.isMediumID==1 & muons.relIso < 0.25)"
event_selection = "muons.pt>20" # dummy
#event_selection = "(muons.pt>30 && ( muons[0].isHltMatched[2]==1 || muons[0].isHltMatched[3]==1 || muons[1].isHltMatched[2]==1 || muons[1].isHltMatched[3]==1 ) )"
weightstring = "(GEN_wgt * PU_wgt)"

#cut_string = "muons.charge>0 & abs(muons.pt_kinfit - muons.GEN_pt)<20"
cut_string = "muons.charge>0 & abs(muons.pt_bs - muons.GEN_pt)<20"

c1 = ROOT.TCanvas("c1","c1",900,600)
kfgend0pos = ROOT.TH2F("kf{0}gend0pos".format(variable),"kf{0}gend0pos".format(variable),20,-0.005,0.005,20,-5,5)
pfgend0pos = ROOT.TH2F("pf{0}gend0pos".format(variable),"pf{0}gend0pos".format(variable),20,-0.005,0.005,20,-5,5)

tree.Draw("{0}>>kf{1}gend0pos".format(variable_kinfit_string,variable),"{0} * ({1} & {2} & {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")
tree.Draw("{0}>>pf{1}gend0pos".format(variable_pf_string,variable),"{0} * ({1} & {2} & {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")

kfgend0pos_pfx = kfgend0pos.ProfileX("kf{0}gend0pos_pfx".format(variable))
pfgend0pos_pfx = pfgend0pos.ProfileX("pf{0}gend0pos_pfx".format(variable))
kfgend0pos_pfx.SetLineColor(ROOT.kRed)
kfgend0pos_pfx.Draw()
ROOT.gPad.Update()
stat_kfgend0pos_pfx = kfgend0pos_pfx.FindObject("stats")
print(kfgend0pos_pfx.GetRMS(1))
print(kfgend0pos_pfx.GetRMS(2))
pfgend0pos_pfx.Draw("same")
c2 = ROOT.TCanvas("c2","c2",600,600)
pfgend0pos_pfx.Draw()
ROOT.gPad.Update()
#ROOT.gStyle.SetOptStat(1111)
stat_pfgend0pos_pfx = ROOT.TPaveText()
stat_pfgend0pos_pfx = pfgend0pos_pfx.FindObject("stats")
print(pfgend0pos_pfx.GetRMS(1))
print(pfgend0pos_pfx.GetRMS(2))
stat_pfgend0pos_pfx.SetY1NDC(.7)
stat_pfgend0pos_pfx.SetY2NDC(.9)
stat_pfgend0pos_pfx.SetX1NDC(.7)
stat_pfgend0pos_pfx.SetX2NDC(.9)
c1.cd()
pfgend0pos_pfx.Draw("same")
stat_kfgend0pos_pfx.SetY1NDC(.5)
stat_kfgend0pos_pfx.SetY2NDC(.7)
stat_kfgend0pos_pfx.SetX1NDC(.9)
stat_kfgend0pos_pfx.SetX2NDC(.7)
stat_pfgend0pos_pfx.Draw("same")

#negative muons
cut_string = "muons.charge<0 & abs(muons.pt_kinfit - muons.GEN_pt)<20"

c3 = ROOT.TCanvas("c3","c3",900,600)
kfgend0neg = ROOT.TH2F("kf{0}gend0neg".format(variable),"kf{0}gend0neg".format(variable),20,-0.005,0.005,20,-5,5)
pfgend0neg = ROOT.TH2F("pf{0}gend0neg".format(variable),"pf{0}gend0neg".format(variable),20,-0.005,0.005,20,-5,5)
tree.Draw("{0}>>kf{1}gend0neg".format(variable_kinfit_string,variable),"{0} * ({1} && {2} && {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")
tree.Draw("{0}>>pf{1}gend0neg".format(variable_pf_string,variable),"{0} * ({1} && {2} && {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")
kfgend0neg_pfx = kfgend0neg.ProfileX("kf{0}gend0neg_pfx".format(variable))
pfgend0neg_pfx = pfgend0neg.ProfileX("pf{0}gend0neg_pfx".format(variable))
kfgend0neg_pfx.SetLineColor(ROOT.kRed)
kfgend0neg_pfx.Draw()
ROOT.gPad.Update()
stat_kfgend0neg_pfx = kfgend0neg_pfx.FindObject("stats")
print(kfgend0neg_pfx.GetRMS(1))
print(kfgend0neg_pfx.GetRMS(2))
pfgend0neg_pfx.Draw("same")
#pfmassgend0neg = ROOT.TH2F("pfmassgend0neg","pfmassgend0neg",20,-0.005,0.005,20,-5,5)
#kfmassgend0neg = ROOT.TH2F("kfmassgend0neg","kfmassgend0neg",20,-0.005,0.005,20,-5,5)
#tree.Draw("{0}>>kfmassgend0neg".format(variable_kinfit_string),"{0} * ({1} && {2} && {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")
#tree.Draw("{0}>>pfmassgend0neg".format(variable_pf_string),"{0} * ({1} && {2} && {3})".format(weightstring,event_selection,muon_selection,cut_string),"goff")
#kfmassgend0neg_pfx = kfmassgend0neg.ProfileX("kfmassgend0neg_pfx")
#pfmassgend0neg_pfx = pfmassgend0neg.ProfileX("pfmassgend0neg_pfx")
#kfmassgend0neg_pfx.SetLineColor(ROOT.kRed)
#kfmassgend0neg_pfx.Draw()
#ROOT.gPad.Update()
#stat_kfmassgend0neg_pfx = kfmassgend0neg_pfx.FindObject("stats")
#pfmassgend0neg_pfx.Draw("same")
c4 = ROOT.TCanvas("c4","c4",600,600)
pfgend0neg_pfx.Draw()
ROOT.gPad.Update()
#ROOT.gStyle.SetOptStat(1111)
stat_pfgend0neg_pfx = ROOT.TPaveText()
stat_pfgend0neg_pfx = pfgend0neg_pfx.FindObject("stats")
stat_pfgend0neg_pfx.SetY1NDC(.7)
stat_pfgend0neg_pfx.SetY2NDC(.9)
stat_pfgend0neg_pfx.SetX1NDC(.7)
stat_pfgend0neg_pfx.SetX2NDC(.9)
c3.cd()
pfgend0neg_pfx.Draw("same")
stat_kfgend0neg_pfx.SetY1NDC(.5)
stat_kfgend0neg_pfx.SetY2NDC(.7)
stat_kfgend0neg_pfx.SetX1NDC(.9)
stat_kfgend0neg_pfx.SetX2NDC(.7)
stat_pfgend0neg_pfx.Draw("same")

#pfmassgend0neg_pfx.Draw()
#ROOT.gPad.Update()
##ROOT.gStyle.SetOptStat(1111)
#stat_pfmassgend0neg_pfx = ROOT.TPaveText()
#stat_pfmassgend0neg_pfx = pfmassgend0neg_pfx.FindObject("stats")
#stat_pfmassgend0neg_pfx.SetY1NDC(.7)
#stat_pfmassgend0neg_pfx.SetY2NDC(.9)
#stat_pfmassgend0neg_pfx.SetX1NDC(.7)
#stat_pfmassgend0neg_pfx.SetX2NDC(.9)
#c3.cd()
#pfmassgend0neg_pfx.Draw("same")
#stat_kfmassgend0neg_pfx.SetY1NDC(.5)
#stat_kfmassgend0neg_pfx.SetY2NDC(.7)
#stat_kfmassgend0neg_pfx.SetX1NDC(.9)
#stat_kfmassgend0neg_pfx.SetX2NDC(.7)
#stat_pfmassgend0neg_pfx.Draw("same")

#mass plots
#c1.SaveAs("hmassgend0pos_pfx.pdf")
#c3.SaveAs("hmassgend0neg_pfx.pdf")

#Save plots
c1.SaveAs("h_{0}gend0pos_pfx.pdf".format(variable))
c3.SaveAs("h_{0}gend0neg_pfx.pdf".format(variable))


import math
a = float(input('Digite o valor de a: '))
b = float(input('Digite o valor de b: '))
c = float(input('Digite o valor de c: '))

delta = b ** 2 - 4 * a * c

if delta < 0:
    print('esta equação não possui raízes reais')
else:
    deltaraiz = math.sqrt(delta)
    x1 = (-b+(deltaraiz))/(2*a)
    x2 = (-b-(deltaraiz))/(2*a)
    if delta == 0:
        print(f'a raiz dupla desta equação é', x1)
    if x1 != x2 and x2 > x1:
        print(f'as raízes da equação são',x1,'e',x2)
    if x2 != x1 and x1 > x2:
        print(f'as raízes da equação são',x2,'e',x1)


import cv2
import numpy as np

def gray(img):
    red = img[:, :, 2].copy()
    green = img[:, :, 1].copy()
    blue = img[:, :, 0].copy()
    Y = 0.21626*red + 0.7152*green + 0.0722*blue
    return Y.astype(np.uint8)

img = cv2.imread("test1.jpg")
gray = gray(img)

gray[gray < 128] = 0
gray[gray > 128] = 255

cv2.imwrite("answer1.bmp",gray)
cv2.imshow("result1",gray)

cv2.waitKey(0)
cv2.destroyAllwindows()


import requests, json

class APIConfig(object):
    def __init__(self, api_domain="api.sinemalar.com", api_protocol="http", api_version="v1"):

        if api_protocol not in ('http', 'https'):
            raise ValueError("The protocol must be 'http' or 'https'.")

        self.api_domain = api_domain
        self._api_protocol = api_protocol
        self._api_version = api_version

    def get_url(self):
        if self.api_domain == "www.sinemalar.com":
            return "{protocol}://{domain}/json/mobile".format(
                protocol=self._api_protocol,
                domain=self.api_domain,
            )
        else:
            return "{protocol}://{domain}/ajax/json/ios/{version}".format(
                protocol=self._api_protocol,
                domain=self.api_domain,
                version=self._api_version,
            )

class Method(APIConfig):
    def __init__(self, method="get"):
        self._method = method
        APIConfig.__init__(self)
        self._splitter_char = "/"

    def add_to_url(self, path, *continue_urls):
        for i in continue_urls:
            path = "{}{splitter}{}".format(path, i, splitter=self._splitter_char)
        return path

    def GET(self, *continue_urls):
        method = "get"

        if self.api_domain == "www.sinemalar.com" or continue_urls[0] == "gallery" or continue_urls[0] == "gps":
            return json.loads(requests.get(self.add_to_url(self.get_url(), *continue_urls)).content)
        else:
            return json.loads(requests.get(self.add_to_url(self.get_url(), 'get', *continue_urls)).content)

    def POST(self, *continue_urls):
        method = "post"
        return json.loads(requests.post(self.add_to_url(self.get_url(), 'get', *continue_urls)).content)

class CallObject(Method):
    def __init__(self):
        Method.__init__(self)

    @staticmethod
    def is_True(boolean):
        if boolean:
            return "1"
        else:
            return "0"


import sys
sys.setrecursionlimit(2*10**6)
N = int(input())
G = [[]for _ in range(N)]
subtree = [0]*N
depth = [0]*N
ans = [0]*N
def dfs(G, v, p, d, mode="w"):
    if mode == "w":
        depth[v] = d
        for next_v in G[v]:
            if next_v == p:
                continue
            dfs(G, next_v, v, d+1)
        subtree[v] = 1
        for c in G[v]:
            subtree[v] += subtree[c]
    else:
        for next_v in G[v]:
            if next_v == p:
                continue
            ans[next_v] = ans[v] + N - 2 * subtree[next_v]
            dfs(G, next_v, v, 0, mode="r")
for _ in range(N-1):
    u, v = map(lambda x: int(x) - 1, input().split())
    G[u].append(v)
    G[v].append(u)
root = 0
# 探索(-1はv[0]の親がいないことを表す）
dfs(G, root, -1, 0)
ans[root] = sum(depth)
dfs(G, root, -1, 0, mode="r")
print(*ans, sep="\n")
f()


# 다음은 문자열을 입력받아 역 순서의 문자열을 리턴하는 프로그램이다. 빈칸을 채우시오
# import nltk
# nltk.download('book',quiet=True)
# from nltk.book import *

def reverse(L,a):
    n = len(L)
    if a < n-1 :
        L[a],L[n-a-1] = L[n-a-1],L[a]
        reverse(L,a+1)

L = list(input('문자열을 입력하세요:')) #문자열 입력받기
reverse(L,0)
print("".join(str(x) for x in L)) #역 순서의 문자열 출력

# 다 못풀었음...


import random
class Paddle:

     # Описание платформы для игры

    def __init__(self, canvas, color, pos_x, command_up_key, command_down_key):

        # canvas означает, что платформа будет нарисована на нашем изначальном холсте
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 10, 100, fill=color)

        self.starting_point_x = pos_x
        self.canvas.move(self.id, self.starting_point_x, 200)

        self.vy = 0
        self.canvas_height = self.canvas.winfo_height() #высота окна
        #обработка жмяканий
        self.command_down_key = command_down_key
        self.command_up_key = command_up_key
        self.canvas.bind_all(self.command_up_key, self.turn_up)
        self.canvas.bind_all(self.command_down_key, self.turn_down)
        # '<KeyPress-Left>'

    def update_controls(self):
        self.canvas.bind_all(self.command_up_key, self.turn_up)
        self.canvas.bind_all(self.command_down_key, self.turn_down)

    def turn_up(self, event):
        self.vy = -4

    def turn_down(self, event):
        self.vy = 4

    def draw(self):#движение платформы
        pos = self.canvas.coords(self.id)#координаты
        if pos[1] <= 0 and self.vy < 0:                     # крайние положения - верх
            self.vy = 0
        elif pos[3] >= self.canvas_height and self.vy > 0:   # низ
            self.vy = 0

        self.canvas.move(self.id, 0, self.vy)