import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.cluster import KMeans
from sklearn import preprocessing
import random

import sys
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
dirs = os.path.join( os.path.dirname(__file__),'..')
os.sys.path.append(os.path.join( os.path.dirname(__file__), '..'))
from tools.get_data import GetData
from tools.pearson import CalcPerson

import argparse

class svm3calss(object):
    def __init__(self,  args):
        self.input_size = args.input_size
        self.num_classes =  args.num_classes
        self.learning_rate=args.learning_rate

        self.inputs = tf.placeholder(shape=[None, self.input_size], dtype=tf.float32)
        self.targets = tf.placeholder(shape=[self.num_classes, None], dtype=tf.float32)
        self.batch_size=tf.placeholder(shape=None,dtype=tf.int32,name='batch_size')

        self.bias = tf.Variable(tf.random_normal(shape=[self.num_classes,self.batch_size]))

        with tf.name_scope('rbf_kernel'):
            gamma=self.rbf_kernel()
        with tf.variable_scope('loss'):
            self.log_loss()
        with tf.variable_scope('predict_kernel'):
            self.rbf_predict_kernel(gamma)
        with tf.variable_scope('predict'):
            self.predict()
        with tf.name_scope('train'):
            self.train_op = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss)

    def rbf_kernel(self):
        gamma = tf.constant(-10.0)
        dist = tf.reduce_sum(tf.square(self.inputs), 1)
        dist = tf.reshape(dist, [-1,1])
        sq_dists = tf.multiply(2., tf.matmul(self.inputs, tf.transpose(self.inputs)))
        self.kernel = tf.exp(tf.multiply(gamma, tf.abs(sq_dists)))
        return gamma
    def reshape_matmul(self,mat):
        v1 = tf.expand_dims(mat, 1)
        v2 = tf.reshape(v1, [self.num_classes, self.batch_size, 1])
        return(tf.matmul(v2, v1))

    def log_loss(self):
        first_term = tf.reduce_sum(self.bias)
        b_vec_cross = tf.matmul(tf.transpose(self.bias), self.bias)
        y_target_cross = self.reshape_matmul(self.targets)
        second_term = tf.reduce_sum(tf.multiply(self.kernel , tf.multiply(b_vec_cross, y_target_cross)),[1,2])
        self.loss = tf.reduce_sum(tf.negative(tf.subtract(first_term, second_term)))

    def rbf_predict_kernel(self,gamma):
        self.prediction_grid = tf.placeholder(shape=[None, self.input_size], dtype=tf.float32)
        rA = tf.reshape(tf.reduce_sum(tf.square(self.inputs), 1),[-1,1])
        rB = tf.reshape(tf.reduce_sum(tf.square(self.prediction_grid), 1),[-1,1])
        pred_sq_dist = tf.add(tf.subtract(rA, tf.multiply(2., tf.matmul(self.inputs, tf.transpose(self.prediction_grid)))), tf.transpose(rB))
        self.pred_kernel = tf.exp(tf.multiply(gamma, tf.abs(pred_sq_dist)))
    def predict(self):
        prediction_output = tf.matmul(tf.multiply(self.targets,self.bias), self.pred_kernel)
        self.prediction = tf.arg_max(prediction_output-tf.expand_dims(tf.reduce_mean(prediction_output,1), 1), 0)
        self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.prediction, tf.argmax(self.targets,0)), tf.float32))

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--is-train', type=int, default=0,help='1=train, 2=test, 0=train and test')
    parser.add_argument('--num-iter', type=int, default=100,help='the number of training steps to take')
    parser.add_argument('--batch-size', type=int, default=64,help='the number of peptide')

    parser.add_argument('--learning-rate', type=float, default=1e-3,help='')
    parser.add_argument('--input-size', type=int, default=192,help='')
    parser.add_argument('--num-classes', type=int, default=3,help='')

    parser.add_argument('--intensity_num_label', type=int, default=4, help="")
    return parser.parse_args()

def get_batch_peptide(merge_list,_batch_size):
    number_of_peptide=len(merge_list[0])
    batch_peptide=[]
    seq_length=[]
    _batch_number=int(number_of_peptide/_batch_size)
    for i in range(_batch_number):
        batch_peptide.append(merge_list[0][i*_batch_size:(i+1)*_batch_size])
        seq_length.append([])
        for j in range(len(batch_peptide[i])):
            seq_length[len(batch_peptide)-1].append(len(merge_list[0][i*_batch_size+j]))
    if _batch_number*_batch_size < number_of_peptide:
        seq_length.append([])
        batch_peptide.append(merge_list[0][_batch_number*_batch_size:])
        for k in range(len(batch_peptide[-1])):
            seq_length[len(batch_peptide)-1].append(len(merge_list[0][_batch_number*_batch_size+k]))
        _batch_number+=1
    return batch_peptide,_batch_number,seq_length

def model_train(args):
    model = svm3calss(args)
    with tf.Session() as sess:
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter("seq2seq-WGAN/svm-logs", sess.graph)
        sess.run(tf.global_variables_initializer())
         #train data
        _,_,train_X,train_y,merge_train_list=data.get_discretization_data('data/data_mm/test_test.txt',args.num_classes)
        print(str(len(merge_train_list[0]))+' train peptides ,DataShape:('+str(np.array(train_X).shape)+str(np.array(train_y).shape)+')')
        train_batch_peptide,_batch_number,seq_length=get_batch_peptide(merge_train_list,args.batch_size)
        y1 = np.array([1 if y==0 else -1 for y in train_y])
        y2 = np.array([1 if y==1 else -1 for y in train_y])
        y3 = np.array([1 if y==2 else -1 for y in train_y])
        train_y = np.array([y1, y2, y3])

        ##val data
        #_,_,test_X,test_y,merge_test_list=data.get_discretization_data('data/data_mm/test.txt',args.num_classes)
        #print(str(len(merge_test_list[0]))+' test peptides ,DataShape:('+str(np.array(test_X).shape)+str(np.array(test_y).shape)+')')

        print('..trainning')
        loss_vec = []
        batch_accuracy = []
        for Iter in range(args.num_iter):
            for i,train_piptides in enumerate(train_batch_peptide):

                train_ions=data.get_split_list2(train_piptides)

                X= train_X[train_ions]
                y= train_y[:,train_ions]
                sess.run(model.train_op, feed_dict={model.inputs: X, model.targets:  y,model.batch_size:len(X)})
                temp_loss = sess.run(model.loss, feed_dict={model.inputs:  y, model.targets:  y,model.batch_size:len(X)})
                loss_vec.append(temp_loss)

                acc_temp = sess.run(model.accuracy, feed_dict={model.inputs: X, model.targets:  y, model.prediction_grid:X,model.batch_size:len(X)})
                batch_accuracy.append(acc_temp)

                print('Step #' + str(i+1))
                print('Loss = ' + str(temp_loss))

            val_acc=0;val_loss=0
            for i, val_piptide_index in enumerate(val_batch_peptide):
                X=[];y=[]

                max_ions_number=max(val_seq_length[i])
                for j in range(len(val_piptide_index)):
                    train_ion_index=data.get_split_list(val_piptide_index[j])
                    X.append(padding_data(val_X[np.array(train_ion_index)],1,max_ions_number))
                    y.append(padding_data(val_y[np.array(train_ion_index)],0,max_ions_number))
                feed_dict_val = {
                            model.X:np.array(X),
                            model.y:np.array(y),
                            model.keep_prob:args.keep_prob,
                            model.seq_length:val_seq_length[i],
                            model.max_time:max_ions_number,
                            model.batch_size:len(X)

                    }
                loss_val, pred_val = sess.run([model.loss, model.tags],feed_dict=feed_dict_val)
                val_loss+=loss_val
                mask = (np.expand_dims(np.arange(max_ions_number), axis=0) < np.expand_dims(val_seq_length[i], axis=1))
                total_labels = np.sum(val_seq_length[i])
                correct_labels = np.sum((np.array(y) == pred_val) * mask)
                accuracy = 100.0 * correct_labels / float(total_labels)
                val_acc+=accuracy
            #val_acc,val_loss=val(args,model,val_X,val_y,val_batch_peptide,val_seq_length)
            print("Epoch: %d" % (Iter+1), "train loss: %.2f" % (train_loss/_batch_number),"train acc: %.2f%%" % (train_acc/_batch_number))
            print("Epoch: %d" % (Iter+1), "val loss: %.2f" % (val_loss/val_batch_number),"val acc: %.2f%%" % (val_acc/val_batch_number))
            result = sess.run(merged, feed_dict)
            writer.add_summary(result, Iter)

            #print('Iter[%d/%d],loss[%.4f]' % (Iter+1,args.num_iter,round(loss,4)))
        print("SaveModel:",tf.train.Saver().save(sess,'lstm/model/model.ckpt'))

def MSE(label,pred):
    return tf.reduce_mean(tf.square(pred-label))

def model_predict(args,kmodel,test_data,merge_test_list,test_label):
    print('predicting..')

    print('number of peptide:'+str(len(merge_test_list[0])))
    with tf.Session() as session:
        batch_peptide,_batch_number,_seq_length=get_batch_peptide(merge_test_list,args.batch_size)

        saver = tf.train.import_meta_graph('lstm/model/model.ckpt.meta')
        saver.restore(session, tf.train.latest_checkpoint('lstm/model/'))
        graph=tf.get_default_graph()
        inputs_X=graph.get_operation_by_name("inputs/X").outputs[0]
        batch_size=graph.get_operation_by_name("batch_size").outputs[0]
        keep_prob=graph.get_operation_by_name("keep_prob").outputs[0]
        max_time=graph.get_operation_by_name("max_time").outputs[0]
        seq_length=graph.get_operation_by_name("seq_length").outputs[0]

        pred_y=tf.get_collection("pred_network")[0]
        pred=[];aaa=[]
        mse_list=[]
        total_labels=0;correct_labels=0
        for i,(test_piptide_index) in enumerate(batch_peptide):
            X=[];y=[]
            _max_ions_number=max(_seq_length[i])
            padding_pep_num=args.batch_size-len(test_piptide_index)
            for j in range(len(test_piptide_index)):
                test_ion_index=get_split_list(test_piptide_index[j])
                X.append(padding_data(test_data[np.array(test_ion_index)],1,_max_ions_number))
                y.append(padding_data(test_label[np.array(test_ion_index)],0,_max_ions_number))

            pred_ = session.run(pred_y,feed_dict={
                inputs_X: np.array(X),
                keep_prob:args.keep_prob,
                seq_length:_seq_length[i],
                max_time:_max_ions_number,
                batch_size:len(X)
                                      })
            for k in range(len(X)):
                pred.extend(pred_[k][:_seq_length[i][k]])
                aaa.extend(y[k][:_seq_length[i][k]])

            mask = (np.expand_dims(np.arange(_max_ions_number), axis=0) < np.expand_dims(_seq_length[i], axis=1))
            total_labels += np.sum(_seq_length[i])
            correct_labels += np.sum((np.array(y) == pred_) * mask)

        accuracy = 100.0 * correct_labels / float(total_labels)
        print("test Accuracy: %.2f%%" % accuracy)
            #pred_[pred_>1]=1
            #pred_[pred_<0]=0
            #_mse=session.run(MSE(np.reshape(y,(-1,1)),pred_))
            #mse_list.append(_mse)
        cunt=0;cunt2=0
        for i in range(len(aaa)):
            if aaa[i]==0:
                cunt+=1
            if aaa[i]==pred[i]:
                cunt2+=1

        print(cunt/len(aaa))
        print(cunt2/len(aaa))
        predaaa = pd.DataFrame({"pred":pred,"label":aaa})
        predaaa.to_csv('data//SwedCAD_pred2.csv')
        min_max_scaler = preprocessing.MinMaxScaler()
        pred_minmax = min_max_scaler.fit_transform(pred)
    return pred_minmax

def get_merge_pred(merge_list,pred,data):
    print('get predict spectrum intensity list...')
    merge_list.append(data.merge_list_1label(pred))
    return merge_list
