'''
给定一个正整数 x，我们将会写出一个形如 x (op1) x (op2) x (op3) x ... 的表达式，其中每个运算符 op1，op2，… 可以是加、减、乘、除（+，-，*，或是 /）之一。
例如，对于 x = 3，我们可以写出表达式 3 * 3 / 3 + 3 - 3，该式的值为 3 。
在写这样的表达式时，我们需要遵守下面的惯例：
除运算符（/）返回有理数。
任何地方都没有括号。
我们使用通常的操作顺序：乘法和除法发生在加法和减法之前。
不允许使用一元否定运算符（-）。例如，“x - x” 是一个有效的表达式，因为它只使用减法，但是 “-x + x” 不是，因为它使用了否定运算符。
我们希望编写一个能使表达式等于给定的目标值 target 且运算符最少的表达式。返回所用运算符的最少数量。
'''

def leastOps(x, target):
    #不需要操作符的情况
    if x == target:
        return 0
    #如果下不是1，要凑出1就必须是x/x
    elif target == 1:
        return 1
    #如果x是1，要凑出target，就只能是数个x相加
    elif x == 1:
        return (target - 1)
    elif target < x:
        return min(2 * target - 1, 2 * (x - target))
    else:
        #分解为x^n + d或者x^(n+1)-d
        exp = 1
        power = x
        while power < target:
            power *= x
            exp += 1
        if power == target:
            return (exp - 1)
        lowerNearest = power // x
        lowerExp = exp - 1
        lowerDiff = target - lowerNearest
        higherNearest = power
        higherExp = exp
        higherDiff = higherNearest - target
        if lowerDiff < target:
            lowerRes = lowerExp - 1 + 1 + leastOps(x, lowerDiff)
        else:
            lowerRes = 1145141919
        if higherDiff < target:
            highRes = higherExp - 1 + 1 + leastOps(x, higherDiff)
        else:
            highRes = 1145141919
        return min(lowerRes, highRes)

x = int(input())
target = int(input())
res = int(leastOps(x, target))
print(res)


# Get Synonyms from WordNet
from nltk.corpus import wordnet

# synonyms = wordnet.synsets("world")

# print(synonyms[0].definition())
# print(synonyms[0].examples())

synonyms = []

for syn in wordnet.synsets('Computer'):

    for lemma in syn.lemmas():
        synonyms.append(lemma.name())

print(synonyms)


from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from cauth.models import UserProfile
from BetApp.models import Bet
from forms import BetForm
from random import randint
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

# Create your views here.

def index(request):
	return render(request, 'index.html', {'home_active': True})

@login_required
def balance(request):
	profile = UserProfile.objects.get(user_id=request.user.id)
	if request.method == "POST":
		input_balance = int(request.POST.get('balance'))
		if input_balance > 50:
			input_balance += input_balance * 0.10
		profile.initial_balance += input_balance
		profile.save()
	balance = int(profile.initial_balance)
	return render(request, 'balance.html', {'balance': balance, 'balance_active': True})

@login_required
def bet(request):
	profile = UserProfile.objects.get(user_id=request.user.id)
	balance = int(profile.initial_balance)
	if request.method == "POST":
		form = BetForm(request.POST)
		if form.is_valid:
			bet = int(request.POST.get('bet'))
			choice = 1
			if bet > balance:
				return render(request, 'bet.html', {'bet_active': True, 'balance': balance, 'errors': True, 'error': 'You do not have suficient credits.'})
			elif bet == 0 or bet > 20:
				return render(request, 'bet.html', {'bet_active': True, 'balance': balance, 'errors': True, 'error': 'You must bet 1 - 20.'})
			else:
				# --------- bet logic
				# 0 -> win  1 -> lose
				choice = randint(0,1)
				win = bet + (20/bet) * 100
				betDB = Bet()
				betDB.bet = bet
				betDB.choice = choice
				betDB.winnings = win
				betDB.user = request.user
				betDB.save()

				# Update user balance
				profile.initial_balance -= bet

				if choice == 0:
					profile.initial_balance += win
					profile.save()
					balance = int(profile.initial_balance)
					return render(request, 'bet.html', {'bet_active': True, 'balance': balance, 'play': True, 'win': win, 'winner': True})
				else:
					profile.save()
					balance = int(profile.initial_balance)
					return render(request, 'bet.html', {'bet_active': True, 'balance': balance, 'play': True, 'win': 0, 'winner': False})
	else:
		return render(request, 'bet.html', {'bet_active': True, 'balance': balance, 'play': False})

@login_required()
def bet_history(request):
	args = {}
	Bets = Bet.objects.filter(user_id=request.user.id)
	args['Bets'] = Bets
	return render(request, 'bet_history.html', args)


import csv
#import sys
f = open('input.csv','r')
w = open('output1.csv','w')

# discretization parameters
n1 = 10 # for PM2.5
n2 = 10 # for PM10
n3 = 10 # for PM1
n4_start = 2.0 # start of temperature
n4 = 4.0 # for temperature
n5 = 10 # for humidity
count = 0

next(csv.reader(f))
for row in csv.reader(f):
	# discretize PM2.5
	if row[3] != 'nan':
		row[3] = float(row[3])
		row[3] = row[3] - row[3] % n1
		row[3] = str(row[3])

	# discretize PM10
	if row[4] != 'nan':
		row[4] = float(row[4])
		row[4] = row[4] - row[4] % n2
		row[4] = str(row[4])

	# discretize PM1
	if row[5] != 'nan':
		row[5] = float(row[5])
		row[5] = row[5] - row[5] % n3
		row[5] = str(row[5])

	# discreteize temperature
	if row[6] != 'nan':
		row[6] = float(row[6])
		row[6] = row[6] - n4_start
		row[6] = row[6] - row[6] % n4
		row[6] = row[6] + n4_start
		row[6] = str(row[6])

	# discrete humidity
	if row[7] != 'nan':
		row[7] = float(row[7])
		row[7] = row[7] - row[7] % n5
		row[7] = str(row[7])

	csv.writer(w).writerow(row)
	count = count + 1
#	sys.stdout.write("\r%d" % count)
#	sys.stdout.flush()
#print ''


N = int(input())
l = []
for x in range(N): l.append(input().split(" "))

for x in l:
  for z in range(N):
    x[z] = int(x[z])

def minu(x, y):
  minus = []
  for z in range(y):
    minus.append(min(x))
    x.remove(min(x))
  return minus

sum = []
for w in l:
  c = 1
  while c <= N:
    sum.append(minu(w, N - 1))
  c += 1


# Given three integers between 1 and 11, if their sum is less than or equal to 21, return their
# sum. If their sum exceeds 21 and there's an eleven, reduce the total sum by 10. Finally, if
# the sum (even after adjustment) exceeds 21, return 'BUST'

def blackjack(n1, n2, n3):
    hand = [n1, n2, n3]
    total = sum(hand)
    if 11 in hand:
        total -= 10
    if total <= 21:
        return total
    else:
        return "BUST"


#! /usr/bin/env python3

# Echo client program
import socket, sys, re

sys.path.append("../lib")       # for params
import params

from framedSock import framedSend, framedReceive

print( "would you like to use stammer proxy or file server?" )

choice = input()

if (choice == "stammer proxy") :        # picks stammer proxy or file server depending on user input
    ipAd = "127.0.0.1:50000"
elif (choice == "file server") :
    ipAd = "127.0.0.1:50001"

switchesVarDefaults = (
    (('-s', '--server'), 'server', ipAd),
    (('-d', '--debug'), "debug", False), # boolean (set if present)
    (('-?', '--usage'), "usage", False), # boolean (set if present)
    )

progname = "framedClient"
paramMap = params.parseParams(switchesVarDefaults)

server, usage, debug  = paramMap["server"], paramMap["usage"], paramMap["debug"]

if usage:
    params.usage()

try:
    serverHost, serverPort = re.split(":", server)
    serverPort = int(serverPort)
except:
    print("Can't parse server:port from '%s'" % server)
    sys.exit(1)