def calc_pear(test_idx,peptide,pred,merge_list,pear,data):

    pred_pd=pear.write_pred(test_idx,peptide,pred)
    merge_list=get_merge_pred(merge_list,pred_pd,data)

    person_mean=pear.get_pearson(merge_list)
    return person_mean

def test(args,data,pear):
    test_idx,peptide,pred,merge_test_list=model_predict(args,data)
    person_mean=calc_pear(test_idx,peptide,pred,merge_test_list,pear,data)
    print(person_mean)

def main(args,data,pear):
    if args.is_train==1:
        model_train(args)
    elif args.is_train==2:
        test(args,data,pear)
    else:
        model_train(args)
        test(args,data,pear)

if __name__ == '__main__':
    args=parse_args()

    data=GetData(args.intensity_num_label)
    pear=CalcPerson(args.intensity_num_label)
    main(args,data,pear)


'''折线图条形图组合:补贴总额和平均补贴'''
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Line
from pyecharts.charts import Bar
from pyecharts.charts import Grid
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

# 导入数据:
datafile = r'E:\学术论文\博士论文\论文图表\政府补贴现状数据.xlsx'
data = pd.read_excel(datafile)
X = list(data.年份) # 或者attr = list(data.['年份'])
Z1 = list(data['补贴金额（亿元）'])
Y1 = []
for i in Z1:
    Y1.append(float('%.2f'%i)) # 总补贴金额（亿元） 条形图数据
Z2 = list(data['平均每家公司补贴'])
Y2 = []
for i in Z2:
    Y2.append(float('%.2f'%i)) # 平均每家公司补贴（亿元） 折线图数据

def grid_multiy():
    bar=(
        Bar()
        .add_xaxis(xaxis_data=X)
        .add_yaxis(
            series_name='上市公司获得总补贴(亿元)',
            yaxis_data=Y1,
            color='#5793f3', #蓝色
            label_opts=opts.LabelOpts(is_show=False),
            )
        .extend_axis(
            yaxis=opts.AxisOpts(
                name='平均补贴',
                type_='value',
                min_=0.2,
                max_=0.8,
                position='right',
                name_textstyle_opts=opts.TextStyleOpts(
                    font_size=20,
                    font_weight='bold',
                    color='#d14a61',
                ),
                #is_scale='True',
                axislabel_opts=opts.LabelOpts(
                    font_size=15,
                    color='#d14a61'
                ),
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(
                        color='#d14a61',
                    ),
                ),
            )
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(
                orient='vertical',
                textstyle_opts=opts.TextStyleOpts(
                    font_size=20,
                ),
            ),
            title_opts=opts.TitleOpts(
                #title='图2.1 上市公司获得政府补贴及每家公司平均获得补贴金额',
                title_textstyle_opts=opts.TextStyleOpts(
                    font_size=25,
                ),
                pos_left='center',
                pos_bottom='bottom',
            ),
            yaxis_opts=opts.AxisOpts(
                name='总补贴',
                position='left',
                #offset=30,
                name_textstyle_opts=opts.TextStyleOpts(
                    font_size=20,
                    font_weight='bold',
                    color='#5793f3',
                ),
                #is_scale='True',
                axislabel_opts=opts.LabelOpts(
                    font_size=15,
                    color='#5793f3',
                ),
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(
                        color='#5793f3',
                    ),
                ),
            ),
        ) # 此处不能多加逗号，否则会结果对象会改变，由一个Bar对象变为多个Bar构成的tuple集合；
    )

    line=(
        Line()
        .add_xaxis(xaxis_data=X)
        .add_yaxis(
            series_name='每家上市公司获得的平均补贴（亿元）',
            y_axis=Y2,
            yaxis_index=1,  #多Y轴时，要设置此项；
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(
                orient='vertical',
                textstyle_opts=opts.TextStyleOpts(
                    font_size=20,
                ),
            ),
        )
    )

    bar.overlap(line)
    return bar
                                # 函数不能多一个括号
grid_multiy().render()
grid_multiy().render(r"E:\学术论文\博士论文\论文图表\图2-1上市公司获得补贴数据.html")
make_snapshot(snapshot,grid_multiy().render(),r"E:\学术论文\博士论文\论文图表\图2-1上市公司获得补贴数据.png")


#!/usr/local/bin/python3.7

import statistics

def push_stack(stack_a, stack_b):
	stack_a.insert(0, stack_b.pop(0))

def swap_stack(stack_a):
	if len(stack_a) == 1:
		return
	tmp = stack_a[0]
	stack_a[0] = stack_a[1]
	stack_a[1] = tmp

def rotate_stack(stack_a):
	tmp = stack_a.pop(0)
	stack_a.append(tmp)

def reverse_rotate_stack(stack_a):
	tmp = stack_a.pop()
	stack_a.insert(0, tmp)

def run_command(command, stack_a, stack_b):
	command = command.strip()
	if command == "pa":
		push_stack(stack_a, stack_b)
	if command == "pb":
		push_stack(stack_b, stack_a)
	if command == "sa":
		swap_stack(stack_a)
	if command == "sb":
		swap_stack(stack_b)
	if command == "ss":
		swap_stack(stack_a)
		swap_stack(stack_b)
	if command == "ra":
		rotate_stack(stack_a)
	if command == "rb":
		rotate_stack(stack_b)
	if command == "rr":
		rotate_stack(stack_a)
		rotate_stack(stack_b)
	if command == "rra":
		reverse_rotate_stack(stack_a)
	if command == "rrb":
		reverse_rotate_stack(stack_b)
	if command == "rrr":
		reverse_rotate_stack(stack_a)
		reverse_rotate_stack(stack_b)

def is_sorted(stack):
	count = len(stack)
	index = 0
	while index < count - 1:
		if stack[index + 1] < stack[index]:
			return False
		index += 1
	return True

def sort_two(stack, ops_list):
	if len(stack) != 2:
		return
	if stack[0] > stack[1]:
		tmp = stack[0]
		stack[0] = stack[1]
		stack[1] = tmp
		ops_list.append("sa")

def sort_three(stack, ops_list):
	if len(stack) != 3 or is_sorted(stack):
		return
	elif stack[0] < stack[1] and stack[1] > stack[2] and stack[0] < stack[2]:
		ops_list.append("rra")
		ops_list.append("sa")
	elif stack[0] > stack[1] and stack[2] > stack[0]:
		ops_list.append("sa")
	elif stack[0] < stack[1] and stack[1] > stack[2]:
		ops_list.append("rra")
	elif stack[0] > stack[1] and stack[1] < stack[2]:
		ops_list.append("ra")
	else:
		ops_list.append("sa")
		ops_list.append("rra")

def get_min_and_pb(stack_a, stack_b, ops_list):
	info = get_info(stack_a)
	while info["index"] != 0:
		if info["index"] > len(stack_a) / 2:
			command = "rra"
		else:
			command = "ra"
		run_command(command, stack_a, stack_b)
		ops_list.append(command)
		info = get_info(stack_a)
	run_command("pb", stack_a, stack_b)
	ops_list.append("pb")

def get_info(stack):
	info = dict()
	info["min"] = min(stack)
	info["index"] = stack.index(info["min"])
	return info

def push_below_median_to_stack_b(stack_a, stack_b, ops_list, median):
	while stack_a[0] < median:
		ops_list.append("pb")
		run_command("pb", stack_a, stack_b)
	pivot = stack_a[0]
	ops_list.append("ra")
	run_command("ra", stack_a, stack_b)
	while stack_a[0] != pivot:
		if stack_a[0] < median:
			ops_list.append("pb")
			run_command("pb", stack_a, stack_b)
		else:
			ops_list.append("ra")
			run_command("ra", stack_a, stack_b)

def push_above_median_to_stack_b(stack_a, stack_b, ops_list, median):
	while stack_a[0] >= median:
		ops_list.append("pb")
		run_command("pb", stack_a, stack_b)
	pivot = stack_a[0]
	ops_list.append("ra")
	run_command("ra", stack_a, stack_b)
	while stack_a[0] != pivot:
		if stack_a[0] >= median:
			ops_list.append("pb")
			run_command("pb", stack_a, stack_b)
		else:
			ops_list.append("ra")
			run_command("ra", stack_a, stack_b)

def count_ops(index, count):
	front = index
	back = count - index
	return min(front, back)

def find_highest_and_push_to_a(stack_a, stack_b, ops_list):
	mini_index = stack_b.index(min(stack_b))
	maxi_index = stack_b.index(max(stack_b))
	mini_ops = count_ops(mini_index, len(stack_b))
	maxi_ops = count_ops(maxi_index, len(stack_b))
	if mini_ops < maxi_ops:
		is_mini = 1
		target_index = mini_index
	else:
		is_mini = 0
		target_index = maxi_index
	target = stack_b[target_index]
	if target_index < len(stack_b) / 2:
		while stack_b[0] != target:
			run_command("rb", stack_a, stack_b)
			ops_list.append("rb")
	else:
		while stack_b[0] != target:
			run_command("rrb", stack_a, stack_b)
			ops_list.append("rrb")
	ops_list.append("pa")
	run_command("pa", stack_a, stack_b)
	if is_mini == 1:
		run_command("ra", stack_a, stack_b)
		ops_list.append("ra")

def push_from_stack_b_to_a(stack_a, stack_b, ops_list):
	while len(stack_b) > 1:
		find_highest_and_push_to_a(stack_a, stack_b, ops_list)
	run_command("pa", stack_a, stack_b)
	ops_list.append("pa")

def sort_multiple(stack_a, stack_b, ops_list):
	median = statistics.median(stack_a)
	push_above_median_to_stack_b(stack_a, stack_b, ops_list, median)
	push_from_stack_b_to_a(stack_a, stack_b, ops_list)
	if is_sorted(stack_a) == True:
		return
	push_below_median_to_stack_b(stack_a, stack_b, ops_list, median)
	while is_sorted(stack_a) == False:
		ops_list.append("rra")
		run_command("rra", stack_a, stack_b)
	push_from_stack_b_to_a(stack_a, stack_b, ops_list)
	while is_sorted(stack_a) == False:
		ops_list.append("rra")
		run_command("rra", stack_a, stack_b)


from geoalchemy2 import Geometry, Geography
from sqlalchemy import Column, Integer, Float, DateTime, ForeignKey, String
from sqlalchemy.orm import composite, relationship

from src.configs.database import Base
from src.rate_policies.domain.models import Location

class Area(Base):
    __tablename__ = "areas"

    area_id = Column(Integer, primary_key=True, autoincrement=True)
    area_boundary = Column(Geometry(geometry_type='POLYGON'))
    area_lat = Column(Float)
    area_lng = Column(Float)

    area_center = composite(Location, area_lat, area_lng)

class ParkingZone(Base):
    __tablename__ = "parking_zones"

    parkingzone_id = Column(Integer, primary_key=True, autoincrement=True)
    parkingzone_center = Column(Geometry(geometry_type='POINT'))
    parkingzone_radius = Column(Float)

class ForbiddenArea(Base):
    __tablename__ = "forbidden_areas"

    forbidden_area_id = Column(Integer, primary_key=True, autoincrement=True)
    forbidden_area_boundary = Column(Geometry(geometry_type='POLYGON'))

class Deer(Base):
    __tablename__ = "deers"

    deer_name = Column(Integer, primary_key=True, autoincrement=True)
    deer_area_id = Column(Integer, ForeignKey('areas.area_id'))

    deer_area = relationship("Area")

class Usage(Base):
    __tablename__ = "usages"

    usage_id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer)
    use_deer_name = Column(Integer, ForeignKey('deers.deer_name'))
    use_end = Column(Geometry(geometry_type='POINT'))
    use_start_at = Column(DateTime)
    use_end_at = Column(DateTime)

    use_deer = relationship("Deer", uselist=True)

class AreaFee(Base):
    __tablename__ = "fees"

    area_fee_id = Column(Integer, primary_key=True, autoincrement=True)
    area_id = Column("Area", ForeignKey('areas.area_id'))
    base = Column(Integer)
    rate_per_minute = Column(Integer)
    currency = Column(String)


import traceback
import os
import sys
import time
import datetime
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
from cosmoscan_api.cosmosWsApi import get_validators

sys.path.append(os.path.dirname(current_dir))
from influxdb_client.influxdb_client_v1 import InfluxClient
from utility.error_logger_writer import logger

db = InfluxClient()

measurement = "cosmoscan_validators"

def subscribe_validators(measurement):
    data = get_validators()['validators']
    for d in data:
        fields = {k:d[k] for k in d if k != "moniker" and k != "__typename" and k != "details"}
        fields.update({"tokens":int(d["tokens"])})
        fields.update({"commissionRate":float(d["commissionRate"])})
        #fields.update({"bondedHeight":int(d["bondedHeight"])})
        fields.update({"is_api_return_timestamp": False})
        tags = {}
        tags.update({"moniker":d["moniker"]})
        dbtime = False
        db.write_points_to_measurement(measurement,dbtime,tags,fields)

if __name__ == "__main__":
    try:
        subscribe_validators(measurement)
    except:
        error = traceback.format_exc()
        #logger(measurement,error)
    while True:
        time.sleep(55)
        try:
            subscribe_validators(measurement)
        except:
            error = traceback.format_exc()
            #logger(measurement, error)


from sentiment_analysis_auto_ml.data_loader import DataLoader

def test_can_load_data():
    dl = DataLoader()

    X, y = dl.load_data()

    assert len(X) != 0
    assert len(X) == len(y)


from datetime import datetime
from numbers import Number

from tqdm import tqdm

from clefts.catmaid_interface import get_catmaid
from clefts.manual_label.constants import Circuit, TABLES_DIR

HEADERS = ("Circuit", "Skeletons", "Nodes", "Presynaptic", "Postsynaptic", "Time")

def fmt_mins(total_mins):
    hrs, mins = divmod(total_mins, 60)
    return f"${hrs}$hr ${mins}$min"

def skids_to_row(*skids):
    contrib = catmaid.get_contributor_statistics_single(*skids)
    assert contrib["n_nodes"] > 0, "no nodes found"
    ret = (
        len(skids),
        contrib["n_nodes"],
        contrib["n_pre"],
        contrib["n_post"],
        fmt_mins(contrib["construction_minutes"]),
    )
    assert len(ret) == len(HEADERS) - 1
    return ret

def join_row(label, *values):
    fmt = "${}$"
    val_strs = [fmt.format(v) if isinstance(v, Number) else v for v in values]
    return " & ".join([label] + val_strs)

catmaid = get_catmaid()

pre_rows = (
    "\\begin{tabular}{l|" + "l" * len(HEADERS) + "}",
    " & ".join(HEADERS) + " \\\\ \\hline",
)

post_rows = ("\\end{tabular}",)

rows = list(pre_rows)

all_skids = set()

for circuit in tqdm(Circuit):
    annotation = circuit.annotation()

    skels = catmaid.get_skeletons_by_annotation(annotation)
    skids = [skel["skeleton_id"] for skel in skels]
    all_skids.update(skids)

    rows.append(join_row(circuit.tex_short(), *skids_to_row(*skids)) + " \\\\")

rows[-1] = rows[-1] + " \\hline"

total_label = "\\textbf{TOTAL}"
total_row = join_row(total_label, *skids_to_row(*all_skids))

rows.append(total_row)
rows.extend(post_rows)

s = "\n".join(rows) + "\n"

timestamp = datetime.now().isoformat()

fpath = TABLES_DIR / f"circuit_stats_{timestamp}.tex"
with open(fpath, "w") as f:
    f.write(s)


import os
import sys
root_path = os.path.join(os.getenv("HOME"), "vqa2019")
sys.path.append(root_path)
from utils.data_provider import VQADataProvider

def load_ques2set(filename):
	ques_set = set()
	with open(filename, "r") as f:
		for row in f:
			qa = row.rstrip().split("|")
			ques_set.add(qa[1])
	return ques_set

def load_all2set(filename):
	ques_set 	= set()
	ans_set 	= set()
	img_set 	= set()
	with open(filename, "r") as f:
		for row in f:
			qa = row.rstrip().split("|")
			if(len(qa) == 3):
				img_set.add(qa[0])
				ques_set.add(qa[1])
				ans_set.add(qa[2])
			else:
				print("Abnormal Row")
	return img_set, ques_set, ans_set

def check_raw_data(filename):
	answer_set = set()
	img_id_set = set()
	ques_set = set()
	q_i_pair_set = set()
	q_a_pair_set = set()
	with open(filename, "r") as f:
		# print("total q-i pair: ", len(f))
		counter = 0
		for row in f:
			qa = row.split("|")
			img_id_set.add(qa[0])
			ques_set.add(qa[1])
			answer_set.add(qa[2].rstrip())
			counter += 1
			q_i_pair_set.add((qa[0], qa[1]))
			q_a_pair_set.add((qa[1], qa[2].rstrip()))
		print("total records: ", counter)
	return answer_set, img_id_set, ques_set, q_i_pair_set, q_a_pair_set

def check_raw_readme(filename, ans_list):
	print("readme length: ", len(ans_list))
	raw_ans, _, _, _, _ = check_raw_data(filename)
	in_data_not_readme = raw_ans.difference(set(ans_list))
	in_readme_not_data = set(ans_list).difference(raw_ans)
	return in_data_not_readme, in_readme_not_data

def check_q_i_a_pair(filename):
	_, _, _, q_i_pairs, _ = check_raw_data(filename)
	q_i_dic_for_ans = {}
	with open(filename, "r") as f:
		for row in f:
			qa = row.rstrip().split("|")
			if((qa[0], qa[1]) in q_i_dic_for_ans):
				q_i_dic_for_ans[(qa[0], qa[1])].append(qa[2])
			else:
				q_i_dic_for_ans[(qa[0], qa[1])] = [qa[2]]
	return q_i_dic_for_ans

# this function does not work for all the answers
def check_multi_answer(filename):
	# counter = 0
	sep_answer_list = []
	sep_answer_set = []
	with open(filename, "r") as f:
		for row in f:
			qa = row.rstrip().split("|")
			ans = qa[2]
			ans_ls = [a.strip() for a in ans.split(",")]
			ans_set = set([a.strip() for a in ans.split(",")])
			sep_answer_list.append(ans_ls)
			if(ans_set not in sep_answer_set):
				sep_answer_set.append(ans_set)
	return sep_answer_list, sep_answer_set

# check if there is any overlapping question in different categories
def check_ques_overlap(file1, file2):
	ques_1_set = set()
	with open(file1, "r") as f1:
		for row in f1:
			qa = row.rstrip().split("|")
			ques_1_set.add(qa[1])

	ques_2_set = set()
	with open(file2, "r") as f2:
		for row in f2:
			qa = row.rstrip().split("|")
			ques_2_set.add(qa[1])
	return ques_1_set.intersection(ques_2_set)

def check_ques_completeness():
	path = os.path.join(root_path, "data/train/All_QA_Pairs_train.txt")
	m_path = os.path.join(root_path, "data/train/QAPairsByCategory/C1_Modality_train.txt")
	p_path = os.path.join(root_path, "data/train/QAPairsByCategory/C2_Plane_train.txt")
	o_path = os.path.join(root_path, "data/train/QAPairsByCategory/C3_Organ_train.txt")
	a_path = os.path.join(root_path, "data/train/QAPairsByCategory/C4_Abnormality_train.txt")

	questions = load_ques2set(path)
	m_ques = load_ques2set(m_path)
	p_ques = load_ques2set(p_path)
	o_ques = load_ques2set(o_path)
	a_ques = load_ques2set(a_path)

	sub_ques = set()
	sub_ques.update(m_ques, p_ques, o_ques, a_ques)
	outside_ques = questions.difference(sub_ques)
	return outside_ques

def check_len(filename):
	q_len_num = {}
	a_len_num = {}
	exclude_ls = ["<break>", "<START>", "<END>", "<UNKNOWN>"]
	# exclude_ls = ["<START>", "<END>"]
	with open(filename, "r") as f:
		for row in f:
			qa = row.rstrip().split("|")
			# words_q = qa[1].split()
			words_q = VQADataProvider.text_to_list(qa[1])
			words_q = [word for word in words_q if word not in exclude_ls]
			if(len(words_q) in q_len_num):
				q_len_num[len(words_q)] += 1
			else:
				q_len_num[len(words_q)] = 1

			# words_a = qa[2].split()
			words_a = VQADataProvider.text_to_list(qa[2])
			words_a = [word for word in words_a if word not in exclude_ls]
			if(len(words_a) in a_len_num):
				a_len_num[len(words_a)] += 1
			else:
				a_len_num[len(words_a)] = 1
	return q_len_num, a_len_num

def check_img_type(folder):
	import os
	img_ls = os.listdir(folder)
	print("image number: ", len(img_ls))
	jpg_ls = [img for img in img_ls if img.endswith(".jpg")]
	print("jpg image number: ", len(jpg_ls))

def check_unique_ans_num():
	path = os.path.join(root_path, "data/train/All_QA_Pairs_train.txt")
	m_path = os.path.join(root_path, "data/train/QAPairsByCategory/C1_Modality_train.txt")
	p_path = os.path.join(root_path, "data/train/QAPairsByCategory/C2_Plane_train.txt")
	o_path = os.path.join(root_path, "data/train/QAPairsByCategory/C3_Organ_train.txt")
	a_path = os.path.join(root_path, "data/train/QAPairsByCategory/C4_Abnormality_train.txt")

	_, _, all_ans = load_all2set(path)
	_, _, m_ans = load_all2set(m_path)
	_, _, p_ans = load_all2set(p_path)
	_, _, o_ans = load_all2set(o_path)
	_, _, a_ans = load_all2set(a_path)

	sub_ans = set()
	sub_ans.update(m_ans, p_ans, o_ans, a_ans)
	outside_ans = all_ans.difference(sub_ans)

	sub_ans2 = set()
	sub_ans2.update(m_ans, p_ans, o_ans)

	return outside_ans, sub_ans2

def check_train_val_imgId_overlap():
	train_path = os.path.join(root_path, "data/train/train_ImageIDs.txt")
	val_path = os.path.join(root_path, "data/val/val_ImageIDs.txt")
	train_img_ids = []
	with open(train_path, "r") as f:
		for row in f:
			train_img_ids.append(row.strip())
	val_img_ids = []
	with open(val_path, "r") as f:
		for row in f:
			val_img_ids.append(row.strip())
	print("train+val images number: ", len(train_img_ids) + len(val_img_ids))
	comb_ids = train_img_ids + val_img_ids
	comb_ids = set(comb_ids)
	print("unique total images number: ", len(comb_ids))