s = None
for res in socket.getaddrinfo(serverHost, serverPort, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        print("creating sock: af=%d, type=%d, proto=%d" % (af, socktype, proto))
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        print(" error: %s" % msg)
        s = None
        continue
    try:
        print(" attempting to connect to %s" % repr(sa))
        s.connect(sa)
    except socket.error as msg:
        print(" error: %s" % msg)
        s.close()
        s = None
        continue
    break

if s is None:
    print('could not open socket')
    sys.exit(1)

print("Enter file name: ")
fileName = input()      # saves file name

try:
    oFile = open(fileName, "rb")        # opens file to read
    data = oFile.read()

    framedSend(s, fileName.encode(), debug)     # sends file name to the server

    i=0
    while i <= len(data):
        str = data[i:i+100]        # reads every 100 bytes
        framedSend(s, str, debug)   #sends every 100 bytes
        i+=100

    framedSend(s, b"%%e", debug)  # signal end of input
except (FileNotFoundError) :                # checks for files that do not exist
    print("Wrong file or file path")


'''
Created on Feb 13, 2012

@author: giulio
'''

FilterString = "({proto} {dir_port} {flags})"

ConfigHard = dict()

ConfigHard['multi-address'] = "MetricMultiAddr"
ConfigHard['multi-homing'] = "MetricMultiHome"
ConfigHard['subnet'] = "MetricSubnet"
ConfigHard['prob'] = "MetricProb"
ConfigHard['printcount'] = "MetricPrintable"

ThreadPoolTemplate = \
"""         <threadpool id="Thread_pool{0}" num_threads="1"><core number="{0}"/></threadpool>
"""

SnifferTemplate= \
"""         <block id="Sniffer{0}" type="PFQSource" sched_type="active" threadpool="Thread_pool{0}">
	            <params>
		            <queues device="eth4">
			            {1}
		            </queues>
	            </params>
         </block>
"""

SnifferPcapTemplate=\
"""
<block id="Sniffer0" type="PcapSource" sched_type="active" threadpool="Thread_pool0"><params><!--<queues device="{source_name}"/>--><source type="{source_type}" name="{source_name}"/>

		{hw_metrics}

		<!-- <bpf_filter expression="" /> --></params></block>
"""

EventFactoryTemplate= \
""" <block id="Factory{id}" type="EventFactory">  <!-- PacketPlus_Factory -->
            <params>
                {Fields}
                <timeouts classes = "{timeout_classes}"/>
                {KeyRules}
            </params>
         </block>
"""

FeatureBlockTemplate=\
"""
    <block id="FeatureBlock{0}" type="FeatureBlock">
        <params>
        </params>
    </block>
"""

FilteringBlockTemplate=\
"""
        <block id="FilteringBlock{0}" type="FilteringBlock">
            <params>
            </params>
        </block>
"""

ExporterBlockTemplate=\
"""
    <block id="ExporterBlock0" type="Exporter">
        <params protocol="{protocol}" ip="{ip}" port="{port}">
        </params>
    </block>
"""

PrimaryKeyTemplate = \
"""
<primary-key name="{name}" proto="{proto_code}" src_port="{src_port}" dst_port="{dst_port}" flags="{flags}" value="{val}" />
"""

ConnectionTemplate = \
"""
<connection src_block="{0}" src_gate="{1}" dst_block="{2}" dst_gate="{3}"/>
"""

TableLibHeader = \
"""
#pragma once

#define DECL(T, id) T* _t##id=0;

#define TCHECK(tname, key, id) \
    (_t##id=(tname)->get( (key) ) )

#define TVALUE(id) \
    *(_t##id)

#define TCONDGET(tname, key, id) \\
    (_t##id=(tname)->get( (key) )) && *(_t##id)

#define TGET(tname, key) \\
    (tname)->get( (key) )

"""

BotstreamLibHeader = \
"""
#include <cstdlib>
#include <type_traits>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <chrono>
#include <sstream>
#include <string>
#include <algorithm>
#include <fstream>

#include <pugixml.hpp>

#include "actionlib.hpp"
#include "operators.hpp"
#include "tables.hpp"

#include <unordered_set>

#define gt >
#define gt_eq >=
#define lt <
#define lt_eq <=

long timestamp()
{
    namespace sc = std::chrono;

    auto time = sc::system_clock::now(); // get the current time

    auto since_epoch = time.time_since_epoch(); // get the duration since epoch

    auto nanoseconds= sc::duration_cast<sc::nanoseconds>(since_epoch);

    return nanoseconds.count();
}

std::string getClientId()
{
	std::ifstream idfile("id");
	std::string line;
	std::getline(idfile, line);
	return line;
}

std::string getProbeId()
{
	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file("botstream.xml");
	std::string probe_id = doc.child("composition").attribute("probe_id").value();
	return probe_id;
}

"""

BotstreamLibFooter = \
"""
#undef gt
#undef gt_eq
#undef lt
#undef lt_eq
"""

# FEATURE FUNCION TEMPLATE
#
# {0} represents class name
# {1} represents arguments of Operator function
# {2} represents function return value
#

GenericFunctionTemplate = \
"""
extern "C" {2} {0}( double* Input )
{{
    return {1};
}}
"""

# THIS WILL BE BY STATUS

EventTemplate = \
"""
event Events[]
{{
    {0},
    {{ NULL, NULL, {{-1, 0}}, -1 }}

}};
"""

StatusTemplate = \
"""
state_tuple Event_{0}[]
{{
        {1}
}};
"""

FeatureListTemplate = \
"""
feature_function {1}FeatureList[] =
{{
        {0},
        NULL
}};
"""

ConditionListTemplate = \
"""
condition_tuple {1}ConditionList[] =
{{
       {0}
       {{ NULL, NULL, -1 }}
}};
"""

ActionListTemplate = \
"""
action_function {1}ActionList{2}[] = {{ {0}, NULL }};
"""


import os
import json
import itertools
import numpy as np
from scipy.linalg import lstsq
from types import SimpleNamespace

from phasefinder import jackknife
from phasefinder.optimization import cvxopt_solve_qp
from phasefinder.datasets import Ising
from phasefinder.utils import build_path

###process magnetization, order parameter curves, and U_4 Binder cumulant curves

def gather_magnetizations(data_dir, results_dir, J, L, N_test=None):
	temperatures = []
	measurements = []
	L_dir = os.path.join(data_dir, J, "L{:d}".format(L))
	for temperature_dir in sorted(os.listdir(L_dir)):
		if temperature_dir[0] != "T":
			continue
		I = Ising()
		Ms = I.magnetization(os.path.join(L_dir, temperature_dir), per_spin=True, staggered=(J=="antiferromagnetic"))[-2::-2]
		temperatures.append(I.T)
		measurements.append(Ms)
	temperatures = np.array(temperatures)
	measurements = np.stack(measurements, 0)
	if N_test is not None:
		measurements = measurements[:,:N_test]
	output_dir = "{}/{}/magnetization/L{:d}".format(results_dir, J, L)
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "measurements.npz"), temperatures=temperatures, measurements=measurements)

def calculate_stats(results_dir, J, observable_name, L, N_test, N=None, fold=None, seed=None, L_test=None, bins=50):
	dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test)
	measurements = np.load(os.path.join(dir, "measurements.npz"))
	temperatures = measurements["temperatures"]
	measurements = measurements["measurements"].T[:N_test]
	order_means, order_stds = jackknife.calculate_mean_std( jackknife.calculate_samples(np.abs(measurements)) )
	u4_means, u4_stds = jackknife.calculate_mean_std( \
		1 - jackknife.calculate_samples(measurements**4)/( 3*jackknife.calculate_samples(measurements**2)**2 ) )
	distribution_range = (measurements.min(), measurements.max())
	distributions = []
	for slice in measurements.T:
		hist, _ = np.histogram(slice, bins=bins, range=distribution_range, density=False)
		hist = hist/len(slice)
		distributions.append(hist)
	distributions = np.stack(distributions, 0)
	distribution_range = np.array(list(distribution_range))
	output_dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "stats.npz"), temperatures=temperatures, distributions=distributions, distribution_range=distribution_range, order_means=order_means, order_stds=order_stds, u4_means=u4_means, u4_stds=u4_stds)

### process critical temperature estimates

def U4_samples(measurements):
	samples_2 = jackknife.calculate_samples(measurements**2)
	samples_4 = jackknife.calculate_samples(measurements**4)
	samples = 1 - samples_4/(3*samples_2**2)
	return samples

def critical_temperature_samples(temperatures, u4samples):
	n_samples, n_temperatures = u4samples.shape
	nums = np.arange(n_temperatures+1)[None,:]
	nums[0,0] = 1
	cumsums = np.concatenate([np.zeros((n_samples, 1)), np.cumsum(u4samples, 1)], 1)
	means_forward = cumsums/nums
	means_backward = (cumsums[:,-1,None]-cumsums)/nums[:,::-1]
	tc_samples = []
	for i in range(n_samples):
		means = np.tril(np.repeat(means_forward[i,:,None], n_temperatures, axis=1), k=-1) \
			+ np.triu(np.repeat(means_backward[i,:,None], n_temperatures, axis=1), k=0)
		j = np.argmin( np.mean((u4samples[None,i,:]-means)**2, 1) )
		tc_samples.append( (temperatures[j]+temperatures[j-1])/2 )
	tc_samples = np.array(tc_samples)
	return tc_samples

def minimize_std(temperatures, samples):
	upper_bound_indices = np.less(temperatures[:,None], samples[None,:]).sum(0)
	upper_bounds = temperatures[upper_bound_indices]
	lower_bounds = temperatures[upper_bound_indices-1]
	n = len(samples)-1
	P = np.eye(n+1)
	P[:,0] = -1
	P[0,:] = -1
	P[0,0] = n
	P = P.astype(np.double)
	q = np.zeros((n+1)).astype(np.double)
	G = np.concatenate([np.eye(n+1), -np.eye(n+1)], 0).astype(np.double)
	h = np.concatenate([upper_bounds, -lower_bounds], 0).astype(np.double)
	new_samples = cvxopt_solve_qp(P, q, G=G, h=h)
	new_samples = new_samples + ( np.min(upper_bounds-new_samples) - np.min(new_samples-lower_bounds) )/2
	return new_samples

def minimize_std_without_bias(temperatures, samples):
	upper_bound_indices = np.less(temperatures[:,None], samples[None,:]).sum(0)
	upper_bounds = temperatures[upper_bound_indices]
	lower_bounds = temperatures[upper_bound_indices-1]
	upper_bounds = upper_bounds[1:]
	lower_bounds = lower_bounds[1:]
	n = len(samples)-1
	P = np.eye(n+1)
	P[:,0] = -1
	P[0,:] = -1
	P[0,0] = n
	P = P.astype(np.double)
	q = np.zeros((n+1)).astype(np.double)
	G = np.concatenate([np.eye(n+1)[1:], -np.eye(n+1)[1:]], 0).astype(np.double)
	h = np.concatenate([upper_bounds, -lower_bounds], 0).astype(np.double)
	A = np.full((1, n+1), -1, dtype=np.double)
	A[0,0] = n
	b = np.zeros((1), dtype=np.double)
	new_samples = cvxopt_solve_qp(P, q, G=G, h=h, A=A, b=b)
	new_samples = new_samples + ( np.min(upper_bounds-new_samples[1:]) - np.min(new_samples[1:]-lower_bounds) )/2
	new_samples[0] = new_samples[1:].mean()
	return new_samples

def lstsq_samples(x, y_samples, weights=None):
	ones = np.ones((x.shape[0], 1))
	if weights is None:
		weights = ones
	if weights.ndim == 1:
		weights = weights[:,None]
	if x.ndim == 1:
		x = x[:,None]
	x = np.concatenate((ones, x), 1)
	fit_samples = lstsq(x*weights, y_samples*weights)[0]
	yhat_samples = x.dot(fit_samples)
	y_samples_centered = y_samples - y_samples.mean(0, keepdims=True)
	yhat_samples_centered = yhat_samples - yhat_samples.mean(0, keepdims=True)
	r2_samples = (y_samples_centered*yhat_samples_centered).sum(0)**2/( np.sum(y_samples_centered**2, 0)*np.sum(yhat_samples_centered**2, 0) )
	return fit_samples, r2_samples

def calculate_critical_temperatures(results_dir, J, observable_name, L, N=None, fold=None, seed=None, L_test=None, optimize_std=False, optimize_reduce_bias=True):
	dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test)
	measurements = np.load(os.path.join(dir, "measurements.npz"))
	temperatures = measurements["temperatures"]
	measurements = measurements["measurements"].T
	samples = critical_temperature_samples(temperatures, U4_samples(measurements))
	if optimize_std:
		if optimize_reduce_bias:
			samples = minimize_std(temperatures, samples)
		else:
			samples = minimize_std_without_bias(temperatures, samples)
	mean, std, mean_bias, std_bias = jackknife.calculate_mean_std(samples, reduce_bias=False, return_bias=True)
	output_dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "tc.npz"), mean=mean, std=std, mean_bias=mean_bias, std_bias=std_bias, samples=samples)

def calculate_critical_temperature_extrapolates(results_dir, J, observable_name, Ls, N=None, fold=None, seed=None, jitter=1e-6):
	load_tc = lambda L: np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "tc.npz"))
	x = 1/np.array(Ls)
	y_samples = np.stack([load_tc(L)["samples"] for L in Ls], 0)
	weights = 1/(np.array([float(load_tc(L)["std"]) for L in Ls]) + jitter)
	fit_samples, r2_samples = lstsq_samples(x, y_samples, weights=weights)
	samples = np.concatenate([fit_samples, r2_samples[None,:]], 0).T
	mean, std, mean_bias, std_bias = jackknife.calculate_mean_std(samples, reduce_bias=False, return_bias=True)
	stats = ["mean", "std", "mean_bias", "std_bias"]
	values = [mean, std, mean_bias, std_bias]
	kwds = ["yintercept", "slope", "r2"]
	output_dict = {stat: value[0] for (stat, value) in zip(stats, values)}
	for ((i, kwd), (stat, value)) in itertools.product(enumerate(kwds), zip(stats, values)):
		output_dict["{}_{}".format(kwd, stat)] = value[i]
	output_dir = build_path(results_dir, J, observable_name, None, N, fold, seed, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "tc.npz"), **output_dict)

### process execution times

def calculate_times(results_dir, J, observable_name, L, N_test, N=None, fold=None, seed=None, L_test=None, n_Ls=1):
	L_dir = os.path.join("data", J, "L{:d}".format(L))
	T_dirs = sorted(os.listdir(L_dir))
	T_dirs = [os.path.join(L_dir, dir) for dir in T_dirs if dir[0] == "T"]
	with open(os.path.join(T_dirs[0], "args.json"), "r") as fp:
		args = json.load(fp)
	N_max = args["nmcs"]//args["nmeas"]
	generation_time = 0
	for dir in T_dirs:
		with open(os.path.join(dir, "time.txt"), "r") as fp:
			generation_time += float(fp.read())
	generation_time_ieq = generation_time*args["ieq"]/(args["ieq"] + args["nmcs"])
	generation_time_mc = generation_time*args["nmcs"]/(args["ieq"] + args["nmcs"])
	if observable_name == "magnetization":
		N = 0
		training_time = 0
		preprocessing_time = 0
	else:
		dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=None)
		with open(os.path.join(dir, "results.json"), "r") as fp:
			results = json.load(fp)
		training_time = results["time"]
		with open(os.path.join(L_dir, "aggregate", "times.json"), "r") as fp:
			key = "states_symmetric" if results["args"]["symmetric"] else "states"
			preprocessing_time = json.load(fp)[key]
	generation_time_mc *= (N_test + N/n_Ls)/N_max
	preprocessing_time *= (N_test + N/n_Ls)/N_max
	output_dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "time.npz"), generation=generation_time_ieq+generation_time_mc, preprocessing=preprocessing_time, training=training_time)

def calculate_time_extrapolates(results_dir, J, observable_name, Ls, N=None, fold=None, seed=None):
	dicts = [dict(np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "time.npz"))) for L in Ls]
	output_dict = {key: sum(D[key] for D in dicts) for key in dicts[0]}
	output_dir = build_path(results_dir, J, observable_name, None, N=N, fold=fold, seed=seed, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "time.npz"), **output_dict)

### calculate functional correlations

def calculate_functional_cors(results_dir, J, observable_name, L, N=None, fold=None, seed=None, L_test=None):
	dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test)
	measurements = np.load(os.path.join(dir, "measurements.npz"))
	temperatures = measurements["temperatures"]
	measurements = measurements["measurements"].T
	magnetization = np.load(os.path.join(build_path(results_dir, J, "magnetization", L), "measurements.npz"))["measurements"].T
	cor = np.sum(measurements*magnetization)/np.sqrt(np.sum(measurements**2)*np.sum(magnetization**2))
	cor = np.maximum(0, 1-cor**2)
	onsager = np.where(temperatures<2/np.log(1+np.sqrt(2)), np.clip(1-1/np.sinh(2/temperatures)**4, 0, None)**(1/8), np.zeros_like(temperatures))
	samples = jackknife.calculate_samples(np.abs(measurements))
	samples = samples.dot(onsager)/np.sqrt(np.sum(samples**2, 1)*np.sum(onsager**2))
	samples = np.maximum(0, 1-samples**2)
	mean, std, mean_bias, std_bias = jackknife.calculate_mean_std(samples, reduce_bias=False, return_bias=True)
	output_dir = build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, L_test=L_test, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "cor.npz"), magnetization=cor, onsager_mean=mean, onsager_std=std, onsager_mean_bias=mean_bias, onsager_std_bias=std_bias, onsager_samples=samples)

def calculate_functional_cor_extrapolates(results_dir, J, observable_name, Ls, N=None, fold=None, seed=None, jitter=1e-6):
	load_tc = lambda L: np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "cor.npz"))
	x = 1/np.array(Ls)
	y_samples = np.stack([load_tc(L)["onsager_samples"] for L in Ls], 0)
	weights = 1/(np.array([float(load_tc(L)["onsager_std"]+load_tc(L)["onsager_std_bias"]) for L in Ls]) + jitter)
	fit_samples, r2_samples = lstsq_samples(x, y_samples, weights=weights)
	samples = np.concatenate([fit_samples, r2_samples[None,:]], 0).T
	mean, std, mean_bias, std_bias = jackknife.calculate_mean_std(samples, reduce_bias=False, return_bias=True)
	stats = ["mean", "std", "mean_bias", "std_bias"]
	values = [mean, std, mean_bias, std_bias]
	kwds = ["yintercept", "slope", "r2"]
	output_dict = {stat: value[0] for (stat, value) in zip(stats, values)}
	for ((i, kwd), (stat, value)) in itertools.product(enumerate(kwds), zip(stats, values)):
		output_dict["{}_{}".format(kwd, stat)] = value[i]
	output_dict = {"onsager_"+key: value for (key, value) in output_dict.items()}
	output_dict["magnetization"] = 0
	output_dir = build_path(results_dir, J, observable_name, None, N, fold, seed, subdir="processed")
	os.makedirs(output_dir, exist_ok=True)
	np.savez(os.path.join(output_dir, "cor.npz"), **output_dict)

### gather critical temperatures, times, and correlations into CSV

def aggregate_tc(results_dir, J, observable_name, L, N=None, folds=None, seeds=None, reduce_bias=False, jackknife_std=True, r2=False):
	stats = ["mean", "std", "mean_bias", "std_bias"]
	key = SimpleNamespace(**{stat: stat for stat in stats})
	if r2:
		for stat in stats:
			setattr(key, stat, "r2_"+stat)
	if observable_name == "magnetization":
		with np.load(os.path.join(build_path(results_dir, J, observable_name, L, subdir="processed"), "tc.npz")) as D:
			mean, std = D[key.mean]+D[key.mean_bias]*int(reduce_bias), np.sqrt(D[key.std]**2+D[key.std_bias]**2*int(reduce_bias))*int(jackknife_std)
		return mean, std
	else:
		means, stds = [], []
		for (fold, seed) in itertools.product(folds, seeds):
			with np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "tc.npz")) as D:
				means.append(D[key.mean]+D[key.mean_bias]*int(reduce_bias))
				stds.append(np.sqrt(D[key.std]**2+D[key.std_bias]**2*int(reduce_bias))*int(jackknife_std))
		means = np.array(means)
		stds = np.array(stds)
		mean = means.mean()
		std = np.sqrt( (stds**2).mean() + means.std()**2 )
		return mean, std

def aggregate_time(results_dir, J, observable_name, L, N=None, folds=None, seeds=None, key="training", reduce=np.mean):
	if observable_name == "magnetization":
		with np.load(os.path.join(build_path(results_dir, J, observable_name, L, subdir="processed"), "time.npz")) as D:
			t = D[key]
		return t
	else:
		ts = []
		for (fold, seed) in itertools.product(folds, seeds):
			with np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "time.npz")) as D:
				ts.append(D[key])
		ts = np.array(ts)
		t = reduce(ts)
		return t

def aggregate_cor(results_dir, J, observable_name, L, N=None, folds=None, seeds=None, key="magnetization", func=np.abs, reduce_bias=False, jackknife_std=True):
	assert key in ["magnetization", "onsager"], "Keyword argument key must be either magnetization or onsager; got {} instead.".format(reduce)
	if observable_name == "magnetization":
		with np.load(os.path.join(build_path(results_dir, J, observable_name, L, subdir="processed"), "cor.npz")) as D:
			mean = func(D["magnetization"]) if key=="magnetization" else D["onsager_mean"]+D["onsager_mean_bias"]*int(reduce_bias)
			std = 0 if key=="magnetization" else np.sqrt(D["onsager_std"]**2+D["onsager_std_bias"]**2*int(reduce_bias))*int(jackknife_std)
		return mean, std
	else:
		means, stds = [], []
		for (fold, seed) in itertools.product(folds, seeds):
			with np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), "cor.npz")) as D:
				mean = func(D["magnetization"]) if key=="magnetization" else D["onsager_mean"]+D["onsager_mean_bias"]*int(reduce_bias)
				std = 0 if key=="magnetization" else np.sqrt(D["onsager_std"]**2+D["onsager_std_bias"]**2*int(reduce_bias))*int(jackknife_std)
				means.append(mean)
				stds.append(std)
		means = np.array(means)
		stds = np.array(stds)
		mean = means.mean()
		std = np.sqrt( (stds**2).mean() + means.std()**2 )
		return mean, std

def aggregate_slope_yintercept(results_dir, J, observable_name, L, N=None, folds=None, seeds=None, reduce_bias=False, prefix="tc"):
	assert prefix in ["tc", "onsager"], "Keyword prefix must be either tc or onsager."
	if L is not None:
		return 0, 0
	kwds = {"slope": "slope_mean", "slope_bias": "slope_mean_bias", "yintercept": "yintercept_mean", "yintercept_bias": "yintercept_mean_bias"}
	if prefix == "onsager":
		kwds = {key: prefix+"_"+value for (key, value) in kwds.items()}
	key = SimpleNamespace(**kwds)
	filename = "tc.npz" if prefix == "tc" else "cor.npz"
	if observable_name == "magnetization":
		with np.load(os.path.join(build_path(results_dir, J, observable_name, L, subdir="processed"), filename)) as D:
			slope, yintercept = D[key.slope]+D[key.slope_bias]*int(reduce_bias), D[key.yintercept]+D[key.yintercept_bias]*int(reduce_bias)
		return slope, yintercept
	else:
		slopes, yintercepts = [], []
		for (fold, seed) in itertools.product(folds, seeds):
			with np.load(os.path.join(build_path(results_dir, J, observable_name, L, N=N, fold=fold, seed=seed, subdir="processed"), filename)) as D:
				slopes.append(D[key.slope]+D[key.slope_bias]*int(reduce_bias))
				yintercepts.append(D[key.yintercept]+D[key.yintercept_bias]*int(reduce_bias))
		slope = np.array(slopes).mean()
		yintercept = np.array(yintercepts).mean()
		return slope, yintercept