if __name__ == "__main__":
	filename = os.path.join(root_path, "data/train_val/QAPairsByCategory/C4_Abnormality_train_val.txt")
	_, _, ans_set = load_all2set(filename)
	unq_words = set()
	for ans in ans_set:
		words = VQADataProvider.text_to_list(ans)
		for w in words:
			unq_words.add(w)
	print(len(unq_words))
	print(list(unq_words)[:5])
	print("<END>" in unq_words)

	# filename = "/Users/leishi/Desktop/Internship/vqa2019/ImageClef-2019-VQA-Med-Training/QAPairsByCategory/C4_Abnormality_train.txt"
	# all_ans, all_img, all_ques, all_q_i_pairs, all_a_a_pairs = check_raw_data(filename)
	# print("\nunique answer length: ", len(all_ans))
	# print("\nunique img length: ", len(all_img))
	# print("\nunique question length: ", len(all_ques))
	# # print("\nall answer")
	# # print(all_ans)
	# print("\nunique question-image pairs: ", len(all_q_i_pairs))
	# print("\nunique question-answer pairs: ", len(all_a_a_pairs))
	# # print(all_ans)

	# as_m = ["XR - Plain Film", "CT - noncontrast", "CT w/contrast (IV)", "CT - GI & IV Contrast", "CTA - CT Angiography",
	# "CT - GI Contrast", "CT - Myelogram", "Tomography", "MR - T1W w/Gadolinium", "MR - T1W - noncontrast", "MR - T2 weighted",
	# "MR - FLAIR", "MR - T1W w/Gd (fat suppressed)", "MR T2* gradient,GRE,MPGR,SWAN,SWI", "MR - DWI Diffusion Weighted",
	# "MRA - MR Angiography/Venography", "MR - Other Pulse Seq.", "MR - ADC Map (App Diff Coeff)", "MR - PDW Proton Density",
	# "MR - STIR", "MR - FIESTA", "MR - FLAIR w/Gd", "MR - T1W SPGR", "MR - T2 FLAIR w/Contrast", "MR T2* gradient GRE", "US - Ultrasound",
	# "US-D - Doppler Ultrasound", "Mammograph", "BAS - Barium Swallow", "UGI - Upper GI", "BE - Barium Enema", "SBFT - Small Bowel",
	# "AN - Angiogram", "Venogram", "NM - Nuclear Medicine", "PET - Positron Emission"]

	# as_p = ["Axial", "Sagittal", "Coronal", "AP", "Lateral", "Frontal", "PA", "Transverse", "Oblique", "Longitudinal", "Decubitus",
	# "3D Reconstruction", "Mammo - MLO", "Mammo - CC", "Mammo - Mag CC", "Mammo - XCC"]

	# as_o = ["Breast", "Skull and Contents", "Face, sinuses, and neck", "Spine and contents", "Musculoskeletal", "Heart and great vessels",
	# "Lung, mediastinum, pleura", "Gastrointestinal", "Genitourinary", "Vascular and lymphatic"]

	# # as_all = as_m + as_p + as_o

	# as_m = [a.lower() for a in as_m]
	# as_p = [a.lower() for a in as_p]
	# as_o = [a.lower() for a in as_o]
	# # as_all = [a.lower() for a in as_all]

	# filename = "/home/leishi/vqa2019/data/train/QAPairsByCategory/C3_Organ_train.txt"

	# in_data_not_readme, in_readme_not_data = check_raw_readme(filename, as_o)
	# print("\ndifference between answer of raw data and answer in readme")
	# print(in_data_not_readme)
	# print("\ndifference between answer in readme and answer of raw data")
	# print(in_readme_not_data)

	# q_i_dic_for_ans = check_q_i_a_pair(filename)
	# num_list = []
	# for key in q_i_dic_for_ans:
	# 	num_list.append(len(q_i_dic_for_ans[key]))
	# # print("\n# of answers for each question-image pair")
	# print(max(num_list), min(num_list))

	# sep_answer_list, sep_answer_set = check_multi_answer(filename)
	# # print("# of separated answers for each question-image pair")
	# # print([len(a) for a in sep_answer_list])
	# x0 = [1 for a in sep_answer_list if len(a)>1]
	# print("\n# of q-i pairs that have multiple answers")
	# print(sum(x0))
	# x1 = [1 for b in sep_answer_set if len(b)>3]
	# print("\n# of unique answer which have more than one sub answers")
	# print(sum(x1))
	# print("answers that have multiple sub answers")
	# print([b for b in sep_answer_set if len(b)>3])

	# file1 = "/Users/leishi/Desktop/Internship/vqa2019/ImageClef-2019-VQA-Med-Training/QAPairsByCategory/C4_Abnormality_train.txt"
	# file2 = "/Users/leishi/Desktop/Internship/vqa2019/ImageClef-2019-VQA-Med-Training/QAPairsByCategory/C3_Organ_train.txt"
	# overlap = check_ques_overlap(file1, file2)
	# print("\n# of overlappling questions")
	# print(overlap)

	# diff_ques = check_ques_completeness()
	# print("# of uncovered questions: ", len(diff_ques))
	# print(diff_ques)

	# filename = "/home/leishi/vqa2019/data/train/QAPairsByCategory/C4_Abnormality_train.txt"
	# filename = "/home/leishi/vqa2019/data/train/All_QA_Pairs_train.txt"
	# q_len_num, a_len_num = check_len(filename)
	# print("\nquestion length")
	# # # for k, v in sorted(q_len_num.items(), key=lambda item: item[1]):
	# # # 	print("%d: %d" % (k, v))
	# for k in sorted(q_len_num.keys()):
	# 	print("%d: %d" % (k, q_len_num[k]))
	# print("\nanswer length")
	# # # for k, v in sorted(a_len_num.items(), key=lambda item: item[1]):
	# # # 	print("%d: %d" % (k, v))
	# for k in sorted(a_len_num.keys()):
	# 	print("%d: %d" % (k, a_len_num[k]))

	# # number of classes in each category (first 3 categories)
	# filename = "/home/leishi/vqa2019/data/train/QAPairsByCategory/C3_Organ_train.txt"
	# ans_set, _, _, _, _ = check_raw_data(filename)
	# print(len(ans_set))
	# print(ans_set)

	# folder = "/home/leishi/vqa2019/data/%s/%s_images" % ("val", "val")
	# check_img_type(folder)

	# # check answer completeness
	# outside_ans, sub_ans2 = check_unique_ans_num()
	# print("\noutside answers")
	# print(outside_ans)
	# print("number of unique ansers in first 3 categories: ", len(sub_ans2))

	# # check if train and val data have overlap img ids
	# check_train_val_imgId_overlap()


import requests
from pprint import pprint
class DataManager:
    def __init__(self):
        self.getresponse = requests.get(url="https://api.sheety.co/5843be2185b02f97b1f6fc8d2a584dfa/flightDeals/prices")
        self.length = len(self.getresponse.json()['prices'])
        self.prices = self.getresponse.json()['prices']
        self.putresponse = ""
        self.body = {}

    def put(self, sheet_data):
        for i in range(len(sheet_data)):
            self.body = {
                'price': {
                    "iataCode": f"{sheet_data[i]['iataCode']}"
                }
            }
            self.putresponse = requests.put(
                url=f"https://api.sheety.co/5843be2185b02f97b1f6fc8d2a584dfa/flightDeals/prices/{sheet_data[i]['id']}",
                json=self.body)


import sys
from platform_config import (
    PlatformConfig,
    python_arch,
)

cross_config_contents = """\
# For more information, see
# https://tttapa.github.io/py-build-cmake/Cross-compilation.html

arch = '{arch}'
toolchain_file = '{triple}.toolchain.cmake'
"""

def get_py_build_cmake_cross_config(cfg: PlatformConfig):
    subs = {
        "arch": python_arch(cfg),
        "triple": str(cfg),
    }
    return cross_config_contents.format(**subs)

if __name__ == "__main__":
    triple = sys.argv[1]
    outfile = sys.argv[2]
    cfg = PlatformConfig.from_string(triple)
    with open(outfile, "w") as f:
        f.write(get_py_build_cmake_cross_config(cfg))


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

import os
import time
import logging
import datetime
import multiprocessing

from queue import Empty
from ipaddress import ip_address

from datalog_http_monitoring.consumers_feeder import ConsumersFeeder

logger = logging.getLogger(__name__)

class LogCollector(ConsumersFeeder):
    def __init__(self, log_files):
        super(LogCollector, self).__init__()

        self.log_files = log_files

        # check that file can be read early
        for log_file in log_files:
            assert os.path.exists(log_file), f"Log file {log_file!r} must exists"
            assert os.path.isfile(log_file), f"Log file {log_file!r} must be a file"
            assert os.access(log_file, os.R_OK), f"Log file {log_file!r} must be a readable"

        self.logs_queue = multiprocessing.Queue()
        self.watcher_process = multiprocessing.Process(
            name="LogWatcherProcess",
            target=self.watcher,
            args=(self.log_files, self.logs_queue),
            daemon=True
        )

    @staticmethod
    def watcher(log_files, logs_queue):
        try:
            logger.info(f"LogConsumerWatcher thread started on {log_files!r}")
            last_reads = {log_file: 0 for log_file in log_files}
            positions = {log_file: 0 for log_file in log_files}
            while True:
                # avoid using `stat` too frequently
                time.sleep(0.1)

                for log_file in log_files:
                    try:
                        last_reads[log_file], positions[log_file] = LogCollector.read_logs(
                            log_file, logs_queue, last_reads[log_file], positions[log_file])
                    except AssertionError:
                        # file has not changed since last read
                        pass
                    except IOError as err:
                        logger.error(f"Unable to read {log_file!r}", exc_info=err)
                    except Exception as err:
                        logger.error(f"Unable to collect {log_file!r} logs", exc_info=err)

        except (KeyboardInterrupt, SystemExit):
            pass

    @staticmethod
    def read_logs(log_file, logs_queue, last_read, position):
        # see if log file has changed
        stats = os.stat(log_file)
        has_changed = stats.st_mtime > last_read or stats.st_size > position
        assert has_changed, "No changes"
        last_read = stats.st_mtime

        # read new log lines
        # logger.debug(f"Detected new content in {log_file}")
        with open(log_file, "r", encoding="utf-8") as fd:
            fd.seek(position)
            while True:
                line = fd.readline()
                if not line:
                    # logger.debug(f"Finish reading logs in {log_file}")
                    break
                log = Log.from_string(line)
                if log:
                    logs_queue.put(log)
                    # note: we only move position once we successfully read
                    # a log line, that means that if we have garbage or incomplete
                    # line, we will retry reading until we got a log
                    position = fd.tell()

        return last_read, position

    def __iter__(self):
        while True:
            try:
                yield self.logs_queue.get(block=True, timeout=.5)
            except ValueError:
                # ignore semaphore release bug from Queue when debugging
                pass
            except Empty:
                yield EmptyLog()

    def run(self):
        self.watcher_process.start()
        for log in self:
            self.feed_consumers(log)

class Log(object):
    def __init__(self, ip, user, date, method, path, status_code, size):
        self.ip = ip
        self.user = user
        self.date = date
        self.method = method
        self.path = path
        self.status_code = status_code
        self.size = size

    @staticmethod
    def from_string(line):
        try:
            # 127.0.0.1 - james [09/May/2018:16:00:39 +0000] "GET /report HTTP/1.0" 200 123
            ip, _, user, date_str, tz_str, method, path, _, status_str, bytes_str, *other = line.split()
            return Log(
                ip=ip_address(ip),
                user=user if user != '-' else None,
                date=datetime.datetime.strptime(f"{date_str} {tz_str}", "[%d/%b/%Y:%H:%M:%S %z]"),
                method=method[1:],
                path=path,
                status_code=int(status_str),
                size=int(bytes_str),
            )
        except ValueError as err:
            logger.debug(f"Unable to parse log line {line!r} (reason: {err})")

class EmptyLog(Log):
    def __init__(self, date=None):
        if not date:
            date = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
        super(EmptyLog, self).__init__(
            ip=None, user=None, method=None, path=None, status_code=None, size=None, date=date,)


def MRL(sample, alpha): #MRL function
    import numpy as np
    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.stats import norm
    import seaborn as sns; sns.set(style = 'whitegrid')
    from scipy.stats import genpareto
    import pandas as pd
    import math as mt
    import scipy.special as sm
    #Defining the threshold array and its step
    step = np.quantile(sample, .995)/60
    threshold = np.arange(0, max(sample), step=step)
    z_inverse = norm.ppf(1-(alpha/2))

    #Initialization of arrays
    mrl_array = [] #mean of excesses intialization
    CImrl = [] #confidence interval for the excesses initialization

    #First Loop for getting the mean residual life for each threshold value and the
    #second one getting the confidence intervals for the plot
    for u in threshold:
        excess = [] #initialization of the excesses array for each loop
        for data in sample:
            if data > u:
                excess.append(data - u) #adding excesses to the excesses array
        mrl_array.append(np.mean(excess)) #adding the mean of the excesses in the mean excesses array
        std_loop = np.std(excess) #getting standard deviation in the loop
        CImrl.append(z_inverse*std_loop/(len(excess)**0.5)) #getting confidence interval

    CI_Low = [] #initialization of the low confidence interval array
    CI_High = [] #initialization of the high confidence interval array

    #Loop to add in the confidence interval to the plot arrays
    for i in range(0, len(mrl_array)):
        CI_Low.append(mrl_array[i] - CImrl[i])
        CI_High.append(mrl_array[i] + CImrl[i])

    #Plot MRL
    plt.figure(1)
    sns.lineplot(x = threshold, y = mrl_array)
    plt.fill_between(threshold, CI_Low, CI_High, alpha = 0.4)
    plt.xlabel('u')
    plt.ylabel('Mean Excesses')
    plt.title('Mean Residual Life Plot')
    plt.show()