def gather(results_dir, Js, observable_names, Ls, Ns, folds, seeds, tc_reduce_bias=False, cor_reduce_bias=True, jackknife_std=True):
	tc = 2/np.log(1+np.sqrt(2))
	output_dir = 	os.path.join(results_dir, "processed")
	os.makedirs(output_dir, exist_ok=True)
	with open(os.path.join(output_dir, "gathered.csv"), "w") as fp:
		fp.write("J,L,observable,N,tc_mean,tc_std,tc_slope,tc_yintercept,generation_time,preprocessing_time,training_time,cor_magnetization_mean,cor_magnetization_std,cor_magnetization_sign,cor_onsager_mean,cor_onsager_std,cor_onsager_slope,cor_onsager_yintercept\n")
		for (J, L, observable_name, N) in itertools.product(Js, Ls, observable_names, Ns):
			tc_mean, tc_std = aggregate_tc(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, reduce_bias=tc_reduce_bias, jackknife_std=jackknife_std)
			tc_slope, tc_yintercept = aggregate_slope_yintercept(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, reduce_bias=tc_reduce_bias, prefix="tc")
			tc_mean = 100*(tc_mean/tc - 1)
			tc_std = 100*tc_std/tc
			tc_slope = 100*tc_slope/tc
			tc_yintercept = 100*(tc_yintercept/tc - 1)
			generation_time = aggregate_time(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="generation", reduce=np.mean)/60
			preprocessing_time = aggregate_time(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="preprocessing", reduce=np.mean)/60
			training_time = aggregate_time(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="training", reduce=np.sum)/60
			cor_magnetization_mean, cor_magnetization_std = aggregate_cor(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="magnetization", reduce_bias=cor_reduce_bias, jackknife_std=jackknife_std)
			cor_onsager_mean, cor_onsager_std = aggregate_cor(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="onsager", reduce_bias=cor_reduce_bias, jackknife_std=jackknife_std)
			cor_onsager_slope, cor_onsager_yintercept = aggregate_slope_yintercept(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, reduce_bias=cor_reduce_bias, prefix="onsager")
			cor_magnetization_sign, _ = aggregate_cor(results_dir, J, observable_name, L, N=N, folds=folds, seeds=seeds, key="magnetization", func=np.sign, reduce_bias=cor_reduce_bias, jackknife_std=jackknife_std)
			L_str = "{:d}".format(L) if L is not None else "inf"
			fp.write("{},{},{},{:d},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f}\n".format(J, L, observable_name, N, tc_mean, tc_std, tc_slope, tc_yintercept, generation_time, preprocessing_time, training_time, 100*cor_magnetization_mean, 100*cor_magnetization_std, 100*cor_magnetization_sign, 100*cor_onsager_mean, 100*cor_onsager_std, 100*cor_onsager_slope, 100*cor_onsager_yintercept))

### process symmetry generators

def calculate_generators(results_dir, Js, encoder_names, Ls, Ns, folds, seeds):
	generator_types = ["spatial", "internal"]
	output_dict = {J: {name: {gen_type: {} for gen_type in generator_types} for name in encoder_names} for J in Js}
	for (J, name, gen_type) in itertools.product(Js, encoder_names, generator_types):
		gens = []
		for (L, N, fold, seed) in itertools.product(Ls, Ns, folds, seeds):
			with open(os.path.join(build_path(results_dir, J, name, L, N=N, fold=fold, seed=seed), "generator_reps.json"), "r") as fp:
				gens.append( json.load(fp)[gen_type] )
		gens = np.array(gens)
		output_dict[J][name][gen_type]["mean"] = float(gens.mean())
		output_dict[J][name][gen_type]["std"] = float(gens.std())
	output_dir = os.path.join(results_dir, "processed")
	os.makedirs(output_dir, exist_ok=True)
	with open(os.path.join(output_dir, "generators.json"), "w") as fp:
		json.dump(output_dict, fp, indent=2)

if __name__ == "__main__":
	results_dir = "results"
	Js = ["ferromagnetic", "antiferromagnetic"]
	Ls = [16, 32, 64, 128]
	Ns = [8, 16, 32, 64, 128, 256]
	N_test = 2048

	observable_names = ["magnetization", "latent", "latent_equivariant"]
	encoder_names = ["latent", "latent_equivariant", "latent_multiscale_4"]
	n_Lss = [1, 1, 4]

	folds = list(range(8))
	seeds = list(range(3))

	print("Gathering magnetizations . . . ")
	for (J, L) in itertools.product(Js, Ls):
		gather_magnetizations("data", results_dir, J, L, N_test=N_test)

	print("Calculating stats . . . ")
	for (J, name) in itertools.product(Js, observable_names):
		calculate_stats(results_dir, J, name, Ls[-1], N_test, N=Ns[-1], fold=0, seed=0)

	print("Calculating critical temperatures . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		for L in Ls:
			calculate_critical_temperatures(results_dir, J, "magnetization", L, optimize_std=True, optimize_reduce_bias=True)
		for encoder_name in encoder_names:
			print(encoder_name)
			for (L, N, fold, seed) in itertools.product(Ls, Ns, folds, seeds):
				calculate_critical_temperatures(results_dir, J, encoder_name, L, N=N, fold=fold, seed=seed, optimize_std=True, optimize_reduce_bias=True)

	print("Calculating critical temperature extrapolates . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		calculate_critical_temperature_extrapolates(results_dir, J, "magnetization", Ls)
		for encoder_name in encoder_names:
			print(encoder_name)
			for (N, fold, seed) in itertools.product(Ns, folds, seeds):
				calculate_critical_temperature_extrapolates(results_dir, J, encoder_name, Ls, N=N, fold=fold, seed=seed)

	print("Calculating execution times . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		for L in Ls:
			calculate_times(results_dir, J, "magnetization", L, N_test)
		for (encoder_name, n_Ls) in zip(encoder_names, n_Lss):
			print(encoder_name)
			for (L, N, fold, seed) in itertools.product(Ls, Ns, folds, seeds):
				calculate_times(results_dir, J, encoder_name, L, N_test, N=N, fold=fold, seed=seed, n_Ls=n_Ls)

	print("Calculating execution time extrapolates . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		for L in Ls:
			calculate_time_extrapolates(results_dir, J, "magnetization", Ls)
		for encoder_name in encoder_names:
			print(encoder_name)
			for (N, fold, seed) in itertools.product(Ns, folds, seeds):
				calculate_time_extrapolates(results_dir, J, encoder_name, Ls, N=N, fold=fold, seed=seed)

	print("Calculating functional correlations . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		for L in Ls:
			calculate_functional_cors(results_dir, J, "magnetization", L)
		for encoder_name in encoder_names:
			print(encoder_name)
			for (L, N, fold, seed) in itertools.product(Ls, Ns, folds, seeds):
				calculate_functional_cors(results_dir, J, encoder_name, L, N=N, fold=fold, seed=seed)

	print("Calculating functional correlation extrapolates . . . ")
	for J in Js:
		print("J:", J)
		print("magnetization")
		calculate_functional_cor_extrapolates(results_dir, J, "magnetization", Ls)
		for encoder_name in encoder_names:
			print(encoder_name)
			for (N, fold, seed) in itertools.product(Ns, folds, seeds):
				calculate_functional_cor_extrapolates(results_dir, J, encoder_name, Ls, N=N, fold=fold, seed=seed)

	print("Gathering results into CSV . . . ")
	gather(results_dir, Js, ["magnetization"]+encoder_names, Ls+[None], Ns, folds, seeds, tc_reduce_bias=False, cor_reduce_bias=True, jackknife_std=True)

	print("Calculating symmetry generators . . . ")
	calculate_generators(results_dir, Js, ["latent_equivariant"], Ls, Ns, folds, seeds)

	print("Done!")


# madLibs.py - program that reads text file and let user add their own text anywhere the word
# ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file.
from pathlib import Path
import time
import re
import pyinputplus as pyip
import time

# This creates a path to folder separated for this project
filesFolder = (Path.cwd() / 'chapter_09\\madLibsTexts')

# Regexes created to found given word
# and to found if currently checked word ends with a . , or some other sign.
libs_regex = re.compile(r'^ADJECTIVE|^NOUN|^ADVERB|^VERB')
letter_regex = re.compile(r'\W')

template_file = open(f'{str(filesFolder)}\\basic_text.txt', 'r')
template_file_content = template_file.read()
list_of_separated_words = template_file_content.split()

for index, word in enumerate(list_of_separated_words):
    matching_word = libs_regex.search(word)

    # If given word is found, it prompts user to type in word the user sees as correct.
    if matching_word != None:
        user_input = pyip.inputStr(prompt='Enter ' + matching_word.group().lower() + ':\n')

        # Checks if a word ends with a . or , or some other sign.
        # If yes, then puts it at the end of the new word.
        if letter_regex.search(word[-1]):
            user_input = user_input + word[-1]

        list_of_separated_words[index] = user_input

new_string = ' '.join(list_of_separated_words)

# timestamp generated for file name.
timestamp_note = int(time.time())

# New file is created. To differ between new files, timestamp is added to its name.
end_file = open(f'{str(filesFolder)}\\filled_text{timestamp_note}.txt', 'w')
end_file.write(new_string)

template_file.close()
end_file.close()

# find each occurence of said words - regex? ^ADJECTIVE | ^N
# for each occurence, show input prompt so it can be chanded
# print the whole thing
# save it to new text file


# -*- coding: utf-8 -*-

# $Id$

from __future__ import unicode_literals
from builtins import object
from future.utils import PY2

import os
import re
import shutil
import tempfile
import unittest

import pyinotify

from arv.autotest.config import watch_node_validator
from arv.autotest.event_filters import and_
from arv.autotest.event_filters import is_delete_dir_event
from arv.autotest.event_filters import not_
from arv.autotest.event_filters import simple_event_filter_factory
from arv.autotest.event_filters import throttler_factory

class Object(object):
    def __init__(self, **kw):
        self.__dict__.update(kw)

def make_event(path, name, mask=0):
    return Object(path=path, name=name, mask=mask)

def make_watch(path, recurse=False, auto_add=False, include=[], exclude=[]):
    return watch_node_validator({
        "path": path,
        "recurse": recurse,
        "auto_add": auto_add,
        "include": include,
        "exclude": exclude
    })

def make_fake_timer(start, deltas):
    def generator():
        total = start
        yield total
        for i in deltas:
            total += i
            yield total
    if PY2:
        return generator().next
    else:
        return generator().__next__

class TestIsDirEvent(unittest.TestCase):

    def test_exclude_directory_delete(self):
        event = make_event("", "", pyinotify.IN_DELETE | pyinotify.IN_ISDIR)
        self.assert_(is_delete_dir_event(event))

    def test_exclude_self_delete(self):
        event = make_event("", "", pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF)
        self.assert_(is_delete_dir_event(event))

    def test_include_directory_create(self):
        event = make_event("", "", pyinotify.IN_CREATE | pyinotify.IN_ISDIR)
        self.failIf(is_delete_dir_event(event))

class TestSimpleEventFilterFactory(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def make_subdir(self, name):
        name = os.path.join(self.tmpdir, name)
        os.mkdir(name)
        return name

    def test_exclude_excluded_file(self):
        event = make_event(self.tmpdir, "exclude.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, exclude=[u".*\\.abc"])
        ])
        self.failIf(filter(event))

    def test_include_included_file(self):
        event = make_event(self.tmpdir, "include.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, include=[u".*\\.abc"])
        ])
        self.assert_(filter(event))

    def test_exclude_excluced_file_in_subdirectory(self):
        subdir = self.make_subdir("subdir")
        event = make_event(subdir, "exclude.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, exclude=[u".*\\.abc"])
        ])
        self.failIf(filter(event))

    def test_include_included_file_in_subdirectory(self):
        subdir = self.make_subdir("subdir")
        event = make_event(subdir, "include.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, include=[u".*\\.abc"])
        ])
        self.assert_(filter(event))

    def test_exclude_specific_takes_over_generic(self):
        subdir = self.make_subdir("subdir")
        event = make_event(subdir, "exclude.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, include=[u".*\\.abc"]),  # /tmp
            make_watch(subdir, exclude=[u".*\\.abc"]),       # /tmp/subdir
        ])
        self.failIf(filter(event))

    def test_include_specific_takes_over_generic(self):
        subdir = self.make_subdir("subdir")
        event = make_event(subdir, "exclude.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, exclude=[u".*\\.abc"]),  # /tmp
            make_watch(subdir, include=[u".*\\.abc"]),       # /tmp/subdir
        ])
        self.assert_(filter(event))

    def test_global_exclude_takes_over_all(self):
        event = make_event(self.tmpdir, ".exclude.abc")
        filter = simple_event_filter_factory(
            watches=[
                make_watch(self.tmpdir, include=[u".*\\.abc"]),
            ],
            global_ignores=[re.compile("\\..*")]
        )
        self.failIf(filter(event))

    def test_neither_excluded_nor_included_gets_excluded_by_default(self):
        event = make_event(self.tmpdir, "exclude.txt")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, exclude=[u".*\\.abc"], include=[u".*\\.py"])
        ])
        self.failIf(filter(event))

    def test_unwatched_directory_raises_ValueError(self):
        parent = os.path.dirname(self.tmpdir)
        event = make_event(parent, "exclude.abc")
        filter = simple_event_filter_factory([
            make_watch(self.tmpdir, include=[u".*\\.abc"])
        ])
        self.assertRaises(ValueError, filter, event)