from Tablas import *
from Constantes import *
import math
import os

"""
    <summary>
        Funcion menu que realiza la prueba de hipotesis de una diferencia de medias.
    </summary>
    <param name="x_bar1">Media de la población 1.</param>
    <param name="x_bar2">Media de la población 2.</param>
    <param name="sigma1">Desviación estándar de la población 1.</param>
    <param name="sigma2">Desviación estándar de la población 2.</param>
    <param name="n1">Numero de ensayos en la población 1.</param>
    <param name="n2">Numero de ensayos en la población 2.</param>
    <param name="delta">Diferencia proporcional que existe entre las medias.</param>
    <param name="alpha">Nivel de significacia.</param>
    <param name="z">Valor del estadistico de prueba.</param>
"""
def menu_diferencia_medias():
    print("Elige la prueba de hipotesis a utilizar:")
    print("1.")
    print("H0. " + Smu + "1 - " + Smu + "2" + " = 0")
    print("H1. " + Smu + "1 - " + Smu + "2" + " < 0")
    print("2.")
    print("H0. " + Smu + "1 - " + Smu + "2" + " = 0")
    print("H1. " + Smu + "1 - " + Smu + "2" + " > 0")
    print("3.")
    print("H0. " + Smu + "1 - " + Smu + "2" + " = 0")
    print("H1. " + Smu + "1 - " + Smu + "2" + " != 0")
    opcion = input("Opcion: ")
    os.system("cls")

    if opcion not in ["1", "2", "3"]:
        print("Opcion incorrecta, vuelve a intentarlo.")
        print("Presiona una tecla para continuar...")
        os.system("pause")
        os.system("cls")
        menu_diferencia_medias()
        return

    print("Prueba de hipotesis de una diferencia de medias")
    print("H0. " + Smu + "1 - " + Smu + "2" + " = 0")
    print("H1. " + Smu + "1 - " + Smu + "2" + " " + ["<", ">", "!="][int(opcion) - 1] + " 0")
    print("")
    print("Introduce los datos:")
    x_bar1 = float(input(Sxbar + "1: "))
    x_bar2 = float(input(Sxbar + "2: "))
    delta = float(input(Sdelta + ": "))
    sigma1 = float(input(Ssigma + "1: "))
    sigma2 = float(input(Ssigma + "2: "))
    n1 = int(input("n1: "))
    n2 = int(input("n2: "))
    alpha = float(input(Salpha + ": "))
    if opcion=="3":
        alpha = 0.5 - alpha/2
    else:
        alpha = 0.5 - alpha
    z = (x_bar1 - x_bar2 - delta) / (math.sqrt((sigma1**2 / n1) + (sigma2**2 / n2)))
    prueba(z, alpha, opcion)
    os.system("cls")

"""
    <summary>
        Funcion que rechaza o no rechaza H0.
    </summary>
    <param name="z">Valor de la prueba.</param>
    <param name="alpha">Valor de alpha.</param>
    <param name="opcion">Opcion de la prueba.</param>
"""
def prueba(z, alpha, opcion):
    if opcion == "1":
        if z < -buscarZ(alpha):
            print("Se rechaza H0.")
        else:
            print("No se rechaza H0.")
    elif opcion == "2":
        if z > buscarZ(alpha):
            print("Se rechaza H0.")
        else:
            print("No se rechaza H0.")
    elif opcion == "3":
        if z < -buscarZ(alpha) or z > buscarZ(alpha):
            print("Se rechaza H0.")
        else:
            print("No se rechaza H0.")
    print("Presiona una tecla para continuar...")
    os.system("pause")
    os.system("cls")
    return


import logging
import multiprocessing
import os
import signal
import threading
from typing import List

from unicorn.server import Server

spawn: multiprocessing.context.SpawnContext = multiprocessing.get_context("spawn")

logger = logging.getLogger('unicorn')

def run_worker(app: str, host: str, port: int):
    """ Initializes and runs a worker process with the given application server details.
    :param app: ASGI callable for the application.
    :param host: IP address on which the application server will run.
    :param port: Port on which the application server will listen for requests.
      """
    logger.info(f'Initializing worker process [{os.getpid()}]')
    Server(app=app, host=host, port=port).run()

class Manager:
    """
    This manager class is responsible for spawning worker process to handle requests of the application server.
    All the worker process can handle requests received at the defined host and port.
    Load balancing across different worker process is done by the TCP kernel.
    Request will be picked by each worker, on accept() system call, which returns a file descriptor of the connection.
    Data is only read on the  recv() system call on the connection
    """

    def __init__(self, app: str, workers: int = 1, host: str = '0.0.0.0', port: int = 8000):
        """
        Initializes the Manager class
        :param app: ASGI callable
        :param workers: Number of workers to spawn which will handle the request to application server.
        :param host: IP address on where the application server will be run
        :param port: Port on which application server will listen for request. Default to 8000.
        """
        self.app = app
        self.host = host
        self.port = port
        self.workers = workers
        self.should_wait: threading.Event = threading.Event()
        self.processes: List[spawn.Process] = []
        self.pid = os.getpid()

    def on_interrupt(self, sig, _):
        logger.info(f'Received interrupt={sig} on manager.')
        self.should_wait.set()

    def run(self):
        """Starts the Manager and spawns the required number of worker processes.

        Sets up signal listeners for interruption. After spawning the worker processes,
        it blocks the main process until an interrupt is received. Upon interruption,
        it terminates all the worker processes.
        """
        logger.info(f'Starting manager process [{os.getpid()}]')
        # Setup listeners for signal
        for sig in [signal.SIGINT, signal.SIGTERM]:
            signal.signal(sig, self.on_interrupt)

        # Spawns the required number of worker processes
        for idx in range(self.workers):
            process = spawn.Process(target=run_worker, kwargs={
                'app': self.app,
                'host': self.host,
                'port': self.port
            })
            process.start()
            self.processes.append(process)

        # Block the execution on this process till an interrupt is received
        self.should_wait.wait()

        # Terminate all the worker processes, when an interrupt is received
        for process in self.processes:
            process.terminate()
            process.join()
        logger.info(f'Closing manager process [{os.getpid()}]')


import logging
from datetime import datetime, timedelta
from dateutil import parser
import pytz
from babel.dates import format_date, format_time
from icalendar import vDatetime
import time

import airbnb
from airbnb_automation.gcalendar import GCalendar
from airbnb_automation.notifications import Notifications
from airbnb_automation.nuki import Nuki

LOGGER = logging.getLogger('airbnb_automation')

class Airbnb():
    def __init__(self, config):
        self.config = config
        self.debug = config['debug']
        self.notifications = Notifications(config)
        self.cal = GCalendar(config)
        self.nuki = Nuki(config)

    def daily_nuki_opener_status(self):
        message = 'Nuki Opener Mode: {}\n'.format(self.nuki.opener_mode())
        self.notifications.send_telegram_private(message)

    def weekly_nuki_battery_status(self):
        message = 'Nuki Battery Critical: {}'.format(
            self.nuki.battery_critical())
        self.notifications.send_telegram_private(message)

    def weekly_messages(self):
        messages = []

        events = self.cal.get_public_calendar()
        for event in events:
            start = parser.parse(event['start']['dateTime'])
            end = parser.parse(event['end']['dateTime'])
            now = datetime.now().astimezone(pytz.utc)
            if now > start:
                if end < now + timedelta(days=7):
                    messages.append("{} odjíždí {}. ".format(
                        event['summary'],
                        format_date(end, 'EEE d MMM', locale='cs_CZ')))
            elif start < now + timedelta(days=7):
                messages.append("{} přijíždí {} a odjíždí {}.".format(
                    event['summary'],
                    format_date(start, 'EEE d MMM', locale='cs_CZ'),
                    format_date(end, 'EEE d MMM', locale='cs_CZ')))

        if len(messages) == 0:
            # message_EN = "No visitors arriving or leaving this week"
            message = "Tento týden nepřijíždí ani neodjíždí žádní hosté."
        else:
            message = "Hosté tento týden: "
            message += " ".join(messages)

        # notifications.send_sms(config['cleaner_number'], message)
        self.notifications.send_telegram_public(message)

    def cleaning_reminder(self):
        events = self.cal.get_public_calendar()
        check_out = None
        check_in = None
        difference = timedelta(days=60)

        for event in events:
            if 'dateTime' not in event['start'].keys():
                continue
            start = parser.parse(event['start']['dateTime'])
            end = parser.parse(event['end']['dateTime']).replace(hour=0)
            now = datetime.now().astimezone(pytz.utc)
            # Check out day is not today, and is less than 1 day away
            if (end > now) and (end < now + timedelta(days=1)):
                check_out = event
            else:
                if (start - now) < difference:
                    difference = start - now
                    check_in = event

        if check_out:
            message = "Připomenutí: {} odjíždí zítra v {}.".format(
                check_out['summary'],
                format_time(
                    parser.parse(check_out['end']['dateTime']),
                    'H:mm',
                    locale='cs_CZ'))
            if check_in:
                check_in_time = parser.parse(check_in['start']['dateTime'])
                message += " Příští host ({}) přijíždí {}".format(
                    check_in['summary'],
                    format_date(check_in_time, 'EEE d MMM', locale='cs_CZ'))

            # notifications.send_sms(config['cleaner_number'], message)
            self.notifications.send_telegram_public(message)

    def check_in_time(self, check_in_date):
        date = datetime.strptime(check_in_date, '%Y-%m-%d')
        check_in_time = pytz.timezone('Europe/Prague').localize(date).replace(
            hour=15).astimezone(pytz.utc)
        return check_in_time.isoformat()

    def check_out_time(self, check_out_date):
        date = datetime.strptime(check_out_date, '%Y-%m-%d')
        check_out_time = pytz.timezone('Europe/Prague').localize(date).replace(
            hour=12).astimezone(pytz.utc)
        return check_out_time.isoformat()

    def get_reservations(self):
        airbnb_api = airbnb.Api(access_token=self.config['airbnb_oauth_token'])
        calendar = airbnb_api.get_listing_calendar(
            self.config['airbnb_listing_id'], calendar_months=6)
        reservations = []

        for day in calendar['calendar']['days']:
            if not day['available'] and day['reservation']:
                reservation = day['reservation']
                event = {
                    'summary': reservation['guest']['full_name'],
                    'check_in': self.check_in_time(reservation['start_date']),
                    'check_out': self.check_out_time(reservation['end_date']),
                }
                reservations.append(event)

        reservations = list({v['summary']: v for v in reservations}.values())

        return reservations

    def calendar_sync(self):
        reservations = self.get_reservations()
        public_events = self.cal.get_public_calendar()

        for event in reservations:
            matching_public_events = [
                d for d in public_events if d['summary'] == event['summary']
            ]
            if len(matching_public_events) == 0:
                LOGGER.info(
                    "Public calendar entry for {} not found, creating".format(
                        event['summary']))
                self.cal.create_public_event(event)

                message = "Synced entry for {} to public calendar".format(
                    event['summary'])
                self.notifications.send_telegram_private(message)

                # Send message for cleaner to see bookings
                start_date = parser.parse(event['check_in'])
                end_date = parser.parse(event['check_out'])
                message_public = "Nová rezervace potvrzena: {} {} až {}".format(
                    event['summary'], format_date(start_date, locale='cs_CZ'),
                    format_date(end_date, locale='cs_CZ'))
                self.notifications.send_telegram_public(message_public)

            # else:
            #     LOGGER.info(
            #         "Public calendar entry for {} already exists".format(
            #             event['summary']))

    def nuki_sync(self):
        # Enable nuki continuous mode on check in day
        events = self.cal.get_public_calendar()
        check_in_day = False
        for event in events:
            if 'dateTime' in event['start'].keys():
                if datetime.today().date() == parser.parse(
                        event['start']['dateTime']).date():
                    check_in_day = True
                    message = "{} is checking in today, setting Nuki to continuous mode".format(
                        event['summary'])
                    LOGGER.info(message)
                    self.notifications.send_telegram_private(message)

                    count = 0
                    while count <= 5:
                        self.nuki.set_opener_mode('continuous')
                        time.sleep(
                            10
                        )
                        if self.nuki.check_opener_mode('continuous'):
                            break
                        else:
                            count += 1

                    if not self.nuki.check_opener_mode('continuous'):
                        self.notifications.send_telegram_private('PROBLEM: NUKI NOT SET TO CONTINUOUS')

        # Disable nuki continuous mode on non-check in days
        if not check_in_day:

            nuki_mode = self.nuki.opener_mode()
            count = 0
            if nuki_mode == 'continuous':
                message = "Nuki is in continuous node. Setting to disabled"
                self.notifications.send_telegram_private(message)
                while count <= 5:
                    self.nuki.set_opener_mode('disabled')
                    time.sleep(10) # allow opener mode to change before any reporting on this happens
                    if self.nuki.check_opener_mode('door'):
                        break
                    else:
                        count += 1

                if not self.nuki.check_opener_mode('door'):
                    self.notifications.send_telegram_private('PROBLEM: nuki continuous mode not disabled')

    def nuki_notifications_from_log(self):
        logs = self.nuki.log()
        for log in logs:
            log_time = parser.parse(log['date'])
            cz_log_time = log_time.astimezone(pytz.timezone('Europe/Prague'))
            if log_time + timedelta(minutes=5) > datetime.now().astimezone(
                    pytz.utc):

                # Check ring to open or continuous mode enabled
                if log['openerLog']['activeCm'] or log['openerLog']['activeRto']:
                    message = "Nuki opened door at {:d}:{:02d} for ring to open".format(cz_log_time.hour, cz_log_time.minute)
                else:
                    # Swipe
                    if log['action'] == 3:
                        message = "Nuki opened door at {:d}:{:02d} for {} due to swipe".format(cz_log_time.hour, cz_log_time.minute, log['name'])
                    # Ring but didn't open
                    else:
                        message = "Someone rung the bell at {:d}:{:02d} but Nuki did not open".format(cz_log_time.hour, cz_log_time.minute)

                self.notifications.send_telegram_private(message)
            else:
                break