class TestFilterCombinators(unittest.TestCase):

    def setUp(self):
        def is_odd(value):
            return bool(value % 2)

        def is_big(value):
            return value > 100

        self.is_odd = is_odd
        self.is_big = is_big

    def test_preconditions(self):
        self.assert_(self.is_odd(3))
        self.failIf(self.is_odd(2))
        self.assert_(self.is_big(1000))
        self.failIf(self.is_big(2))

    def test_negation(self):
        f = not_(self.is_odd)
        self.assert_(f(2))
        self.failIf(f(3))

    def test_and(self):
        f = and_(self.is_odd, self.is_big)
        self.assert_(f(1003))
        self.failIf(f(1002))
        self.failIf(f(3))
        self.failIf(f(2))

class TestThrottlingFilter(unittest.TestCase):

    def set_up(self, deltas, start=0, count=10):
        self.cfg = Object(max_events_second=count)  # 10 events/second
        self.timer = make_fake_timer(start, deltas)
        self.throttler = throttler_factory(self.cfg, self.timer)
        self.event = make_event("/tmp", "foo")

    def test_fake_timer(self):
        timer = make_fake_timer(5, [0.1, 0, 0.5])
        self.assertEqual(timer(), 5)
        self.assertEqual(timer(), 5.1)
        self.assertEqual(timer(), 5.1)
        self.assertEqual(timer(), 5.6)
        self.assertRaises(StopIteration, timer)

    def test_first_event_is_accepted(self):
        self.set_up([])
        self.assert_(self.throttler(self.event))

    def test_event_accepted_if_delta_elapsed(self):
        self.set_up([0.1])
        self.throttler(self.event)
        self.assert_(self.throttler(self.event))

    def test_event_accepted_if_delta_elapsed_cummulative(self):
        self.set_up([0.05, 0.06])
        self.throttler(self.event)
        self.failIf(self.throttler(self.event))
        self.assert_(self.throttler(self.event))

    def test_event_rejected_if_delta_not_elapsed(self):
        self.set_up([0.01])
        self.throttler(self.event)
        self.failIf(self.throttler(self.event))

    def test_None_cfg_disables_throttling(self):
        throttler = throttler_factory(None, make_fake_timer(0, [0, 0]))
        event = make_event("/tmp", "foo")
        self.assert_(throttler(event))  # always accepted
        self.assert_(throttler(event))  # two deltas
        self.assert_(throttler(event))
        self.assert_(throttler(event))  # timer isn't called

    def test_adjust_delta(self):
        self.set_up([0.2, 0.5])
        self.throttler(self.event)
        self.assert_(self.throttler(self.event))
        self.throttler.adjust_delta(0.7)
        self.failIf(self.throttler(self.event))


# Data structure to store a binary tree node
class Node:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

# Recursive function to find the minimum depth of a path starting
# from the given node in a binary tree
def findMinDepth(root):

    # base case
    if root is None:
        return 0

    # find the minimum depth of the left subtree
    l = findMinDepth(root.left)

    # find the minimum depth of the right subtree
    r = findMinDepth(root.right)

    # if the left child does not exist, consider the depth
    # returned by the right subtree
    if root.left is None:
        return 1 + r

    # if the right child does not exist, consider the depth
    # returned by the left subtree
    if root.right is None:
        return 1 + l

    # otherwise, choose the minimum depth returned by the
    # left and right subtree
    return min(l, r) + 1

if __name__ == '__main__':

    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.left = Node(6)
    root.right.right = Node(7)
    root.left.left.right = Node(8)
    root.left.right.right = Node(9)
    root.right.right.left = Node(10)
    root.right.right.left = Node(11)
    root.left.left.right.right = Node(12)

    print('The minimum depth is', findMinDepth(root))

Find the minimum depth of a binary tree/Python.py


import importlib
import inspect
import os
import sys

def load(clazz, directories=[]):
    modules = []
    for directory in directories:
        abspath = os.path.abspath(directory)
        sys.path.append(abspath)
        for candidate in os.listdir(directory):
            if candidate == '__init__.py' or candidate[-3:] != '.py':
                continue
            path_modules = importlib.import_module(candidate[:-3])

            for name, obj in inspect.getmembers(path_modules):
                if inspect.isclass(obj) and obj:
                    try:
                        instance = getattr(path_modules, name)()
                        for super_class in instance.__class__.__bases__:
                            if super_class.__name__ == clazz.__name__:
                                modules.append(instance)
                                continue
                    except Exception as e:
                        continue

    return modules


'tests for decorators'
from unittest import TestCase

from aaron.composition import Composition
from aaron.decorators import composable

def add_one(n):
    return n + 1

class ComposableTests(TestCase):
    'tests for composable decorator'

    def test_isinstance(self):
        self.assertFalse(isinstance(add_one, Composition))
        self.assertTrue(isinstance(composable(add_one), Composition))


import os
from iconsdk.builder.transaction_builder import CallTransactionBuilder, TransactionBuilder
from iconsdk.exception import JSONRPCException
from iconsdk.icon_service import IconService
from iconsdk.providers.http_provider import HTTPProvider
from iconsdk.signed_transaction import SignedTransaction
from iconsdk.wallet.wallet import KeyWallet
from tbears.libs.icon_integrate_test import SCORE_INSTALL_ADDRESS, IconIntegrateTestBase
from iconsdk.builder.call_builder import CallBuilder

T_BEARS_URL = os.environ.get("T_BEARS_URL") or "http://127.0.0.1:9000"

print(f"T-Bears URL {T_BEARS_URL}")