import sys
import os
import time

from awasu_tools.utils import safe_xml, pretty_xml
from awasu_tools.log import log_msg, log_raw_msg

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

class Feed:
    """Container for a feed and its items."""

    def __init__( self, title, home_url, description=None, image_url=None, updated_time=None, extra_args=None ):
        """Initialize the Feed."""
        self.title = title
        self.home_url = home_url
        self.description = description
        self.image_url = image_url
        self.updated_time = updated_time if updated_time else time.time()
        self.extra_args = extra_args
        self.feed_items = []

    def get_xml( self, templ=None, log=None ):
        """Generate the feed XML."""
        # initialize
        if templ:
            assert isinstance( templ, str )
        else:
            # use the default template
            templ = """<?xml version="1.0" encoding="UTF-8"?>""" "\n" \
                    """<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xh="http://www.w3.org/1999/xhtml">""" "\n" \
                    """<title type="text">{title}</title>""" "\n" \
                    """<subtitle type="html">{description}</subtitle>""" "\n" \
                    """<link href="{url}" />""" "\n" \
                    """<logo>{image_url}</logo>""" "\n" \
                    """<updated>{updated_time}</updated>""" "\n" \
                    """{feed_items}""" "\n" \
                    """</feed>"""
        # generate the feed XML
        args = {
            "title": self.title,
            "description": self.description,
            "url": self.home_url,
            "image_url": self.image_url,
            "updated_time": _format_time( self.updated_time ),
        }
        if self.image_url and not self.image_url.startswith( ( "http://", "https://", "file://" ) ):
            # NOTE: We assume image_url has been set to point to a file in our directory.
            ### IMPORTANT CAVEAT ###
            # If the channel image is accessed as a local file, it won't show properly
            # because of CORS (although it seems to work if the channel is opened in Awasu) :-/
            # Things will also definitely not work if the channel summary page is loaded
            # from another computer, via a remote Awasu API call, since the other computer
            # obviously won't have access to file on the local computer. We can live with this
            # for now; if it's really a problem for someone, they can copy the logo file
            # somewhere accessible, and override the image URL in the feed template.
            # FUDGE! The directory name could contain non-ASCII characters!
            dname = os.path.split( sys.argv[0] )[0].decode( "windows-1252" )
            args["image_url"] = "file:///" + os.path.join( dname, self.image_url )
        if self.extra_args:
            args.update( self.extra_args )
        args = {
            k: safe_xml( v ) if v is not None else ""
            for k, v in args.items()
        }
        args["feed_items"] = "\n".join(
            fi.get_xml(log) for fi in self.feed_items
        )
        if not self.feed_items:
            # tidy up the output if there are no feed items
            pos = templ.find( "{feed_items}\n" )
            if pos >= 0:
                templ = templ[:pos] + templ[pos+13:]
        return templ.format( **args )

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

class FeedItem:
    """Container for a feed item."""

    def __init__( self, title, url, content=None, updated_time=None, templ=None, extra_args=None ):
        """Initialize the FeedItem."""
        self.title = title
        self.url = url
        self.content = content
        self.updated_time = updated_time
        self.templ = templ
        self.extra_args = extra_args

    def get_xml( self, log=None ):
        """Generate the feed item XML."""
        # initialize
        if self.templ:
            assert isinstance( self.templ, str )
            templ = self.templ
        else:
            # use the default template
            templ = """<entry>""" \
                    """<title type="text">{title}</title>""" \
                    """<link href="{url}" />""" \
                    """<updated>{updated_time}</updated>""" \
                    """<content type="html">{content}</content>""" \
                    """</entry>"""
        # prepare to generate the feed item XML
        args = {
            "title": self.title,
            "url": self.url,
            "updated_time": _format_time( self.updated_time ),
            "content": self.content,
        }
        if self.extra_args:
            args.update( self.extra_args )
        if log:
            log_msg( "" )
            log_msg( "Generating feed item..." )
            for key, val in args.items():
                log_msg( "- {} = {}", key, "" if val is None else val )
        # generate the feed item XML
        args = {
            k: safe_xml( v ) if v is not None else ""
            for k, v in args.items()
        }
        buf = templ.format( **args )
        if log:
            log_msg( "Generated feed item XML:" )
            log_raw_msg( pretty_xml( buf ) )
        return buf

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

def _format_time( val ):
    """Format a time value for insertion into a feed."""
    if isinstance( val, float ):
        return time.strftime( "%Y-%m-%dT%H:%M:%SZ", time.gmtime( val ) )
    else:
        return val

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

if __name__ == "__main__":
    # a simple example
    feed = Feed( "An example feed", "http://test.com" )
    feed.feed_items.append(
        FeedItem( "Feed item 1", "http://test.com/item1", "This is feed item #1" )
    )
    feed.feed_items.append(
        FeedItem( "Feed item 2", "http://test.com/item2", "This is feed item #2", time.time() )
    )
    print( feed.get_xml() )


# pip install opencv-python --> 이미지및 영상처리를 하기 위한 라이브러리 버전 4.6

import numpy as np  #
import cv2  # 이미지 및 영상처리를 하기위한 라이브러리

face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
eye_casecade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")

ff = np.fromfile(r"샘플이미지.jpg", np.uint8)
img = cv2.imdecode(ff, cv2.IMREAD_UNCHANGED)
img = cv2.resize(img, dsize=(0, 0), fx=1.0, fy=1.0, interpolation=cv2.INTER_LINEAR)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.2, 10)

for (x, y, w, h) in faces:
    face_img = img[y : y + h, x : x + w]
    # 찾아낸 얼굴 이미지 크기만큼만 가지고옴
    face_img = cv2.resize(face_img, dsize=(0, 0), fx=0.15, fy=0.15)
    # 가져온 이미지를 기존 사이즈에 0.15 배만큼 크기를 줄임
    face_img = cv2.resize(face_img, (w, h), interpolation=cv2.INTER_AREA)
    # 다시 원래 사리즈로 늘림
    img[y : y + h, x : x + w] = face_img
    # 해당 이미지 위치로 다시가서 변경된 이미지 치환
cv2.imshow("face find", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

얼굴모자이크처리/얼굴찾아모자이크.py


from django.db.models.fields import CharField, NullBooleanField
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.forms import fields
import vat_moss.id
from oscar_vat_moss.util import u

# The longest VAT IDs are currently 2-letter country code + 15
# characters. Make the max_length 32 to be on the safe side.
DEFAULT_MAX_LENGTH = 32

def vatin(verbose_name=_('VAT Identification Number (VATIN)'),
          name='vatin',
          verify_exists=None,
          blank=True,
          help_text=_('Required if you are associated with a business '
                      'registered for VAT in the European Union.')):
    """Convenience method to return a properly configured VATIN field."""
    return VATINField(verbose_name=verbose_name,
                      name=name,
                      verify_exists=verify_exists,
                      blank=blank,
                      help_text=help_text)

def digital_goods(verbose_name=_('Digital Goods?'),
                  name='digital_goods',
                  help_text=_("Indicates that products in this class are "
                              "Digital Goods per EU VAT regulations, "
                              "meaning the customer's location determines "
                              "the applicable VAT rate.")):
    return NullBooleanField(verbose_name=verbose_name,
                            name=name,
                            help_text=help_text)

class VATINField(CharField):
    def __init__(self, verbose_name=None, name=None,
                 verify_exists=None, **kwargs):
        kwargs['max_length'] = kwargs.get('max_length', DEFAULT_MAX_LENGTH)
        CharField.__init__(self, verbose_name, name, **kwargs)
        validator = VATINValidator(None)
        self.validators.append(validator)

    def formfield(self, **kwargs):
        # As with CharField, this will cause VATIN validation to be performed
        # twice.
        defaults = {
            'form_class': fields.CharField,
        }
        defaults.update(kwargs)
        return super(CharField, self).formfield(**defaults)

    def deconstruct(self):
        """
        deconstruct() is needed by Django's migration framework
        """
        name, path, args, kwargs = super(VATINField, self).deconstruct()
        # We have a default value for max_length; remove it in that case
        if self.max_length == DEFAULT_MAX_LENGTH:
            del kwargs['max_length']
        return name, path, args, kwargs

class VATINValidator(validators.BaseValidator):
    def __call__(self, value):
        """Verify that a VATIN is valid and exists"""
        return self.validate_vatin(value)

    def validate_vatin(self, value):
        """Validate a VATIN and check that it exists.

        :raises:
            ValidationError wrapping one of the following errors from
            vat_moss.id:
                ValueError - If the is not a string or is not in the
                format of two characters plus an identifier
                InvalidError - If the VAT ID is not valid
                WebServiceUnavailableError - If the VIES VAT ID
                service is unable to process the request - this is
                fairly common
                WebServiceError - If there was an error parsing the
                response from the server - usually this means
                something changed in the webservice
                urllib.error.URLError/urllib2.URLError - If there is
                an issue communicating with VIES or data.brreg.no
        """
        # vat_moss expects that all strings are unicode, so force it
        # here.
        try:
            vat_moss.id.validate(u(value))
        except Exception as e:
            raise ValidationError(_(str(e)))


import cv2
import numpy as np
import tensorflow as tf
from img2vec_pytorch import Img2Vec

def CalHistogram(img, bin=[8,8,8]):
    hist = cv2.calcHist([img],[0, 1, 2],None,[bin[0], bin[1], bin[2]],[0,256, 0, 256, 0, 256])
    hist = hist.reshape(1, -1)/ hist.sum()
    return hist

def split_image_to_tiles(img, rows=3, columns=3):
    height = img.shape[0]
    width = img.shape[1]
    h = height // rows
    w = width // columns
    tiles = []
    hindex = 0
    for i in range(0, height, h):
        windex = 0
        for j in range(0, width, w):
            addw = j + w if windex != columns - 1 else width
            addh = i + h if hindex != rows - 1 else height
            tile = img[i:addh, j:addw, :]
            tiles.append(tile)
            if windex == columns - 1:
                break
            windex += 1
        if hindex == rows - 1:
            break
        hindex += 1
    return tiles

def get_region_histogram(img, rows=3, columns=3):
    tiles = split_image_to_tiles(img, rows, columns)
    features = []
    for tile in tiles:
        histogram = CalHistogram(tile, [8,8,8])
        histogram = cv2.normalize(histogram, histogram).flatten()
        features.extend(histogram)
    features = np.array(features)
    features = features.reshape(1, -1)
    return features

def get_mobilenet(img):
    IMAGE_DIMS = (160, 160, 3)
    mnet_model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=IMAGE_DIMS,
                                               include_top=False, weights='imagenet')
    img=cv2.resize(img, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
    im_toarray = tf.keras.preprocessing.image.img_to_array(img)
    im_toarray = np.expand_dims(img, axis=0)
    im_toarray = tf.keras.applications.mobilenet.preprocess_input(im_toarray)
    data_stack = np.vstack([im_toarray])
    feature = mnet_model.predict(data_stack)
    return feature

def CalHistogram_RGB(img):
    features = []
    for channel in range(0, 3):
        hist = cv2.calcHist([img], [channel], None, [255], [1, 256])
        features.append(hist)
    features = np.array(features)
    res = features.reshape(1, features.shape[0] * features.shape[1] * features.shape[2])
    return res

def get_dominant_color(img, number_of_colors=5):
    nrow, ncol, nchl = img.shape
    g = img.reshape(nrow*ncol,nchl)

    flags = cv2.KMEANS_RANDOM_CENTERS
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
    # Add one more color in case dominant color is black
    _, labels, centroids = cv2.kmeans(np.float32(g), number_of_colors + 1, None, criteria, 10, flags)
    palette = np.uint8(centroids)
    return palette

def check_color(palette, number_of_colors=5):
    result = []
    for color in palette:
        if color[0] <= 32 and color[1] <= 32 and color[2] <= 32:
            continue
        result.append(color)
        if len(result) == number_of_colors:
            break
    result = np.array(result)
    while result.shape[0] < 5:
        result = np.append(result, [[0, 0, 0]], axis=0)
    result = result.reshape(1, -1)
    return result

def get_resnet_embedding(img):
    img2vec = Img2Vec(cuda=True)
    vec = img2vec.get_vec(img, tensor=True)
    vec = vec.reshape([-1, 1])
    vec = vec.detach().numpy()
    return vec


class _Link(object):
    def __init__(self,val):
        self.data=val
        self._next=None
    def gonext(self):
        return self._next
    def __str__(self):
        return str(self.data)

class LinkedList(object):
    def __init__(self,in_list=None):
        """
        params:
        =======
        head : optional initial data for linked list
        input_list : optional parameter.  if specified, list will be converted
                    to linked list
        """
        self.head=None
        self.tail=None
        if in_list is not None:
            for item in in_list:
                self.new_link(item)

    def new_link(self,val):
        newlink=_Link(val)
        if self.head == None:
            self.head=newlink
        if self.tail != None:
            self.tail._next = newlink
        self.tail=newlink

    def pop(self,index=0):
        prev=None
        curr=self.head
        i=0
        while curr!=None and i<index:
            prev=curr
            curr=curr._next
            i+=1
        if prev==None:
            self.head=curr._next
            return curr.data
        else:
            prev._next=curr._next
            return curr.data
    def __str__(self):
        curr=self.head
        s=''
        while curr:
            s+=str(curr.data)+'\n'
            curr=curr.gonext()
        return s


""" IMPORT MODULES AND LIBRARIES """

from functions.utils.prompt import prompt

# Import required libraries
import os
from simple_colors import yellow, blue, green, red, cyan
import sys
from tabulate import tabulate
import pickledb
# import ipaddress
import time
import datetime

""" PICKLEDB SETUP """
# db = pickledb.load('netsploit.db', False)

""" CONFIG """
# NOTE: Change the paths to to absolute paths if needed
# UPDATE: using `functions/utils/config.py` for storing configs

# username = os.popen('whoami').read()
# username = username.replace("\n", "")

# oui_file_path = f'/home/{username}/path/to/netsploit-py/oui.txt'  # noqa
# oui_file_path = 'oui.txt'

# logs_folder_path = f'/home/{username}/path/to/netsploit-py/logs/'  # noqa
# logs_folder_path = 'logs/'

""" STARTUP """
# Print ASCII art of "NetSploit"
header = """
     __     _   __       _       _ _
  /\ \ \___| |_/ _\_ __ | | ___ (_) |_
 /  \/ / _ \ __\ \| '_ \| |/ _ \| | __|
/ /\  /  __/ |__\ \ |_) | | (_) | | |_
\_\ \/ \___|\__\__/ .__/|_|\___/|_|\__|
                  |_|
"""  # noqa
print(blue(header, 'bold'))

# Print init messages
print(yellow(' => netsploit v1.0', 'bold'))
print(yellow(' => Powered by nmap, ping and hping3', 'bold'))
print(green(' => 9 modules ready to use', 'bold'))
# print(blue(' => network-scanner - device-info - os-guesser - oui-lookup - \n      port-scanner - dos - ping - vuln-scanner - custom', 'bold'))  # noqa
print(blue(' => 5 Scanners, 3 Info, 1 Attack', 'bold'))  # noqa
# print(cyan(' => Happy Hacking! :)', 'bold'))

# Set variables for printing network configuration

gateway = os.popen("ip route show | grep -i 'default via'| awk '{print $3 }'").read()  # noqa
gateway = gateway.replace("\n", "")

up_interface = os.popen("route | awk '/Iface/{getline; print $8}'").read()
up_interface = up_interface.replace("\n", "")

# Get wireless network name (was used previously)
# n_name = os.popen('iwgetid -r').read()

# Work-around to get wireless network name on Fedora Linux:
n_name = os.popen('nmcli -t -f NAME connection show --active').read()

# Get network MAC address
n_mac = os.popen(
    "ip addr | grep 'state UP' -A1 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'").read()  # noqa
n_ip = os.popen("hostname -I").read()  # Local IP address
n_host = os.popen("hostname").read()  # hostname
# Print network configuration header

print(""" \033[1;36m
╒═════════════════════════════════════════════════════════════════════════╕
│                         Your Network Configuration                      │
╘═════════════════════════════════════════════════════════════════════════╛     \033[1;m""")  # noqa

# Print network configuration , using tabulate as table.

table = [["IP Address", "MAC Address", "Gateway", "Iface", "Hostname"],
         ["", "", "", "", ""],
         [n_ip, n_mac.upper(), gateway, up_interface, n_host]]
print(tabulate(table, stralign="center",
               tablefmt="fancy_grid", headers="firstrow"))
print()
print(f'{green("[+] Please type help to view commands", "bold")}')
# print()
# print(f'{cyan("[TIP]", "bold")} You can use the {yellow("auto", "bold")} command to automate your process!')  # noqa
print()

try:
    prompt()
except KeyboardInterrupt:
    print()
    exit()


import numpy as np

def compare(X_i,X_j):
    # Edge Case
    if len(X_i) != len(X_j):
        return False
    evaluate = True
    for k in range(len(X_i)):
        evaluate = evaluate and X_j[k]<X_i[k]
    return evaluate

def V(i,array):
    X = array[i]
    number,n = 0.,float(len(array)-1)
    for item in array:
        if compare(X,item):
            number += 1.
    return number/n

def V_avg(array):
    total,n = 0.,float(len(array))
    for i in range(int(n)):
        total += V(i,array)
    return total/n

def tau_n(array):
    dim = len(array[0])
    factor1,factor2 = float(2.**dim),float(2.**(dim-1)-1.)
    if dim == 3 and len(array)==8579:
        return (0.127348820425*factor1-1.)/factor2
        #return (0.140600039717*factor2-1.)/factor2
    elif dim == 6 and len(array)==2434:
        return (0.0456267407777*factor1-1.)/factor2
        #return (0.0547965677359*factor1-1.)/factor2
    else:
        return (V_avg(array)*factor1-1.)/factor2

def f(x,tau):
    x,tau = float(x),float(tau)
    x1,x2,x3 = x,x**2,x**3
    return 465.*(1-tau)*x3 +(330.-1426*tau)*x2+(60.-1116.*tau)*x1-248.*tau

def bisection(tau,n=1000):
    interval = [0.,100.]
    midpoint = 0.
    while n>0:
        midpoint = 0.5*(sum(interval))
        if f(midpoint,tau)*f(interval[1],tau)>0.:
            interval[1] = midpoint
        else:
            interval[0] = midpoint
        n -= 1
    return midpoint

import InputReader as IR

def theta_n(array):
    dim,tau,theta = len(array[0]),tau_n(array),-1.
    if dim == 3:
        theta = (2.*tau)/(1.-tau)
    elif dim == 6:
        theta = bisection(tau)
    if theta<0.:
        theta = 0.01
    return theta

def makeStep(array):
    return 1./float(len(array))

def makeRange(array):
    step = makeStep(array)
    return np.arange(0.,1.+step,step)

def K_n(array):
    n,K,y = float(len(array)),[],[]
    for i in range(len(array)):
        k = V(i,array)*((n-1.)/(n))
        K.append(k)
    for j in makeRange(array):
        total = 0.
        for item in K:
            if item <= j:
                total += 1.
        total /= n
        y.append(total)
    return y
"""
import Clayton3D as C3D
import Clayton6D as C6D
import matplotlib.pyplot as plt

single = IR.read('single.csv')
double = IR.read('double.csv')
plt.figure(0)
x11,y11 = makeRange(single),K_n(single)
x12= np.arange(0.,1.001,0.01)
y12 =C3D.CDF(x12,theta_n(single))
plt.plot(x11,y11,c='g',label='Empirisch',lw=1.6)
plt.plot(x12,y12,c='b',label='Theoretisch',lw=1.2)
plt.xlabel('u []', size=12.)
plt.ylabel('Cumulatieve kansdichtheid []', size=12.)
plt.legend(loc=4)
plt.xlim(0.,1.)
plt.ylim(0.,1.01)
plt.grid(True)
plt.figure(1)
x11,y11 = makeRange(double),K_n(double)
x12= np.arange(0.,1.001,0.01)
y12 =C6D.CDF(x12,theta_n(double))
plt.plot(x11,y11,c='g',label='Empirisch',lw=1.6)
plt.plot(x12,y12,c='b',label='Theoretisch',lw=1.2)
plt.xlabel('u []', size=12.)
plt.ylabel('Cumulatieve kansdichtheid []', size=12.)
plt.legend(loc=4)
plt.xlim(0.,1.)
plt.ylim(0.,1.01)
plt.grid(True)
plt.show()"""


import matplotlib.pyplot as plt
import numpy as np

dict_races = {}
dict_race_count_mr_sr = {}#list for each race - 0idx - count of SR, 1 idx - count of MR

count_mr = 0
# with open('pat_don_mr_high_geno_races.csv') as in_f:
with open('new_data_geno.txt') as in_f:
    for line in in_f:
        line = line.strip().split(',')
        race1 = line[2]
        race2 = line[3]

        races = [race1, race2]
        if race1 != race2:
            if race1 in dict_race_count_mr_sr:
                dict_race_count_mr_sr[race1][1] +=1
            else:
                dict_race_count_mr_sr[race1] = [0,1]

            if race2 in dict_race_count_mr_sr:
                dict_race_count_mr_sr[race2][1] += 1
            else:
                dict_race_count_mr_sr[race2] = [0, 1]

            count_mr +=1
        else:
            if race1 in dict_race_count_mr_sr:
                dict_race_count_mr_sr[race1][0] += 1
                #dict_race_count_mr_sr[race1] += 1
            else:
                dict_race_count_mr_sr[race1] = [1, 0]
                #dict_race_count_mr_sr[race1] = 1
        races = ('_').join(sorted(races))

        dict_races[races] = dict_races.get(races, 0) + 1

print(count_mr)
print(len(dict_races))

sorted_dict_races = sorted(dict_races.items(), key=lambda kv: kv[1], reverse=True)
sorted_dict = sorted(dict_race_count_mr_sr.items(), key=lambda kv: kv[1], reverse=True)
sum = 0

x, y = [], []
v1, v2 = [],[]

for key, val in sorted_dict:
    value = val[0]+val[1]
    x.append(key)
    y.append(value)
    v1.append(val[0])
    v2.append(val[1])

    print(key + '-  ' + str(val))
    sum += val[0]

# #mr_sr_plot
# plt.figure()
# plt.bar(x, v1, color='royalblue', label='sr')
# plt.bar(x, v2, color='silver',  label='mr', bottom=v1)
# plt.xticks(np.arange(len(sorted_dict)), x, rotation='vertical',fontsize=7)
# plt.yticks(fontsize=7)
# plt.ylabel('Number in category')
# plt.xlabel('Races')
# plt.title('Number of individuals per race')
# plt.legend()
# plt.tight_layout()
# plt.show()
# print(sum)

plt.figure()
plt.bar(x, y,color='royalblue', label='sr')
plt.xticks(np.arange(len(sorted_dict)), x, rotation='vertical',fontsize=7)
plt.yticks(fontsize=7)
plt.ylabel('Number in category')
plt.xlabel('Races')
plt.title('Number of individuals per race')
plt.legend()
plt.tight_layout()
plt.show()
print(sum)


capitals = {
    "France": "Paris",
    "Germany": "Berlin"
}

# Nesting a List in a Dictionary
cities = {
    "France": ["Paris", "Lille", "Dijon"],
    "Germany": ["Berlin", "Munich", "Dortmund"],
}

# Nesting a Dictionary in a Dictionary
travel_log = {
    "France": {
        "cities_visited": ["Paris", "Lille", "Dijon"],
        "total_visits": 12,
        "popular_sites": ["Eiffel Tower", "Stade de France", "Arc de Triomphe"],
    },
    "Germany": {
        "cities_visited": ["Berlin", "Munich", "Dortmund"],
        "total_visits": 26,
        "popular_sites": ["Bradenburg Gate", "Allianz Arena", "Signal Iduna Park"],
    }
}

# Nesting a Dictionary in a List
travel_log2 = [
    {
        "country": "France",
        "cities_visited": ["Paris", "Dijon", "Lille"],
        "total_visits": 23,
        "popular_sites": ["Eiffel Tower", "Stade de France", "Arc de Triomphe"],
    },
    {
        "country": "Germany",
        "cities_visited": ["Berlin", "Munich", "Dortmund"],
        "total_visits": 45,
        "popular_sites": ["Bradenburg Gate", "Allianz Arena", "Signal Iduna Park"],
    }
]


from turtle import Turtle, Screen

class Alien(Turtle):
    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        self.shapesize(stretch_wid=0.5, stretch_len=0.5)
        self.penup()
        self.fire = False
        self.speed("fastest")
    # sets fire to true if shots been fired
    def alien_shoot(self):
        self.fire = True

class Laser(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("square")
        self.color("#67c94d")
        self.shapesize(stretch_wid=0.1, stretch_len=1.5)
        self.penup()
        self.hideturtle()
        self.setheading(90)
        self.speed("fastest")

class Space_ship(Turtle):
    def __init__(self, laser: Turtle):
        super().__init__()
        self.shape("outfile.gif")
        self.penup()
        self.goto(0, -280)
        self.laser = laser
        self.laser.hideturtle()
        self.laser.penup()
        self.laser.goto(0, -280)
        self.fire = False
        self.speed("fastest")
    # sets fire to true if shots been fired
    def shoot(self):
        if self.laser.ycor() < -270:
            self.fire = True
    # move right
    def right(self):
        self.goto(self.xcor() + 10, self.ycor())
        if not self.fire:
            self.laser.goto(self.xcor(), self.ycor())
    # move left
    def left(self):
        self.goto(self.xcor() - 10, self.ycor())
        if not self.fire:
            self.laser.goto(self.xcor(), self.ycor())

def arrange_alien(screen):
    alien_list = []
    alien_gun_list = []
    x = -250
    y = 400
    for n in range(24):
        alien_list.append(Alien(screen))
        alien_gun_list.append(Laser())
        alien_list[n].goto(x, y)
        alien_list[n].shape("alien_t.gif")
        x += 60
        if x > 200:
            x = -250
            y -= 45
    return alien_list, alien_gun_list

# game physics
def alien_prop(alien_list: list, pace, laser: Turtle, space_ship: Turtle, alien_gun_list, obstacle_list,score,screen: Screen, dead_gun):
    import random
    edge = False
    hit = False
    track = 0
    # for the aliens movement
    for alien in alien_list:
        alien.goto(alien.xcor() + pace, alien.ycor())
        # checks if the aliens gun has been shot
        if not alien.fire:
            alien_gun_list[track].hideturtle()
            alien_gun_list[track].goto(alien.xcor() + pace, alien.ycor())
            # to increase the odds of firing
            probability = random.randint(0, 200 + round((len(alien_list)/24) * 300))
            # probality of an alien shooting is 1 of probability
            if probability == 5:
                alien.fire = True
        else:
            alien_gun_list[track].showturtle()
            alien_gun_list[track].backward(10)
        # returns laser  to the alien if it has passed a coordinate
        if alien_gun_list[track].ycor() < -300:
            alien_gun_list[track].hideturtle()
            alien_list[track].fire = False

        if alien.xcor() > 350 or alien.xcor() < -350:
            edge = True

        # checks if laser has hit an obstacle
        for i in obstacle_list:
            if alien_gun_list[track].distance(i) < i.dist:
               if alien_gun_list[track].xcor() + 2 >= i.xcor() >= alien_gun_list[track].xcor() or alien_gun_list[track].xcor() - 2 <= i.xcor() <= alien_gun_list[track].xcor():
                   if i.lenght > 1:
                       alien.fire = False
                       alien_gun_list[track].hideturtle()
                       i.lenght -= 1
                       i.dist = (50 * i.lenght) / 4.16
                       i.shapesize(0.1, i.lenght)
                       i.backward(i.rev_dist)
                       i.rev_dist += (i.lenght / 10) - 0.5 - 0.05
                       alien_gun_list[track].goto(alien.xcor(), alien.ycor())
                   else:
                       i.hideturtle()
                       obstacle_list.remove(i)

            # space ship laser hits an obstacle
            if laser.distance(i) < i.dist:
                if laser.xcor() + 2 >= i.xcor() >= laser.xcor() or laser.xcor() - 2 <= i.xcor() <= laser.xcor():
                    if i.lenght > 1:
                        i.lenght -= 1
                        i.dist = (50 * i.lenght) / 4.16
                        i.shapesize(0.1, i.lenght)
                        i.forward(i.rev_dist)
                        i.rev_dist += (i.lenght / 10) - 0.5 - 0.05
                        laser.hideturtle()
                        laser.goto(space_ship.xcor(), space_ship.ycor())
                        space_ship.fire = False
                    else:
                        i.hideturtle()
                        obstacle_list.remove(i)

        # checks if space ship has been hit
        if alien_gun_list[track].distance(space_ship) < 75:
            if alien_gun_list[track].xcor() + 25 >= space_ship.xcor() >= alien_gun_list[track].xcor() or alien_gun_list[track].xcor() - 25 <= space_ship.xcor() <= alien_gun_list[track].xcor():
                alien_gun_list[track].hideturtle()
                alien_gun_list[track].goto(alien.xcor(), alien.ycor())
                hit = True

        #checks if an alien has been hit
        if laser.distance(alien) < 25 and laser.ycor() < alien.ycor() < laser.ycor() + 25:
            score.score += 100
            score.add()
            alien.hideturtle()
            alien_list.remove(alien)
            dead_gun.append(alien_gun_list[track])
            alien_gun_list.pop(track)
            laser.hideturtle()
            laser.goto(space_ship.xcor(), space_ship.ycor())
            space_ship.fire = False
        # checks if alien laser collides with space ship laser
        elif alien_gun_list[track].distance(laser) < 35 and alien.fire:
            if alien_gun_list[track].xcor() + 3 >= laser.xcor() >= alien_gun_list[track].xcor() or alien_gun_list[track].xcor() - 3 <= laser.xcor() <= alien_gun_list[track].xcor():
                laser.hideturtle()
                laser.goto(space_ship.xcor(), space_ship.ycor())
                alien_gun_list[track].hideturtle()
                alien_gun_list[track].goto(alien.xcor(), alien.ycor())
                alien.fire = False
                space_ship.fire = False
        # to make sure the already shot laser of a dead alien continues moving
        try:
            dead_gun[track].forward(10)
            if dead_gun[track].ycor < -280:
                dead_gun[track].hideturtle()
                dead_gun[track].pop(track)
        except IndexError:
            pass
        except TypeError:
            pass

        track += 1

    return edge,hit

class Obstacle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("square")
        self.color("#67c94d")
        self.setheading(90)
        self.penup()
        self.lenght = 10
        self.shapesize(0.1, 10)
        self.speed("fastest")
        self.dist = 120
        self.rev_dist = 10

def obstacle_line(pos):
    obstacle_list = []
    width = 20
    x = pos
    y = -50
    for ver in range((width)):
        obstacle_list.append(Obstacle())
        obstacle_list[ver].goto(x, y)
        x += 5
    return obstacle_list

def arrange_obstacle():
    all_obstacle = []
    for i in range(-350, 350, 120):
        all_obstacle += obstacle_line(i)
    return all_obstacle

class Score(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.score=0
        self.goto(250,420)
        self.hideturtle()
        self.color("#67c94d")
        self.write(f"score:{self.score}",font=("Arial",24,"normal"))

    def add(self):
        self.clear()
        self.goto(250,420)
        self.write(f"score:{self.score}",font=("Arial",24,"normal"))

class Lives(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.lives=3
        self.goto(-300,420)
        self.hideturtle()
        self.color("#67c94d")
        self.write(f"lives:{self.lives}",font=("Arial",24,"normal"))

    def reduce_lives(self):
        self.clear()
        self.lives -= 1
        self.write(f"lives:{self.lives}",font=("Arial",24,"normal"))
    def update_(self):
        self.clear()
        self.write(f"lives:{self.lives}",font=("Arial",24,"normal"))


from rest_framework import serializers
from django.shortcuts import get_object_or_404
from rest_framework.validators import UniqueTogetherValidator
from rest_framework.serializers import ValidationError

from posts.models import Follow, Group, Comment, Post, User

class FollowSerializer(serializers.ModelSerializer):
    user = serializers.SlugRelatedField(
        slug_field='username', read_only=True,
        default=serializers.CurrentUserDefault()
    )
    following = serializers.SlugRelatedField(
        slug_field='username',
        queryset=User.objects.all()
    )

    class Meta:
        model = Follow
        fields = ('user', 'following')

        validators = [
            UniqueTogetherValidator(
                queryset=Follow.objects.all(),
                fields=('user', 'following'),
                message='The subscription must be unique'
            )
        ]

    def validate_following(self, value):
        value = get_object_or_404(User, username=value)
        if value == self.context.get('request').user:
            raise ValidationError('You can\'t subscribe to yourself')
        return value

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'

class PostSerializer(serializers.ModelSerializer):
    author = serializers.SlugRelatedField(
        slug_field='username', read_only=True
    )

    class Meta:
        fields = '__all__'
        model = Post

class CommentSerializer(serializers.ModelSerializer):
    author = serializers.SlugRelatedField(
        read_only=True, slug_field='username'
    )

    class Meta:
        fields = '__all__'
        model = Comment
        read_only_fields = ('post',)