class RegisterPReps(IconIntegrateTestBase):

    def setUp(self):
        super().setUp(network_only=True)
        self.icon_service = IconService(HTTPProvider(T_BEARS_URL, 3))
        self.nid = 3
        self.tx_result_wait = 5
        self._register_preps()

    def _register_preps(self):
        has_enough_preps = False
        self.count = 0
        preps_count = 0
        try:
            params = {"startRanking": 1, "endRanking": 100}
            params = {} if params is None else params
            call = CallBuilder(
                to=SCORE_INSTALL_ADDRESS,
                method="getPReps",
                params=params
            ).build()
            tx_result = self.process_call(call, self.icon_service)
            if "preps" in tx_result:
                preps_count = len(tx_result['preps'])
                has_enough_preps = preps_count >= 100
            else:
                print("not enough P-Reps")
        except JSONRPCException:
            has_enough_preps = False
        if has_enough_preps is False:
            print("registering P-Reps...")
            self._register_100_preps(20, 50)
            self._register_100_preps(50, 80)
            self._register_100_preps(80, 110)
            self._register_100_preps(110, 140)
            print("P-Reps registered.")
        else:
            print(f"t-bears has {preps_count} P-Reps")

    def test_init(self):
        self.assertEqual(1, 1, "P-Reps configuration successful")

    def _register_100_preps(self, start, end):
        txs = []
        send_icx_txs = []
        print(f"registering {end - start} P-Reps")
        for i in range(start, end):
            node_address = "hx9eec61296a7010c867ce24c20e69588e283212" + hex(i)[2:]
            params = {
                "name": f'Test P-rep{str(i)}',
                "country": "KOR",
                "city": "Unknown",
                "email": f'node{node_address}@example.com',
                "website": f'https://node{node_address}.example.com',
                "details": f'https://node{node_address}.example.com/details',
                "p2pEndpoint": f'node{node_address}.example.com:7100',
                "nodeAddress": node_address
            }
            wallet = self._wallet_array[i]
            send_icx_txs.append(self.build_send_icx(self._test1, wallet.get_address(), 3000000000000000000000))
            txs.append(self.build_tx(wallet, SCORE_INSTALL_ADDRESS,
                                     2000000000000000000000, "registerPRep", params))
        self.process_transaction_bulk(send_icx_txs, self.icon_service, 10)
        self.process_transaction_bulk(txs, self.icon_service, 10)
        self.count += end - start
        print(f"{self.count} P-Reps registered")

    def build_tx(self, from_: KeyWallet, to: str, value: int = 0, method: str = None, params: dict = None) \
            -> SignedTransaction:
        params = {} if params is None else params
        tx = CallTransactionBuilder(
            from_=from_.get_address(),
            to=to,
            value=value,
            step_limit=3_000_000_000,
            nid=self.nid,
            nonce=5,
            method=method,
            params=params
        ).build()
        signed_transaction = SignedTransaction(tx, from_)
        return signed_transaction

    def build_send_icx(self, from_: KeyWallet, to: str, value: int,
                       step_limit: int = 1000000, nonce: int = 3) -> SignedTransaction:
        send_icx_transaction = TransactionBuilder(
            from_=from_.get_address(),
            to=to,
            value=value,
            step_limit=step_limit,
            nid=self.nid,
            nonce=nonce
        ).build()
        signed_icx_transaction = SignedTransaction(send_icx_transaction, from_)
        return signed_icx_transaction


def counting_sort(unsorted: list):
    size = len(unsorted)
    sorted_list = [0]*size
    _max = max(unsorted, key=lambda item:item[0])[0]

    # get counts of each integer in unsorted
    count = [0]*(_max+1)
    for i in range(size):
        count[unsorted[i][0]] += 1
        # print(count)

    # get cumulative count
    for i in range(1, _max+1):
        count[i] += count[i-1]
        # print(count)

    # create sorted list
    i = size - 1
    while i >= 0:
        sorted_list[count[unsorted[i][0]] - 1] = unsorted[i]
        count[unsorted[i][0]] -= 1
        # print(sorted_list)
        # print(count)
        i -= 1

    return sorted_list

# unsorted1 = [5, 2, 1, 7, 3, 3, 5, 2, 7, 4, 11]
# unsorted2 = [4, 1, 2, 2, 3, 9, 8, 9, 2]
unsorted3 = [(3,'a'), (1,'a'), (1,'b'), (2,'a'), (2,'b')]
# print(counting_sort(unsorted1))
# print(counting_sort(unsorted2))
print(counting_sort(unsorted3))


from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import SelectKBest, mutual_info_classif
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
import pandas as pd
from sklearn.metrics import classification_report

unclean_df = pd.read_csv(r'C:\Users\User\Desktop\Applied Research Project\HeartDiseasePrediction (8) (1)\HeartDiseasePrediction\HeartDiseasePrediction\HeartDiseasePrediction\heart.csv')

unclean_df = unclean_df.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)
unclean_df['age'] = unclean_df['age'].apply(lambda x: round(x))

unclean_df['age'] = unclean_df['age'].apply(lambda x: round(x))

unclean_df.to_csv('cleaned_data2.csv')
df = unclean_df
original_data = df
X = df.drop(columns=['target'])
y = df['target']
SelectKBest_features = SelectKBest(mutual_info_classif, ).fit(X, y)

SelectKBest_features.get_support(indices=True)
X_method3 = X.iloc[:, SelectKBest_features.get_support(indices=True)]
X_method3

X_train3, X_test3, y_train3, y_test3 = train_test_split(X_method3, y, test_size=0.3, random_state=42)
logreg3 = LogisticRegression(max_iter=5000)
logreg3.fit(X_train3, y_train3)
y_pred_3 = logreg3.predict(X_test3)
print('Accuracy of logistic regression classifier on test set: {:.6f}'.format(logreg3.score(X_test3, y_test3)))
new_features = X_method3
new_features
ss = StandardScaler()

zscore = ss.fit_transform(new_features)

X_scaled = pd.DataFrame(zscore, index=new_features.index, columns=new_features.columns)
X_scaled = X_scaled.reset_index(drop=True)
X_scaled

X_train1, X_test1, y_train1, y_test1 = train_test_split(X_scaled, y, test_size=0.25, random_state=0)

X_train = X_train1
y_train = y_train1
X_test = X_test1
y_test = y_test1
AdaBoost_Classifier = AdaBoostClassifier(random_state=0).fit(X_train1, y_train1)
ada_scores = cross_val_score(AdaBoost_Classifier, X_train, y_train, cv=3)
ada_scores_max = ada_scores.max()
ada_scores_max

AdaBoost_Classifier_pred = AdaBoost_Classifier.predict(X_test1)

AdaBoost_Classifier_acc = accuracy_score(y_test1, AdaBoost_Classifier_pred)
AdaBoost_Classifier_acc
AdaBoost_Classifier_matrix = confusion_matrix(y_test1, AdaBoost_Classifier_pred)
AdaBoost_Classifier_matrix

print(classification_report(y_test1, AdaBoost_Classifier_pred))


class Payout:

    def __init__(self, product_id, game, currency, amount, transaction_date, count):
        self.ProductID = product_id
        self.Currency = currency
        self.Amount = amount
        self.TransactionDate = transaction_date
        self.Count = count
        self.Game = game

class PayoutFact:

    def __init__(self, player, payout):
        self.player_id = player.PlayerID
        self.currency = payout.Currency
        self.amount = payout.Amount
        self.product_id = payout.ProductID
        self.activity_time = payout.TransactionDate
        self.payout_count = payout.Count
        self.game = payout.Game

    def to_csv(self):
        csv = [self.player_id, self.currency, self.amount, self.product_id, self.activity_time,
               self.payout_count]

        if self.product_id == 1:
            csv += [self.game["GameIdentifier"], self.game["GameType"]]
        elif self.product_id == 2:
            csv += [self.game["Sport"], self.game["League"], self.game["Event"], self.game["EventDate"], self.game["Live"]]
        elif self.product_id == 3:
            csv += [self.game["Event"], self.game["EventCategory"], self.game["EventDate"]]
        elif self.product_id == 4:
            csv += [self.game["Game"], self.game["League"], self.game["Event"], self.game["EventCategory"], self.game["EventDate"]]
        elif self.product_id == 5:
            csv += [self.game["Name"], self.game["Category"], self.game["DrawDate"]]
        elif self.product_id == 6:
            csv += [self.game["Event"], self.game["Breed"], self.game["EventDate"]]
        return csv


import streamlit as st
from namedivider import BasicNameDivider, GBDTNameDivider
from pathlib import Path
Path("~/.cache").expanduser().mkdir(exist_ok=True)

@st.experimental_singleton
def get_basic_name_divider():
    basic_divider = BasicNameDivider()
    return basic_divider

@st.experimental_singleton
def get_gbdt_name_divider():
    gbdt_divider = GBDTNameDivider()
    return gbdt_divider

def main():
    col1, col2, col3, col4, col5 = st.columns(5)
    with col1:
        option = st.selectbox(
            'Language',
            ("Japanese", 'English'))

    basic_divider = get_basic_name_divider()
    gbdt_divider = get_gbdt_name_divider()

    if option == "Japanese":
        st.title("NameDivider Demo")
        st.write("NameDividerは、姓名が連結している日本語の名前を姓と名に分割するツールです。")
        st.write("https://github.com/rskmoi/namedivider-python")
        st.write("左側のテキストエリアに名前を入力すると、右側に分割された名前が出力されます。")
        mode_select = "分割手法を選択してください"
        mode_basic = "BasicNameDivider(速いけれど、精度が99.2%)"
        mode_gbdt = "GBDTNameDivider(遅いけれど、精度が99.9%)"
        input_label = "入力"
        output_label = "結果"
        too_many_error = "一度に分割できる名前の数は500人までです"
        info = 'このページはNameDividerの使用感をお伝えするためのデモページです。\n ' \
            '1. このページの使用により生じる不利益について作者は一切責任を負わないものとします。 \n ' \
            '2. このページは任意のタイミングで非公開になる可能性が有ります。 \n ' \
            '3. 入力された名前は保存をしておらず、作者が確認することはできません。'
    elif option == "English":
        st.title("NameDivider Demo")
        st.write("NameDivider is a tool for dividing the Japanese full name into a family name and a given name.")
        st.write("https://github.com/rskmoi/namedivider-python")
        st.write("Entering names in the text area on the left side will output the divided names on the right side.")
        mode_select = "Please select a division method."
        mode_basic = "BasicNameDivider(Fast, but accuracy is 99.2%)"
        mode_gbdt = "GBDTNameDivider(Slow, but accuracy is 99.9%)"
        input_label = "Input undivided names."
        output_label = "Divided names are shown here."
        too_many_error = "The number of names that can be divided at one time is limited to 500"
        info = 'This page is a demo page to show how NameDivider works.\n ' \
            '1. The author assumes no responsibility for any disadvantages that may result from the use of this page. \n ' \
            '2. This page may become private at any time. \n ' \
            '3. The name entered is not saved and cannot be confirmed by the author.'

    else:
        raise ValueError

    mode = st.radio(
        mode_select,
        (mode_basic, mode_gbdt))

    input_area, output_area = st.columns(2)
    undivided_names = input_area.text_area(input_label,
                                           height=500,
                                           placeholder="菅義偉\n岸田文雄\n鳩山由紀夫")
    divided_names = []
    undivided_names_list = undivided_names.split("\n")
    if len(undivided_names_list) > 500:
        st.error(too_many_error)
    for _name in undivided_names_list:
        if len(_name) < 2:
            continue
        if mode == mode_basic:
            _divided_name = str(basic_divider.divide_name(_name))
        elif mode == mode_gbdt:
            _divided_name = str(gbdt_divider.divide_name(_name))
        else:
            raise ValueError
        divided_names.append(_divided_name)
    output_area.text_area(label=output_label,
                          value="\n".join(divided_names),
                          height=500,
                          placeholder="菅 義偉\n岸田 文雄\n鳩山 由紀夫")

    st.info(info)

if __name__ == '__main__':
    main()


from setuptools import find_packages, setup

install_requires = [
    "tqdm>=4.15.0,<5",
    "wagtail>=2.15,<4.2",
]

testing_extras = [
    "coverage>=3.7.0",
    "mock>=1.0.0",
]

setup(
    name="wagtail-inventory",
    url="https://github.com/cfpb/wagtail-inventory",
    author="CFPB",
    author_email="tech@cfpb.gov",
    description="Lookup Wagtail pages by block content",
    long_description=open("README.rst", "r", encoding="utf-8").read(),
    license="CCO",
    version="1.6",
    include_package_data=True,
    packages=find_packages(),
    python_requires=">=3.8",
    install_requires=install_requires,
    extras_require={"testing": testing_extras},
    classifiers=[
        "Framework :: Django",
        "Framework :: Django :: 3.2",
        "Framework :: Django :: 4.0",
        "Framework :: Wagtail",
        "Framework :: Wagtail :: 2",
        "Framework :: Wagtail :: 3",
        "Framework :: Wagtail :: 4",
        "License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication",
        "License :: Public Domain",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
    ],
)


import testtools

# Third Party Imports

# Cloudify Imports is imported and used in operations
from cloudify.exceptions import NonRecoverableError
from docker_plugin import docker_client

class TestClient(testtools.TestCase):

    def test_bad_daemon_dictionary(self):
        daemon_client = {
            'base_url': 'ZZZZZZXXXXXX'
        }
        ex = self.assertRaises(
            NonRecoverableError, docker_client.get_client, daemon_client)
        self.assertIn('Error while getting client', ex.message)


from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from playsound import playsound
import time
import pyttsx3
import sys
import os
import ctypes
import winsound

# Returns true if file path exists
def is_valid_path(path):
    return os.path.exists(path)

# Check file path and exit with message if invalid
def invalid_path_exit(path):
    if not is_valid_path(path):
        print(f"'{path}' is invalid, ending script")
        sys.exit()

# Function for generating text-to-speech
def text_to_speech(text):
    # Initialize the text-to-speech engine
    engine = pyttsx3.init()

    # Convert text to speech
    engine.say(text)
    engine.runAndWait()

# Function for generating a windows popup
def show_popup(text):
    # Get the file name of the script
    script_file = os.path.basename(__file__)
    # Generate the popup
    ctypes.windll.user32.MessageBoxW(0, text, script_file, 0)

# Checks a target url for a string at a given interval and runs specified alert behaviour
def open_and_refresh(url, headless_mode, success_type, refresh_interval, check_string, alert_type, mp3_path=''):
    # Prep webdriver
    if headless_mode:  # Don't show the browser window
        chrome_options = Options()
        chrome_options.add_argument("--headless")  # Make the browser run in headless mode
        driver = webdriver.Chrome(options=chrome_options)  # Pass the options when creating the webdriver
    else:  # Do show the browser window
        driver = webdriver.Chrome()

    driver.get(url)

    # Core loop
    while True:
        # Wait and print message per second to console based on refresh interval
        for i in range(refresh_interval, 0, -1):
            print(f"Refreshing in {i} seconds...")
            time.sleep(1)
        print("Refreshing now!")
        driver.refresh()

        # Get the page source as a string
        html = driver.page_source

        # Function to run conditional alert behaviour
        def alert(append_string):
            # Function to make string for messages
            def message_string(message_type):
                output_string = f"Target URL: '{url}'\n" \
                                f"Check string: '{check_string}'\n" \
                                f"Success Type: {success_type}\n" \
                                f"Alert Type: '{message_type}'\n" \
                                f"Result / Action: '{append_string}"
                return output_string

            # Generate that string
            alert_message_string = message_string(alert_type)

            # For text to speech
            if alert_type == 'tts':
                print(alert_message_string)
                text_to_speech(alert_message_string)
            # For mp3
            elif alert_type == 'mp3':
                print(alert_message_string)
                invalid_path_exit(mp3_path)
                playsound(mp3_path)
            elif alert_type == 'popup':
                print(alert_message_string)
                winsound.PlaySound("SystemAsterisk", winsound.SND_ASYNC)
                show_popup(alert_message_string)
            # If no valid option was selected
            else:
                print("Choose 'tts' or 'mps' for var alert_type")
                sys.exit()

        # Actions to take if success_type is 'not in'. i.e. script ends if string is NOT found
        if success_type == 'string not found':
            if check_string in html:
                print(f"'{check_string}' string still found - repeating loop")
            else:
                alert('SUCCESS: string not found - breaking loop')
                break

        # Actions to take if success_type is 'in'. i.e. script ends if string IS found
        if success_type == 'string found':
            if check_string in html:
                alert('SUCCESS: string found - breaking loop')
                break
            else:
                print(f"'{check_string}' string not found - repeating loop")

# Path definition
target_url = "https://example.com.au/collections/product-in-stock"
alert_sound_audio = r"C:\Users\kylec\Audio\alert_sound.mp3"

# Running the function
open_and_refresh(
    url=target_url,  # URL to check
    headless_mode=True,  # True or False - False if you want to see the browser window
    success_type='string not found',  # 'string not found' or 'string found'
    refresh_interval=30,  # How often to recheck the page in seconds
    check_string='Stock is empty',  # The string to check the html for
    alert_type='popup',  # 'tts', 'mp3' or 'popup'
    mp3_path=alert_sound_audio)  # path of mp3 audio file to play - OPTIONAL


# Naive method to find a pair in a list with the given sum
def findPair(nums, target):

    # consider each element except the last
    for i in range(len(nums) - 1):

        # start from the i'th element until the last element
        for j in range(i + 1, len(nums)):

            # if the desired sum is found, print it
            if nums[i] + nums[j] == target:
                print('Pair found', (nums[i], nums[j]))
                return

    # No pair with the given sum exists in the list
    print('Pair not found')

if __name__ == '__main__':

    nums = [8, 7, 2, 5, 3, 1]
    target = 10

    findPair(nums, target)

Find a pair with the given sum in an array/Python.py


import os
import cv2
import glob as glob
from tqdm import tqdm

from dataset import CocoDataset

COLOR_PLATE_TRUE = (0,255,0)
COLOR_PLATE_PRED = (0,0,255)
COLOR_FACE_TRUE = (255,0,0)
COLOR_FACE_PRED = (0, 165, 255)
IMG_EXT = ['jpg', 'jpeg', 'png']

def draw_bbox_yolo1(img_path, txt_path, save_path, threshold = 0.1, label_viz = True):
    img = cv2.imread(img_path)
    height, width, _ = img.shape
    os.makedirs(save_path, exist_ok=True)

    with open(txt_path, 'r') as f:
        data = [x for x in f.read().strip().split('\n') if x != '']

    if len(data) == 0:
        return

    for line in data:
        bbox = [x for x in line.split(' ') if x != '']
        conf = float(bbox[-1]) if len(bbox) == 6 else 1

        if conf < threshold:
            print(conf, threshold)
            continue

        x_center, y_center = float(bbox[1]) * width, float(bbox[2]) * height
        box_width, box_height = float(bbox[3]) * width, float(bbox[4]) * height

        x_min = int(x_center - box_width/2)
        y_min = int(y_center - box_height/2)
        x_max = int(x_center + box_width/2)
        y_max = int(y_center + box_height/2)

        if bbox[0] in ['plate', '0'] : #plate
            color = COLOR_PLATE_TRUE if label_viz else COLOR_PLATE_PRED
        elif bbox[0] in ['face', '1']: #face
            color = COLOR_FACE_TRUE if label_viz else COLOR_FACE_PRED
        else:
            raise ValueError(f'not class: {bbox[0]}')

        img = cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, 2)
        # if not label_viz:
            # blur_bbox(img, (x_min, y_min), (x_max, y_max))

    os.makedirs(save_path, exist_ok=True)
    cv2.imwrite(os.path.join(save_path, os.path.basename(img_path)),img)

def draw_bbox_yolov1_folder(img_root, true_txt_root, pred_txt_root, save_path, threshold=0.05, double_viz = True):
    pred_txt_root = pred_txt_root if double_viz else true_txt_root
    img_paths = sorted([x for x in glob.glob(f'{img_root}/**/*', recursive=True) if x.split('.')[-1].lower() in IMG_EXT])
    true_paths = sorted([x for x in glob.glob(f'{true_txt_root}/**/*.txt', recursive=True)])
    pred_paths = sorted([x for x in glob.glob(f'{pred_txt_root}/**/*.txt', recursive=True)])

    for index, img_path in enumerate(tqdm(img_paths)):
        img_name = os.path.splitext(img_path.split(f'{img_root}/')[-1])[0]
        true_path = f'{true_txt_root}/{img_name}.txt'
        pred_path = f'{pred_txt_root}/{img_name}.txt'

        if true_path not in true_paths or pred_path not in pred_paths:
            print(f'{img_name} txt file not found in directory')
            continue

        draw_bbox_yolo1(img_path, true_path, save_path, threshold = threshold)
        if double_viz:
            draw_bbox_yolo1(f'{save_path}/{img_path.split(f"{img_root}/")[-1]}', pred_path, save_path, threshold = threshold, label_viz=False)

def blur_bbox(img, top_left, bottom_right):
    x_min, y_min = top_left
    x_max, y_max = bottom_right
    bbox = img[y_min:y_max, x_min:x_max]
    bbox =  cv2.GaussianBlur(bbox,(51,51),cv2.BORDER_DEFAULT)

    img[y_min:y_max, x_min:x_max] = bbox

    return img

if __name__ == '__main__':
    import yaml

    with open("utils/configs/viz_config.yaml", 'r') as stream:
        try:
            config_dict = yaml.safe_load(stream)
        except yaml.YAMLError as exc:
            print(exc)

    for index in range(len(config_dict['true_annotation_path'])):
        coco_dataset = CocoDataset(
            true_annotation_path = config_dict['true_annotation_path'][index],
            pred_annotation_path = config_dict['pred_annotation_path'][index],
            save_path = config_dict['save_path'][index],
            src_img_root = config_dict['src_img_root'],
            dest_img_root = config_dict['dest_img_root'],
            )

        ## Visualize both onto images
        coco_dataset.visualize_images(annotation_type = 'bbox', conf_threshold = 0.05, size_threshold=144)


import os
import redis.asyncio as redis
from pyrogram.client import Client
from pyrogram import filters

redis_db = redis.from_url("redis://redis")

api_id = os.getenv("API_ID")
api_hash = os.getenv("API_HASH")
app = Client("bayanist", api_id=api_id, api_hash=api_hash)

CHANNELS_TO_LISTEN = set([int(chat_id) for chat_id in os.getenv("CHANNELS_TO_LISTEN").split(",")])
MAX_QUEUE_CAPACITY = 250
QUEUE_NAME = "memes_queue"

def specific_channel_filter(_, __, message):
    return message.chat.id in CHANNELS_TO_LISTEN

is_chosen_channel = filters.create(specific_channel_filter)

async def add_meme_to_queue(data):
    if await redis_db.llen(QUEUE_NAME) >= MAX_QUEUE_CAPACITY:
        await redis_db.rpop(QUEUE_NAME)
    await redis_db.lpush(QUEUE_NAME, data)

@app.on_message(filters.channel & ~filters.forwarded & is_chosen_channel & filters.photo)
async def await_memes(client, message):
    f = await client.download_media(message.photo, in_memory=True)
    await add_meme_to_queue(f.getvalue())

if __name__ == "__main__":
    app.run()


n=int(input())
i=0
res=list()
while i<n:
    num=int(input())
    save=num
    length=0
    while num!=0:
        num=num//2
        length=length+1
    print(2**length-1-save)
    i=i+1


import numpy as np
import os.path
import math, random
import pandas
import matplotlib.pyplot as plt
# import matplotlib.pyplot as plt
# from sklearn.model_selection import train_test_split

def setImage(filename):
    f = open(filename, 'rb')
    array = []
    magic_num = int.from_bytes(f.read(4), 'big')
    image_cnt = int.from_bytes(f.read(4), 'big')
    row_cnt = int.from_bytes(f.read(4), 'big')
    col_cnt = int.from_bytes(f.read(4), 'big')
    # print(magic_num)
    # print(image_cnt)
    # print(row_cnt)
    # print(col_cnt)
    pixel_cnt = row_cnt*col_cnt
    # print(pixel_cnt)
    array = np.zeros((image_cnt,pixel_cnt))
    for i in range(image_cnt):
        for j in range(pixel_cnt):
            array[i][j] = int.from_bytes(f.read(1), 'big')
    f.close()
    # print(np.shape(array))
    array = np.floor(array/128) #tally into 2 bins
    return array

def setLabel(filename):
    f = open(filename, 'rb')
    array = []
    magic_num = int.from_bytes(f.read(4), 'big')
    label_cnt = int.from_bytes(f.read(4), 'big')
    # print(magic_num)
    # print(label_cnt)
    array = np.zeros((label_cnt))
    for i in range(label_cnt):
        array[i] = int.from_bytes(f.read(1), 'big')
    f.close
    return array

def setdata():
    if (os.path.exists('trainImage.npy') and
        os.path.exists('trainLabel.npy') and
        os.path.exists('testImage.npy') and
        os.path.exists('testLabel.npy')):
        # print("save1")
        trainImage = np.load('trainImage.npy')
        trainLabel = np.load('trainLabel.npy')
        testImage = np.load('testImage.npy')
        testLabel = np.load('testLabel.npy')
        # print("save2")
    else:
        trainImage = setImage('train-images.idx3-ubyte')
        trainLabel = setLabel('train-labels.idx1-ubyte')
        testImage = setImage('t10k-images.idx3-ubyte')
        testLabel = setLabel('t10k-labels.idx1-ubyte')
        np.save('trainImage', trainImage)
        np.save('trainLabel', trainLabel)
        np.save('testImage', testImage)
        np.save('testLabel', testLabel)
    return trainImage, trainLabel, testImage, testLabel

def setBernoulli(Image):
    pixel_cnt = Image.shape[1] # = 784
    lamd = np.full((10),0.1) # 0.1 0.1 0.1 0.1 0.1 0.1....
    p_pixels = np.random.rand(10, pixel_cnt) # random(0~1) matrix: 10*784
    return lamd, p_pixels

def E_step(image_cnt,Image,lamd,p):
    #  responsibility: w(x1|0)...w(x1|9), w(x2|0)...w(x2|9),....., w(x60000|0)...w(x60000|9)
    #  wi = exp(Zi)/sum((Zi))
    z = np.zeros((image_cnt,10)) # = 60000*10
    w = np.zeros((image_cnt,10)) # = 60000*10
    for i in range((image_cnt)): # 60000
        unitImage = Image[i] # 784 pixels
        for j in range(10):
            z[i][j] = np.log(lamd[j])+np.sum(p[j]*unitImage)+np.sum((1-p[j])*(1-unitImage))
        w[i] = np.exp(z[i]-max(z[i]))
        w[i] = w[i]/np.sum(w[i])
    return w

def M_step(image_cnt,Image,w):
    wSum = np.sum(w,axis=0) # 10*1
    lamd = wSum/image_cnt
    p = np.dot(np.transpose(w),Image)
    for i in range(10):
        p[i]/=wSum[i]
    return lamd,p

def EM(Image,lamd,p_pixels):
    image_cnt = Image.shape[0] # = 60000
    pixel_cnt = Image.shape[1] # = 784
    p = p_pixels.copy() # = 10*784

    lamd_past = lamd.copy()
    p_past = p.copy()
    iterate_cnt = 0
    while True:
        iterate_cnt+=1
        # E step
        w = E_step(image_cnt,Image,lamd,p)
        # M step
        lamd, p = M_step(image_cnt,Image,w)
        # print("lamd:",lamd," p_pixels:")
        # print(" iterate:",iterate_cnt," del_lamd:",np.linalg.norm(lamd-lamd_past)," del_p:",np.linalg.norm(p-p_past))

        if np.linalg.norm(lamd-lamd_past)<=0.1 and np.linalg.norm(p-p_past)<=0.1:
            break
        lamd_past = lamd.copy()
        p_past = p.copy()

    return lamd, p, iterate_cnt

def LabelClassMapping(Image, Label, lamd, p_pixels):
    # label[i] = class[j]
    p = p_pixels.copy() # = 10*784
    labl_cl_mapping = np.full((10),-1)
    num_probility = np.zeros((10))
    for i in range(10): #10
        Image_is_num_i = Image[Label == i]
        predict_num_appear_times = np.zeros((10))
        for j in Image_is_num_i: # j: single image, 784 pixels
            for k in range(10):
                num_probility[k] = lamd[k]*np.prod(p[k][j==1])
            predict_num = np.argmax(num_probility)
            predict_num_appear_times[predict_num] += 1

        predict_sort_index = np.argsort(predict_num_appear_times)

        index = 9
        while np.any(labl_cl_mapping == predict_sort_index[index]):
            index -= 1
        labl_cl_mapping[i] = predict_sort_index[index]
    return labl_cl_mapping

def ConfusionMatrix(Image, Label, lamd, p_pixels, labl_cl_mapping):
    conf_mtrx = np.zeros((10,4))
    for i in range(10):
        num_y = Image[Label == i]
        num_n = Image[Label != i]
        for r in num_y:
            predict_P = np.zeros((10))
            for j in range(10):
                predict_P[j] = lamd[j]*np.prod(p_pixels[j][r == 1])
            predict = np.argmax(predict_P)
            mapping_index = np.argwhere(labl_cl_mapping == predict)
            predict_label = mapping_index.item()
            if predict_label==i:
                conf_mtrx[i][0] +=1
            else:
                conf_mtrx[i][1] +=1
        for r in num_n:
            predict_P = np.zeros((10))
            for j in range(10):
                predict_P[j] = lamd[j]*np.prod(p_pixels[j][r == 1])
            predict = np.argmax(predict_P)
            mapping_index = np.argwhere(labl_cl_mapping == predict)
            predict_label = mapping_index.item()
            if predict_label==i:
                conf_mtrx[i][2] +=1
            else:
                conf_mtrx[i][3] +=1
    return conf_mtrx

def printResult(p, iterate_cnt, label_mapping, conf_mtrx, error_rate):
# Print out the result
    for i in range(10):
        print("class ", i, ":")
        lbl = label_mapping[i]
        pixels = p[lbl].copy()
        pixels[p[lbl].copy()>0.5] = 1
        pixels[p[lbl].copy()<=0.5] = 0
        pixels = pixels.reshape(28,28)
        for j in range(28):
            for k in range(28):
                if pixels[j][k] == 1:
                    print(1,end='')
                else:
                    print(0,end='')
                if (k+1)%28 == 0:
                    print('')
        print("")

    for i in range(10):
        print("------------------------------------------------------------")
        print("")
        print("Confusion Matrix ", i,":")
        print("                 Predict number ", i," Predict not number ", i)
        print("Is number ",i,"       ",conf_mtrx[i][0],"                 ", conf_mtrx[i][1])
        print("Isn't number ",i,"    ",conf_mtrx[i][2],"                ", conf_mtrx[i][3])
        print("Sensitivity (Successfully predict number ", i, ") : ", conf_mtrx[i][0]/(conf_mtrx[i][0]+conf_mtrx[i][1]))
        print("Specificity (Successfully predict not number ", i, "): ", conf_mtrx[i][3]/(conf_mtrx[i][2]+conf_mtrx[i][3]))
        print("")

    print('Total Iteration to converge: ', iterate_cnt)
    print('Total error rate: ', error_rate)

if __name__ == '__main__':
    testDataCnt = 500

    trainImage, trainLabel, testImage, testLabel = setdata()

    lamd, p_pixels = setBernoulli(trainImage) # lamb=1*10, p_pixel = 10*784
    lamd, p_pixels, iterate_cnt = EM(trainImage,lamd,p_pixels)

    # label[i] = class[j]
    labl_cl_mapping = LabelClassMapping(trainImage, trainLabel, lamd, p_pixels)
    print(labl_cl_mapping)

    # confusion matrix
    conf_mtrx = ConfusionMatrix(trainImage, trainLabel, lamd, p_pixels, labl_cl_mapping)

    # error rate
    correct_cnt = 0
    for i in range(testDataCnt):
        predict_P = np.zeros((10))
        for j in range(10):
            predict_P[j] = lamd[j]*np.prod(p_pixels[j][testImage[i]==1])
        predict = np.argmax(predict_P)
        mapping_index = np.argwhere(labl_cl_mapping == predict)
        predict_label = mapping_index.item()
        if predict_label == testLabel[i]:
            correct_cnt += 1
    error_rate = 1- correct_cnt/testDataCnt

    printResult(p_pixels, iterate_cnt, labl_cl_mapping, conf_mtrx, error_rate)