# created by jenny trac
# created on Nov 23 2017
# program lets user choose how many rows and columns
# program shows all the numbers and calculates the average

import ui
from numpy import random

minimum = 1
maximum = 50

def make_the_array(rows, columns):
    # created the array with random numbers

    global minimum
    global maximum

    if rows >= 0 and columns >= 0:
        the_array = range(rows)
        for row_counter in the_array:
            the_array[row_counter] = range(columns)
            for column_counter in range (0 , columns):
                the_array[row_counter][column_counter] = random.randint(minimum, maximum)
        #print the_array

        return the_array

    else:

        return "-1"

def calculate_average(the_2d_array):
    # calulates average of a 2d array

    total = 0

    for every_row in the_2d_array:
        for every_column in every_row:
            total = total + every_column
    average = total / len(the_2d_array)

    return average

def generate_touch_up_inside(sender):
    # button to make array, show average and show all numbers

    # input
    number_of_rows = int(view['rows_textfield'].text)
    number_of_columns = int(view['columns_textfield'].text)

    #process
    random_array = make_the_array(number_of_rows, number_of_columns)
    if random_array == "-1":
        view['array_textview'].text = str(random_array)
        view['average_label'].text = str(random_array)
    else:
        average_of_array = calculate_average(random_array)

        #output
        view['array_textview'].text = str(random_array)
        view['average_label'].text = "The average is: " + str(average_of_array)

view = ui.load_view()
view.present('sheet')


"""
TODO:
  * add evaluation metrics
  * add data augmentation
  * add optimizer scheduler
  * add other models
"""

import copy
import cv2
import datetime
import json
import numpy as np
import os
import sys
import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.log_device_placement = True
tf.enable_eager_execution(config=config)

from glob import glob
from tqdm import tqdm

from models.micro import FirstCNN
from models.apps import FaceModel, app_net

from utils.analysis import load_model
from utils.Augment import Augmentor
from utils.losses import pairwise_loss, triplet_loss
from utils.preparations import channelSwap, normalize, resize
from utils.preprocess import load_and_preprocess_image
from utils.generals import load_list

def main(
        train_list,
        test_list,
        epochs,
        batchsize = 32,
        lr = 0.001,
        insize = 32,
        outsize = 128,
        model_name = 'first',
        loss_name = 'pairwise',
        epsilon = 1e+5,
        debug = False
    ):
    training_id = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

    # Load data
    print('Load data...', end='')
    def preprocess_image(path):
        image = tf.io.read_file(path)
        image = tf.image.decode_jpeg(image, channels=3)
        image = tf.image.resize(image, [insize, insize])
        image /= 255.0
        return image

    def preprocess_label(path):
        label = tf.strings.split(path, sep='/', result_type='RaggedTensor')[-2]
        label = tf.strings.to_number(label, tf.int32)
        return label

    def get_num_classes(*args):
        path = args[0][0]
        data_root_path = '/'.join(path.split('/')[:-2])
        return len(os.listdir(data_root_path))

    def generate_paths_dataset(data_names, batchsize, batch=True):
        data_num = len(data_names)
        dataset_paths = tf.data.Dataset.from_tensor_slices(data_names)
        if batch:
            dataset_paths = dataset_paths.shuffle(buffer_size=data_num)
            dataset_paths = dataset_paths.batch(batchsize)
            dataset_paths = dataset_paths.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
        return dataset_paths

    def load_images_dataset(ds_paths, batchsize, data_num):
        dataset_image = ds_paths.map(preprocess_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
        dataset_label = ds_paths.map(preprocess_label, num_parallel_calls=tf.data.experimental.AUTOTUNE)
        dataset = tf.data.Dataset.zip((dataset_image, dataset_label))
        dataset = dataset.shuffle(buffer_size=data_num)
        dataset = dataset.batch(batchsize)
        dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
        return dataset

    def generate_images_dataset(data_names, batchsize):
        dataset_paths = generate_paths_dataset(data_names, batchsize, batch=False)
        dataset = load_images_dataset(dataset_paths, batchsize, len(data_names))
        return dataset

    train_names = load_list(train_list)
    test_names = load_list(test_list)
    if debug:
        train_names = train_names[:64]
        test_names = test_names[:64]
    num_classes = get_num_classes(train_names, test_names)
    print('Use {} traindata, {} testdata.'.format(len(train_names), len(test_names)))

    train_dataset_x = generate_images_dataset(train_names, batchsize)
    test_dataset_x = generate_images_dataset(test_names, batchsize)
    if loss_name in ['pairwise', 'triplet']:
        train_dataset_y = generate_images_dataset(train_names, batchsize)
        test_dataset_y = generate_images_dataset(test_names, batchsize)
    if loss_name in ['triplet']:
        train_dataset_z = generate_images_dataset(train_names, batchsize)
        test_dataset_z = generate_images_dataset(test_names, batchsize)

    #augmentor = Augmentor(use_brightness=False, use_darkness=False)
    #def rotate(img):
    #    return tf.keras.preprocessing.image.random_rotation(img, 30, row_axis=0, col_axis=1, channel_axis=2)
    #augmentor.funcs.append(rotate)
    augmentor = lambda x: x

    print('Finished.')

    # Load model
    print('Defining model...', end='')
    config = {}
    config['train_list'] = train_list
    config['test_list'] = test_list
    config['batchsize'] = batchsize
    config['epoch'] = epochs
    config['insize'] = insize
    config['outsize'] = outsize
    config['model_name'] = model_name
    config['loss_name'] = loss_name
    config['epsilon'] = epsilon
    model = load_model(config)

    print('Finished.')
    model.summary()

    # Load loss functions
    print('Defining loss function...', end='')
    if loss_name == 'pairwise':
        METRICS = 'distance'
        def loss_func(x, y, equal):
            return pairwise_loss(x, y, equal, epsilon=epsilon)
    elif loss_name == 'triplet':
        METRICS = 'distance'
        def loss_func(x, y, z):
            return triplet_loss(x, y, z, epsilon=epsilon)
    elif loss_name == 'arcface':
        METRICS = 'classification'
        from arcface_modules.models import ArcHead
        from arcface_modules.losses import SoftmaxLoss
        margin = 0.5
        logist_scale = 64
        archead = ArcHead(num_classes=num_classes, margin=margin,
                          logist_scale=logist_scale)
        softmax_fn = SoftmaxLoss()
        def loss_func(z, labels):
            logist = archead(z, labels)
            return softmax_fn(labels, logist)

    else:
        raise ValueError('Invalid loss_name: {}'.format(loss_name))
    print('Finished.')

    # Define graph
    print('Defining train_step()...', end='')
    optimizer = tf.keras.optimizers.Adam(lr=lr)
    train_loss = tf.keras.metrics.Mean(name='train_loss')
    test_loss = tf.keras.metrics.Mean(name='test_loss')

    if METRICS == 'distance':
        assert loss_name in ['pairwise', 'triplet']

        # pairwise operations
        if loss_name == 'pairwise':
            @tf.function
            def train_step(x, y, equal):
                with tf.GradientTape() as tape:
                    x_ = model(x)
                    y_ = model(y)
                    loss = loss_func(x_, y_, equal)
                gradients = tape.gradient(loss, model.trainable_variables)
                optimizer.apply_gradients(zip(gradients, model.trainable_variables))
                train_loss(loss)

            @tf.function
            def test_step(x, y, equal):
                x_ = model(x)
                y_ = model(y)
                loss = loss_func(x_, y_, equal)
                test_loss(loss)

        # triplet operations
        elif loss_name == 'triplet':
            @tf.function
            def train_step(x, y, z):
                with tf.GradientTape() as tape:
                    x_ = model(x)
                    y_ = model(y)
                    z_ = model(z)
                    loss = loss_func(x_, y_, z_)
                gradients = tape.gradient(loss, model.trainable_variables)
                optimizer.apply_gradients(zip(gradients, model.trainable_variables))
                train_loss(loss)

            @tf.function
            def test_step(x, y, z):
                x_ = model(x)
                y_ = model(y)
                z_ = model(z)
                loss = loss_func(x_, y_, z_)
                test_loss(loss)

    elif METRICS == 'classification':

        # arcface operations
        #@tf.function
        def train_step(x, labels):
            with tf.GradientTape() as tape:
                x_ = model(x)
                loss = loss_func(x_, labels)
            gradients = tape.gradient(loss, model.trainable_variables)
            optimizer.apply_gradients(zip(gradients, model.trainable_variables))
            train_loss(loss)

        #@tf.function
        def test_step(x, labels):
            x_ = model(x)
            loss = loss_func(x_, labels)
            test_loss(loss)
    print('Finished.')

    print('Preparing training configure...', end='')
    # save config
    if not os.path.exists('logs/{}'.format(training_id)):
        os.makedirs('logs/{}'.format(training_id))
    with open('logs/{}/train_config.json'.format(training_id), 'w') as f:
        json.dump(config, f)

    # execute train
    header = 'epoch time trainloss testloss'
    template = '{} {} {:.12f} {:.12f}'
    with open('logs/{}/history.csv'.format(training_id), 'w') as f:
        f.write(header + '\n')
    print('Finished.')

    print('Start training.')
    ts_old = datetime.datetime.now()
    previous_saved_ts = datetime.datetime.now()
    for epoch in range(epochs):
        if loss_name == 'pairwise':
            for (x_images, x_labels), (y_images, y_labels) in zip(train_dataset_x, train_dataset_y):
                train_step(augmentor(x_images), auguentor(y_images), x_labels==y_labels)
            for (x_images, x_labels), (y_images, y_labels) in zip(test_dataset_x, test_dataset_y):
                test_step(x_images, y_images, x_labels==y_labels)
        elif loss_name == 'triplet':
            raise NotImplementedError()
            # TODO
            # * arguments for train_step() are Invalid.
            for (x_images, x_labels), (y_images, y_labels), (z_images, z_labels) in tqdm(zip(train_dataset_x, train_dataset_y, train_dataset_z)):
                train_step(augmentor(x_images), augmentor(y_images), augmentor(z_images))
            for (x_images, x_labels), (y_images, y_labels), (z_images, z_labels) in zip(test_dataset_x, test_dataset_y, test_dataset_z):
                test_step(x_images, y_images, z_images)
        elif loss_name == 'arcface':
            #for images, labels in train_dataset_x:
            for images, labels in train_dataset_x:
                train_step(augmentor(images), labels)
            for images, labels in test_dataset_x:
                test_step(images, labels)

        # logging
        ts_new = datetime.datetime.now()
        print('{} |'.format(epochs) + template.format(epoch+1, ts_new-ts_old, train_loss.result(), test_loss.result()))
        with open('logs/{}/history.csv'.format(training_id), 'a') as f:
            f.write(template.format(epoch+1, ts_new-ts_old, train_loss.result(), test_loss.result())+'\n')
        ts_old = datetime.datetime.now()

        # saving models
        if epoch==0 or (ts_new - previous_saved_ts).total_seconds() > 3600:
            save_dir = 'logs/{}'.format(training_id)
            tf.saved_model.save(model, '{}/model.saved_model'.format(save_dir))
            model.save_weights('{}/model_{}.h5'.format(save_dir, epoch+1))
            previous_saved_ts = datetime.datetime.now()

        # reset loss states
        train_loss.reset_states()
        test_loss.reset_states()

    # save final result
    model.save_weights('{}/model_final.h5'.format(save_dir))

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('train_list', type=str, help='list for train data')
    parser.add_argument('test_list', type=str, help='list for test data')
    parser.add_argument('--lr', type=float, default=0.001)
    parser.add_argument('--modelname', '-M', type=str, default='ResNet50', help='name of model architecture')
    parser.add_argument('--lossname', '-L', type=str, default='pairwise', help='name of loss function')
    parser.add_argument('--epochs', '-E', type=int, default=100)
    parser.add_argument('--batchsize', '-B', type=int, default=16)
    parser.add_argument('--insize', '-SI', type=int, default=32, help='size of input images, int, default=32')
    parser.add_argument('--outsize', '-SO', type=int, default=128, help='size of embedded variables, int, default=128')
    parser.add_argument('--epsilon', '-P', type=float, default=1e+5, help='constants for margins in loss')
    parser.add_argument('--debug', action='store_true')
    args = parser.parse_args()

    main(
        args.train_list,
        args.test_list,
        args.epochs,
        lr = args.lr,
        batchsize = args.batchsize,
        insize = args.insize,
        outsize = args.outsize,
        model_name = args.modelname,
        loss_name = args.lossname,
        epsilon = args.epsilon,
        debug = args.debug
    )


n = int(input())
for _ in range(n):
    number = int(input())

    flag = True
    for i in range(2, 10 ** 6):
        if number % i == 0:
            flag = False
            break

    if flag:
        print("YES")
    else:
        print("NO")


import bpy
from bpy.props import *
from .. utils.layout import writeText
from .. base_types import AnimationNodeSocket
from .. preferences import getDeveloperSettings, debuggingIsEnabled

def draw(self, context):
    node = getattr(bpy.context, "active_node", None)
    if not getattr(node, "isAnimationNode", False): return
    layout = self.layout
    layout.separator()
    drawNodeSettings(layout, node)

def drawNodeSettings(layout, node):
    drawSocketLists(layout, node)

    if debuggingIsEnabled():
        layout.separator()
        drawGenericNodeProperties(layout, node)
        layout.label(text = "Identifier: " + node.identifier)

def drawGenericNodeProperties(layout, node):
    layout.prop(node, "width", text = "Width")

    col = layout.column(align = True)
    col.prop(node, "location", text = "X", index = 0)
    col.prop(node, "location", text = "Y", index = 1)

def drawSocketLists(layout, node):
    row = layout.row(align = True)

    size = max(len(node.inputs), len(node.outputs), 1)

    if len(node.inputs) > 0:
        col = row.column()
        subrow = col.row(align = True)
        subrow.label(text = "Inputs")
        subrow.operator("an.move_input", text = "", icon = "TRIA_UP").moveUp = True
        subrow.operator("an.move_input", text = "", icon = "TRIA_DOWN").moveUp = False
        col.template_list("AN_UL_SocketUiList_Normal", "", node, "inputs", node, "activeInputIndex", rows = size, maxrows = size)

    if len(node.outputs) > 0:
        col = row.column()
        subrow = col.row(align = True)
        subrow.label(text = "Outputs")
        subrow.operator("an.move_output", text = "", icon = "TRIA_UP").moveUp = True
        subrow.operator("an.move_output", text = "", icon = "TRIA_DOWN").moveUp = False
        col.template_list("AN_UL_SocketUiList_Normal", "", node, "outputs", node, "activeOutputIndex", rows = size, maxrows = size)

class SocketUiList_Normal(bpy.types.UIList):
    bl_idname = "AN_UL_SocketUiList_Normal"

    def draw_item(self, context, layout, node, socket, icon, activeData, activePropname):
        if not isinstance(socket, AnimationNodeSocket):
            layout.label(text = "No Animation Node Socket")
            return
        if socket.textProps.editable:
            layout.prop(socket, "text", emboss = False, text = "")
        else: layout.label(text = socket.getDisplayedName())

        if socket.removeable:
            socket.invokeFunction(layout, node, "remove", icon = "X", emboss = False)

        icon = "RESTRICT_VIEW_ON" if socket.hide else "RESTRICT_VIEW_OFF"
        layout.prop(socket, "hide", text = "", icon_only = True, icon = icon, emboss = False)

class MoveInputSocket(bpy.types.Operator):
    bl_idname = "an.move_input"
    bl_label = "Move Input"

    moveUp: BoolProperty()

    @classmethod
    def poll(cls, context):
        socket = getActiveSocket(isOutput = False)
        return getattr(socket, "moveable", False)

    def execute(self, context):
        return moveSocket(isOutput = False, moveUp = self.moveUp)

class MoveOutputSocket(bpy.types.Operator):
    bl_idname = "an.move_output"
    bl_label = "Move Output"

    moveUp: BoolProperty()

    @classmethod
    def poll(cls, context):
        socket = getActiveSocket(isOutput = True)
        return getattr(socket, "moveable", False)

    def execute(self, context):
        return moveSocket(isOutput = True, moveUp = self.moveUp)

def moveSocket(isOutput, moveUp):
    socket = getActiveSocket(isOutput)
    socket.moveInGroup(moveUp)

    node = socket.node
    if isOutput: node.activeOutputIndex = list(node.outputs).index(socket)
    else: node.activeInputIndex = list(node.inputs).index(socket)
    return {"FINISHED"}

def getActiveSocket(isOutput):
    node = getattr(bpy.context, "active_node", None)
    if node is None: return
    if isOutput: return node.activeOutputSocket
    else: return node.activeInputSocket

# Register
##################################

def register():
    bpy.types.NODE_PT_active_node_generic.append(draw)

def unregister():
    bpy.types.NODE_PT_active_node_generic.remove(draw)


#encoding: utf-8

import pandas as pd
import time
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import db.db as db
import redis
import sys

input_dim = 100
hidden_encoder_dim_1 = 133
hidden_encoder_dim_2 = 53
hidden_decoder_dim = 53
latent_dim = 33  # （latent Variable）
lam = 0

# 增量更新
add_ids = 500

# 阈值
ratio = 1.5
cpu_ratio = ratio
memory_ratio = ratio
disk_ratio = ratio
net_ratio = ratio

db_obj = db.database(dict=True)
r = redis.StrictRedis(host='127.0.0.1', port=6379, decode_responses=True)
r_key = 'ai_network_id'
nid = r.get('r_key') if r.get('r_key') else 0
nid = int(nid if int(nid) >= 100 else 100)
print("nid: "+str(nid))

# r.set(r_key,0)
# print(r.get(r_key))
# sys.exit(0)
def weight_variable(shape):
    # initial = tf.random_normal(shape)
    initial = tf.truncated_normal(shape, stddev=0.001)

    # 取正态分布的2σ部分
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0., shape=shape)
    return tf.Variable(initial)

x = tf.placeholder("float", shape=[None, input_dim])  # input x [,784]
l2_loss = tf.constant(0.0)

# encoder1 W b
W_encoder_input_hidden_1 = weight_variable(
    [input_dim, hidden_encoder_dim_1])  # 784*1000
b_encoder_input_hidden_1 = bias_variable([hidden_encoder_dim_1])  # 1000
l2_loss += tf.nn.l2_loss(W_encoder_input_hidden_1)

# Hidden layer1 encoder
hidden_encoder_1 = tf.nn.relu(
    tf.matmul(x, W_encoder_input_hidden_1) + b_encoder_input_hidden_1)  # w*x+b

# encoder2 W b
W_encoder_input_hidden_2 = weight_variable(
    [hidden_encoder_dim_1, hidden_encoder_dim_2])  # 1000*400
b_encoder_input_hidden_2 = bias_variable([hidden_encoder_dim_2])  # 400
l2_loss += tf.nn.l2_loss(W_encoder_input_hidden_2)

# Hidden layer2 encoder
hidden_encoder_2 = tf.nn.relu(tf.matmul(
    hidden_encoder_1, W_encoder_input_hidden_2) + b_encoder_input_hidden_2)  # w*x+b

W_encoder_hidden_mu = weight_variable(
    [hidden_encoder_dim_2, latent_dim])  # 400*20
b_encoder_hidden_mu = bias_variable([latent_dim])  # 20
l2_loss += tf.nn.l2_loss(W_encoder_hidden_mu)

# Mu encoder=+
mu_encoder = tf.matmul(hidden_encoder_2, W_encoder_hidden_mu) + \
             b_encoder_hidden_mu  # mu_encoder:1*20(1*400 400*20)

W_encoder_hidden_logvar = weight_variable(
    [hidden_encoder_dim_2, latent_dim])  # W_encoder_hidden_logvar:400*20
b_encoder_hidden_logvar = bias_variable([latent_dim])  # 20
l2_loss += tf.nn.l2_loss(W_encoder_hidden_logvar)

# Sigma encoder
logvar_encoder = tf.matmul(hidden_encoder_2, W_encoder_hidden_logvar) + \
                 b_encoder_hidden_logvar  # logvar_encoder:1*20(1*400 400*20)

# Sample epsilon
epsilon = tf.random_normal(tf.shape(logvar_encoder), name='epsilon')

# Sample latent variable
std_encoder = tf.exp(0.5 * logvar_encoder)
# z_mu+epsilon*z_std=z,as decoder's input;z:1*20
z = mu_encoder + tf.multiply(std_encoder, epsilon)

W_decoder_z_hidden = weight_variable(
    [latent_dim, hidden_decoder_dim])  # W_decoder_z_hidden:20*400
b_decoder_z_hidden = bias_variable([hidden_decoder_dim])  # 400
l2_loss += tf.nn.l2_loss(W_decoder_z_hidden)

# Hidden layer decoder
# hidden_decoder:1*400(1*20 20*400)
hidden_decoder = tf.nn.relu(
    tf.matmul(z, W_decoder_z_hidden) + b_decoder_z_hidden)

W_decoder_hidden_reconstruction = weight_variable(
    [hidden_decoder_dim, input_dim])  # 400*784
b_decoder_hidden_reconstruction = bias_variable([input_dim])
l2_loss += tf.nn.l2_loss(W_decoder_hidden_reconstruction)

KLD = -0.5 * tf.reduce_sum(1 + logvar_encoder - tf.pow(mu_encoder, 2) -
                           tf.exp(logvar_encoder), reduction_indices=1)  # KLD

x_hat = tf.matmul(hidden_decoder, W_decoder_hidden_reconstruction) + \
        b_decoder_hidden_reconstruction  # x_hat:1*784(reconstruction x)

BCE = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(
    logits=x_hat, labels=x), reduction_indices=1)  # sum cross_entropy

loss = tf.reduce_mean(BCE + KLD)  # average value

regularized_loss = loss + lam * l2_loss

# Record the stored value of loss
loss_summ = tf.summary.scalar("lowerbound", loss)
train_step = tf.train.AdamOptimizer(0.001).minimize(
    regularized_loss)  # Optimization Strategy

# add op for merging summary
summary_op = tf.summary.merge_all()

# add Saver ops
saver = tf.train.Saver()

n_steps = int(1e5 + 1)  # step:1000000
batch_size = 100

def data_get_time(timestamp):
    input_data = time.localtime(timestamp)
    return time.strftime("%Y-%m-%d %H:%M:%S", input_data)

def prediction(input_data):
    # data = pd.read_csv("../data/machine_usage.csv", header=None,names=["machine_id", "time_stamp", "cpu_util_percent", "mem_util_percent", "mem_gps", "mkpi","net_in", "net_out", "disk_io_percent"], nrows=1000000)
    # data["time_stamp"] = data["time_stamp"].apply(data_get_time)
    # id = 'm_2043'
    # input_data = data[data['machine_id'] == id]
    # input_data.drop(['mem_gps', 'mkpi', 'net_in'], axis=1, inplace=True)

    data = np.array(input_data['cpu_util_percent']).reshape([-1, 1])

    flag = False
    output = []

    # cpu part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/cpu/model_cpu.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]

        result = scal.fit_transform(result.reshape([-1, 1]))
        # result = result.reshape([-1, 100])
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > cpu_ratio:
            flag = True
            output.append('2')

    data = np.array(input_data['mem_util_percent']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/mem/model_mem.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > memory_ratio:
            flag = True
            output.append('3')

    data = np.array(input_data['net_out']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/net/model_net.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > net_ratio:
            flag = True
            output.append('1')

    data = np.array(input_data['disk_io_percent']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/io/model_io.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > disk_ratio:
            flag = True
            output.append('4')

    output.sort()
    cur_id = r.get('r_key')
    print("cur_id: "+str(cur_id))
    if not output:
        out_str = '0'
        print('No anomoly')
    else:
        out_str = ','.join(output)
        print('Anomoly_type: '+out_str)
    sql = "update data set anomoly_type = '"+out_str+"' where id = "+str(int(cur_id)+100)
    result = db_obj.save(sql)
    if result is True:
        print("Save success:")
        r.set('r_key', str(int(cur_id)+1))
    else:
        print("Save error:")
    print('over')

def read_data_from_db(add_ids, nid):
    sql = "select * from data where anomoly_type = '-1' and id >= "+str(nid-100)+" limit "+str(100+add_ids)
    result = db_obj.fetch_all(sql)
    # print(result)
    return list(result)

def prediction_all(dataset):
    # print(len(dataset))
    for i in range(100, len(dataset)):
        # print(dataset.iloc[i-100:i])
        prediction(dataset.iloc[i-100:i])

data = read_data_from_db(add_ids, nid)
data = pd.DataFrame(data)
# print(data[:3])
# print(data)
# print(type(data))
prediction_all(data)


import unittest
from int_joukko import IntJoukko

def main():
    joukko = IntJoukko(2,4)
    joukko2 = IntJoukko(1,2)

    joukko.lisaa(33)
    joukko.lisaa(12)
    joukko.lisaa(2)
    joukko2.lisaa(5)
    joukko2.lisaa(2)
    joukko2.lisaa(3)
    joukko2.poista(3)

    print(joukko.to_int_list())
    print(joukko2.to_int_list())
    print(joukko.yhdiste(joukko, joukko2))
    print(joukko.leikkaus(joukko, joukko2))
    print(joukko.erotus(joukko, joukko2))

if __name__ == "__main__":
    main()


'''
APS-2020
Problem Description, Input, Output : https://leetcode.com/contest/weekly-contest-197/problems/number-of-good-pairs/
Code by : Sagar Kulkarni
'''

class Solution:
    def numIdenticalPairs(self, nums: List[int]) -> int:
        count=0
        for i in range(len(nums)-1):
            for j in range(i+1,len(nums)):
                if nums[i]==nums[j]:
                    count+=1
        return count


"""
Utilities for RL training and eval.

Note: this code is for research i.e. quick experimentation; it has minimal
comments for now, but if we see further interest from the community -- we will
add further comments, unify the style, improve efficiency and add unittests.

@contactrika

"""
import os
import pickle

import cv2
import torch

from stable_baselines3.common.callbacks import BaseCallback
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.logger import Video

from dedo.utils.train_utils import object_to_str

def play(env, num_episodes, rl_agent, debug=False, logdir=None,
         cam_resolution=None, filename="play"):
    vidwriter = None
    if logdir is not None:
        if not os.path.exists(logdir):
            os.mkdir(logdir)
        vidwriter = cv2.VideoWriter(
            os.path.join(logdir, f'{filename}.mp4'), cv2.VideoWriter_fourcc(*'mp4v'),
            24, (cam_resolution, cam_resolution))
    for epsd in range(num_episodes):
        episode_rwd = 0.0
        if debug:
            print('------------ Play episode ', epsd, '------------------')
        obs = env.reset()
        if vidwriter is not None:
            img = env.render(mode='rgb_array', width=cam_resolution,
                             height=cam_resolution)
            vidwriter.write(img[..., ::-1])
        step = 0
        while True:
            # rl_agent.predict() to get acts, not forcing deterministic.
            act, _states = rl_agent.predict(obs)
            next_obs, rwd, done, info = env.step(act)
            episode_rwd += rwd
            if vidwriter is not None:
                img = env.render(mode='rgb_array', width=cam_resolution,
                                 height=cam_resolution)
                vidwriter.write(img[..., ::-1])
            if done:
                if debug:
                    print(f'episode reward {episode_rwd:0.2f}')
                break
            obs = next_obs
            step += 1
        # input('Episode ended; press enter to go on')
    if vidwriter is not None:
        vidwriter.release()

class CustomCallback(BaseCallback):
    """
    A custom callback that runs eval and adds videos to Tensorboard.
    """

    def __init__(self, eval_env, logdir, num_train_envs, args,
                 num_steps_between_save=10000, viz=False, debug=False):
        super(CustomCallback, self).__init__(debug)
        # Those variables will be accessible in the callback
        # (they are defined in the base class)
        # The RL model
        # self.model = None  # type: BaseAlgorithm
        # An alias for self.model.get_env(), the environment used for training
        # self.training_env = None  # type: Union[gym.Env, VecEnv, None]
        # Number of time the callback was called
        # self.n_calls = 0  # type: int
        # self.num_timesteps = 0  # type: int
        # local and global variables
        # self.locals = None  # type: Dict[str, Any]
        # self.globals = None  # type: Dict[str, Any]
        # The logger object, used to report things in the terminal
        # self.logger = None  # stable_baselines3.common.logger
        # # Sometimes, for event callback, it is useful
        # # to have access to the parent object
        # self.parent = None  # type: Optional[BaseCallback]
        self._eval_env = eval_env
        self._logdir = logdir
        self._num_train_envs = num_train_envs
        self._my_args = args
        self._num_steps_between_save = num_steps_between_save
        self._viz = viz
        self._debug = debug
        self._steps_since_save = num_steps_between_save  # save right away

    def _on_training_start(self) -> None:
        """
        This method is called before the first rollout starts.
        """
        # Log args to tensorboard.
        self.logger.record('args', object_to_str(self._my_args))

    def _on_rollout_start(self) -> None:
        """
        A rollout is the collection of environment interaction
        using the current policy.
        This event is triggered before collecting new samples.
        """
        pass

    def _on_step(self) -> bool:
        """
        This method will be called by the model after each call to `env.step()`.

        For child callback (of an `EventCallback`), this will be called
        when the event is triggered.

        :return: (bool) If the callback returns False, training is aborted early.
        """
        self._steps_since_save += self._num_train_envs
        if self._steps_since_save >= self._num_steps_between_save:
            # Save checkpoint.
            if self._logdir is not None:
                self.model.save(os.path.join(self._logdir, 'agent'))
                pickle.dump(self._my_args,
                            open(os.path.join(self._logdir, 'args.pkl'), 'wb'),
                            protocol=pickle.HIGHEST_PROTOCOL)
            self._steps_since_save = 0
            # Record video.
            if not self._my_args.disable_logging_video:
                screens = []

                def grab_screens(_locals, _globals):
                    screen = self._eval_env.render(
                        mode='rgb_array', width=300, height=300)
                    # PyTorch uses CxHxW vs HxWxC gym (and TF) images
                    screens.append(screen.transpose(2, 0, 1))

                evaluate_policy(
                    self.model, self._eval_env, callback=grab_screens,
                    n_eval_episodes=1, deterministic=False)
                self.logger.record(
                    'trajectory/video',
                    Video(torch.ByteTensor([screens]), fps=50),
                    exclude=('stdout', 'log', 'json', 'csv'))

        return True

    def _on_rollout_end(self) -> None:
        """
        This event is triggered before updating the policy.
        """
        pass

    def _on_training_end(self) -> None:
        """
        This event is triggered before exiting the `learn()` method.
        """
        pass


import requests
from lxml import etree

def get_page(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
    }
    res = requests.get(url, headers=headers)
    res.encoding = res.apparent_encoding
    data = res.text
    # print(data)
    tree = etree.HTML(data)
    # text = tree.xpath('//ul[@class="list-con"]/li/span/text()')
    li_list = tree.xpath('//ul[@class="list-con"]/li')
    # print(text)
    for li in li_list:
        print(li.xpath('./span/text()'))

'''
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index.html
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_1.html
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_2.html
'''
for i in range(3):
    url = f'http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_{i}.html'
    if i == 0:
        url = 'http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index.html'
    get_page(url)


import argparse

parser = argparse.ArgumentParser()
parser.add_argument("infiles", nargs="+", type=argparse.FileType("r"))
parser.add_argument("outfile", type=argparse.FileType("w"))
args = parser.parse_args()

content = "\n".join(f.read() for f in args.infiles)
args.outfile.write(content)


from pathlib import Path
import numpy as np
import glob

path = Path(r"E:\NN_Research\Example1_Hyperelasticity")
file = path / "E_gl_M_data_and_PK2_M_Data_odb.npz"
arrays = np.load(file)
E_gl_M_data = arrays["E_gl_M_data"]
S_M_data = arrays["PK2__M_Data"]
Eyoung = 200e9
xnu = 0.3
Gshear = Eyoung / (2 * (1 + xnu))
Kbulk = Eyoung / (3 * (1 - 2 * xnu))
lmbda = Kbulk - (2 / 3) * Gshear

file = []
I = np.diag([1, 1])
i = 0
for E_gl, S in zip(E_gl_M_data, S_M_data):
    S = S.reshape(3, 3)
    E_gl = E_gl.reshape(2, 2)
    PK2_stV = lmbda * np.trace(E_gl) * I + 2 * Gshear * E_gl
    check = np.isclose(PK2_stV, S[:2, :2], atol=10 ** 5)

    if check.sum() != 4:
        print("Check failed")
        print("iteration", i)
        print(PK2_stV, S)
    i = i + 1
    # print("Iteration:", i)


from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder

from .callback_data_models import MainAdMenuOption, ParamOnEdit, WatchAllAdsMenuOption

main_menu = InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text='Добавить обьявление', callback_data=MainAdMenuOption(action="create_new_ad").pack()),
                InlineKeyboardButton(text='Все объявления', callback_data=MainAdMenuOption(action="watch_all_ads").pack()),
            ],
            [
                InlineKeyboardButton(text='Мои обьявления', callback_data=MainAdMenuOption(action="watch_own_ads").pack())
            ]
        ]
    )

ad_params_on_edit = InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text='Фото', callback_data=ParamOnEdit(param_name="photo").pack()),
                InlineKeyboardButton(text='Название', callback_data=ParamOnEdit(param_name="title").pack()),
                InlineKeyboardButton(text='Описание', callback_data=ParamOnEdit(param_name="description").pack()),
                InlineKeyboardButton(text='Цену', callback_data=ParamOnEdit(param_name="price").pack())
            ],

            [
                InlineKeyboardButton(text='Назад', callback_data='back_to_watching_ads_before_edit')
            ]
        ],
    )

ad_creating_keyboard = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_creating')
        ]
    ]
)

ad_creating_keyboard_on_photo = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Пропустить', callback_data='skip_adding_photo')
        ],
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_creating')
        ]
    ]
)

break_ad_editing_keyb = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_editing')
        ]
    ]
)

back_to_main_menu = InlineKeyboardMarkup(
    inline_keyboard=
    [
        [
            InlineKeyboardButton(text='Вернуться в главное меню', callback_data='back_to_ad_menu')
        ]
    ]
)

kb_after_edit = InlineKeyboardMarkup(
    inline_keyboard=
    [
        [
            InlineKeyboardButton(text='К просмотру объявлений', callback_data='back_to_watching_ads_after_edit'),
            InlineKeyboardButton(text='В главное меню', callback_data='back-to_advertisement_menu'),
        ]
    ]
)

def kb_on_user_ad_watching(all_ads_len, current_num):
    builder = InlineKeyboardBuilder()
    two_btns_required = (0 < current_num < all_ads_len - 1)

    if current_num == 0 and all_ads_len != 1:
        builder.button(text='➡', callback_data='usr_ad_watch:next')
    elif (current_num == all_ads_len - 1) and all_ads_len != 1:
        builder.button(text='⬅', callback_data='usr_ad_watch:prev')
    elif two_btns_required:
        builder.button(text='⬅', callback_data='usr_ad_watch:prev')
        builder.button(text='➡', callback_data='usr_ad_watch:next')

    builder.button(text='🖋Изменить', callback_data='edit_ad')
    builder.button(text='🗑Удалить', callback_data='delete_ad')

    builder.button(text='В главное меню', callback_data='back-to_advertisement_menu')
    if all_ads_len == 1:
        builder.adjust(2, 1)
    else:
        builder.adjust(1 + two_btns_required, 2, 1)
    return builder.as_markup()

watch_all_ads_options = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Поиск по названию', callback_data=WatchAllAdsMenuOption(action="search").pack()),
            InlineKeyboardButton(text='Смотреть все', callback_data=WatchAllAdsMenuOption(action="watch_all").pack())
        ],
        [
            InlineKeyboardButton(text='Вернуться в главное меню', callback_data=WatchAllAdsMenuOption(action="back_to_ad_menu").pack())
        ]
    ]
)

def kb_on_others_ad_watching(all_ads_len, current_num):
    builder = InlineKeyboardBuilder()
    two_btns_required = (0 < current_num < all_ads_len - 1)

    if not current_num and all_ads_len != 1:
        builder.button(text='➡', callback_data='scroll_ad:next')
    elif (current_num == all_ads_len - 1) and all_ads_len != 1:
        builder.button(text='⬅', callback_data='scroll_ad:prev')
    elif two_btns_required:
        builder.button(text='⬅', callback_data='scroll_ad:prev')
        builder.button(text='➡', callback_data='scroll_ad:next')

    builder.button(text="Назад", callback_data='back_to_watch_others_menu')
    builder.button(text='Главное меню', callback_data='back_to_ad_menu')
    if all_ads_len == 1:
        builder.adjust(2, 1)
    else:
        builder.adjust(1 + two_btns_required, 1)
    return builder.as_markup()


from PIL import Image, ImageOps

# resize the image
def resizeImage(img):
    resize_factor = 130
    size = [int(img.size[0] / resize_factor), int(img.size[1] / resize_factor)]
    print('Image size after resizing: ', size)
    return img.resize(size)

# function to remap the image (turn it to greyscale and map between 0-10)
def remapImage(img):
    img = ImageOps.grayscale(img) # to grayscale

    # transforms pixels to fit on a scale between 0-10
    pixels_for_pi = [int(value / 25.5) for value in list(img.getdata())]

    # transforms pixels back to a scale of 255 for viewing purposes
    pixels = [value * 25.5 for value in pixels_for_pi]

    # shows new rempaed image
    img = Image.new(img.mode, img.size)
    img.putdata(pixels)
    # img.show()

    return pixels_for_pi

def piLoop(pixels):
    # opens file and saves to ...
    with open('data/pi data/pi.txt') as f:
        for line in f:
            data = line[2:]
    print('loaded')

    for x in range(len(data) - len(pixels)):
        if data[x:len(pixels)+x] == pixels:
            print('success!!!!!!!!!!!!!!!!')

if __name__ == '__main__':

    img_path = 'data/img data/face.jpg'
    img = Image.open(img_path)

    img = resizeImage(img)
    pixels = remapImage(img)
    pixels = ''.join([str(x) for x in pixels])
    print(pixels)

    piLoop(pixels)


from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _
from .models import User

@admin.register(User)
class CustomUserAdmin(UserAdmin):
    list_display = ("username", "email", "first_name", "last_name", "is_staff", "user_books")
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("first_name", "last_name", "email", "phone", "hobby", "address")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
    )

    @admin.display(description="Кол-во книг")
    def user_books(self, user: User) -> int:
        return user.book_set.count()


# Solar plots
import matplotlib.dates as mdates
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import datetime
from matplotlib import rc
from matplotlib.ticker import ScalarFormatter
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import pandas
import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)

def do_clipping(patches, special_y, keep_below=True, ax=None):
    ax = ax or plt.gca()
    xmin, xmax = plt.xlim()
    ymin, ymax = plt.ylim()
    height = ymax - ymin
    if keep_below:
        height = -height
    clip_rect = Rectangle((x, special_y), xmax - xmin, height,
                          transform=ax.transData)
    for p in patches:
        p.set_clip_path(clip_rect)

######################### Residuals #######################
def SolRes(dec_day, SolSpec, SolI_SS, SolI_SSb, SolI_SSRes, tide_h, waterdepth, sol, datum, datum_percentage):
    AA = np.ones(len(dec_day), dtype=int)
    aa = 2300*AA
    bb = -300*AA
    fig, ax = plt.subplots(2, figsize=(13,7.34))
    for i in range(len(SolSpec.columns)):
        if i==0:
            colour = 'firebrick'
            a = 0.7
            label='Red '
        elif i==1:
            a = 0.7
            colour = 'seagreen'
            label='Green'
        elif i==2:
            colour = 'steelblue'
            a = 0.7
            label='Blue'
        else:
            colour = 'grey'
            a = 0.3
            label=None

        ax[0].plot(dec_day, SolI_SSRes.iloc[:,i], label=label + 'Res', color=colour)
        ax[0].plot(dec_day, SolI_SSb.iloc[:,i], label=label + 'Sb', color=colour, linestyle='dashed', alpha=0.5)
        ax[0].plot(dec_day, SolI_SS.iloc[:,i], label=label + 'S', color=colour, linestyle='dotted', alpha=0.5)
        ax[0].set_title('Solar \n Difference between Surface and Seabed (Datum) (dot - dashed)')
        ax[0].set_ylabel('Irradiance difference\n (W m$^{-2}$)')
        ax[0].legend(prop={"size":7}, loc='upper right')

    ax2i = ax[1].twinx()
    ax2i.fill_between(dec_day, aa, bb, where=sol < AA, facecolor='lightgrey', alpha=0.3)
    ax2i.axes.get_yaxis().set_visible(False)
    ax[1].plot(dec_day, waterdepth, color='royalblue')
    ax[1].set_title(f'Height of water column above {datum_percentage*100}% max depth')
    ax[1].set_ylabel('water column above datum (m)', color='royalblue')
    ax2 = ax[1].twinx()
    ax2.axhline(y=datum, xmin=0, xmax=366, color='cadetblue', alpha = 0.2, linestyle='dashed', label='datum')
    ax2.legend(prop={"size":8}, loc='upper right')
    ax2.plot(dec_day, tide_h, label='tide', color='cadetblue', alpha=0.2)
    ax2.set_ylabel('Tidal range (m)', color='cadetblue')
    ax[1].set_ylim([-(max(tide_h)/10), (max(tide_h)+max(tide_h)/10)])

############################## 3D Normalised Residuals/ Irradiance ###########################
def Sol3d(dec_day, SolSpec, SolI_SSb, SolI_SSRes, SolI_SS, datum, datum_percentage):

    fig = plt.figure(figsize=(13,7.34))
    ax = fig.add_subplot(1,2,1, projection='3d')
    el=6; az=-99; distance=7

    ylab='Wavelength (nm)'; xlab='Julian Day'; zlab='Normalized Irradiance W/m$^{-2}$'
    fig.suptitle(f'Normalized Spectral Irradiance at {datum}m below Mean Sea-level and Residuals')

    ax.view_init(elev=el, azim=az)
    ax.dist=distance
    ax.set_zlabel(zlab)
    ax.set_xlabel(xlab)
    ax.set_ylabel(ylab)

    ax.set_title('Solar Seabed Spectra: 380-700nm') # DATA title
    for i in range(len(SolSpec.columns)):
        if i==0:
            colour = 'firebrick'
            a = 0.7
            label='620nm=<Red '
        elif i==1:
            a = 0.7
            colour = 'seagreen'
            label='490nm<Green<560nm'
        elif i==2:
            colour = 'steelblue'
            a = 0.7
            label='400nm<Blue<490nm'
        else:
            colour = 'grey'
            a = 0.3
            label=None

                #### ISSUE IS 'zs' requires a numeric value, could just have 0, 1, 2 but plot 1 makes this obsolet with only 3 lines
        Axes3D.plot(ax, xs=dec_day, ys=SolI_SSb.iloc[:,i]/(max(SolI_SSb.iloc[:,i])), zs=i, zdir='y', color=colour)

    ax = fig.add_subplot(1,2,2, projection='3d')
    el=6; az=-99; distance=7

    ylab='Wavelength (nm)'; xlab='Julian Day'; zlab='Normalized Irradiance (umol/m^2/s)'
    fig.suptitle(f'Normalized Spectral Irradiance at {datum}m below Mean Sea-level')

    ax.view_init(elev=el, azim=az)
    ax.dist=distance
    ax.set_zlabel(zlab)
    ax.set_xlabel(xlab)
    ax.set_ylabel(ylab)

    ax.set_title('Solar through water Residuals Spectra: 380-700nm') # DATA1 title
    for i in range(len(SolSpec.columns)):
        if i==0:
            colour = 'firebrick'
            a = 0.7
            label='620nm=<Red '
        elif i==1:
            a = 0.7
            colour = 'seagreen'
            label='490nm<Green<560nm'
        elif i==2:
            colour = 'steelblue'
            a = 0.7
            label='400nm<Blue<490nm'
        else:
            colour = 'grey'
            a = 0.3
            label=None

        Axes3D.plot(ax, xs=dec_day, ys=SolI_SSRes.iloc[:,i]/max(SolI_SSRes.iloc[:,i]), zs=i, zdir='y', color=colour)
        ax.set_yticks([])

###################### Overlay Plots ####################################
def SolOverlay(dec_day, SolSpec, SolI_SS, SolI_SSb, Io, tide_h, waterdepth, sol, IBT, datum, datum_percentage,location, figurepath):
    fig, ax = plt.subplots(4, figsize=(13.5,7.34))
    # fig.suptitle(f'Chlorophyll = {Chlorophyll}, fCDOM = {fCDOM}, backscatter = {backscatter}')
    fig.suptitle(f'{location}\n')

    # SOLAR INT SEALEVEL
    ax[0].plot(dec_day, Io, color='black')
    ax0 = ax[0].twinx()
    #ax0.set_ylim([(-max(Io)/10), (max(Io)+max(Io)/10)])

    AA = np.ones(len(dec_day), dtype=int)
    aa = 2300*AA
    bb = -300*AA

    ax0.fill_between(dec_day, aa, bb, where= sol < AA, facecolor='grey', alpha=0.2)
    ax0.axes.get_yaxis().set_visible(False)
    for x in range(len(SolSpec.columns)):
        if x==0:
            colour = 'orangered'
            a = 0.7
        elif x==1:
            a = 0.7
            colour = 'limegreen'
        elif x==2:
            colour = 'deepskyblue'
            a = 0.7
        else:
            colour = 'grey'
            a = 0.3
        ax[0].plot(dec_day, SolI_SS.iloc[:,x], color=colour, alpha=a)
    ax[0].set_yscale('symlog')
    ax[0].yaxis.set_major_formatter(ScalarFormatter())
    # ax[0].set_ylabel('Irr (umol m^-2 s^-1)')
    ax[0].set_ylabel('Irradiance\n (W m$^{-2}$)')
    ax[0].set_title('Surface Solar Irradiance')
##            ax[0].set_xlim([startdate, enddate])
    ax0.set_ylim([(-max(Io)/10), (max(Io)+max(Io)/10)])

    # SOLAR INT SEABED
    ax1 = ax[1].twinx()

    ax[1].plot(dec_day, IBT, color='black')
    for i in range(len(SolSpec.columns)):
        if i==0:
            colour = 'orangered'
            a = 0.7
            label='620nm=<Red '
        elif i==1:
            a = 0.7
            colour = 'limegreen'
            label='490nm<Green<560nm'
        elif i==2:
            colour = 'deepskyblue'
            a = 0.7
            label='400nm<Blue<490nm'
        else:
            colour = 'grey'
            a = 0.3
            label=None
        ax[1].plot(dec_day, SolI_SSb.iloc[:,i], label=label, color=colour, alpha=a)
    #ax[1].legend()
    ax[1].set_title('Intertidal Solar Irradiance')
    #ax[1].set_ylabel('Irr (umol m^-2 s^-1)')
    ax[1].set_ylabel('Irradiance\n (W m$^{-2}$)')
    ax[1].set_yscale('symlog')
    ax[1].yaxis.set_major_formatter(ScalarFormatter())
    ax1.fill_between(dec_day, aa, bb, where= sol < AA, facecolor='grey', alpha=0.2)
    ax1.axes.get_yaxis().set_visible(False)
    ax1.set_ylim([(-max(IBT)/10), (max(IBT)+max(IBT)/10)])

    # TIDAL MODEL WITH REFERENCE DATUM AND WATERCOLUMN HEIGHT
    ax2i = ax[2].twinx()
    ax2i.fill_between(dec_day, aa, bb, where= sol < AA, facecolor='grey', alpha=0.2)
    ax2i.axes.get_yaxis().set_visible(False)
    ax[2].plot(dec_day, waterdepth, label='water depth', color='royalblue')
    ax[2].set_title('Height of water column')
    ax[2].set_ylabel(f'Water column\n above datum at {int(datum_percentage*100)}%', color='black')
    ax2 = ax[2].twinx()
    ax2.axhline(y=datum, xmin=0, xmax=366, color='cadetblue', alpha = 0.5, linestyle='dashed', label='datum')
    ax2.plot(dec_day, tide_h, label='tide', color='cadetblue', alpha=0.5)
    ax2.legend(prop={"size":8}, loc='upper right')
    ax2.set_ylabel('Tidal range (m)', color='cadetblue', labelpad=15)
    ax[2].set_ylim([-(max(tide_h)/10), (max(tide_h)+max(tide_h)/10)])

    # # DAY/NIGHT
    # for i in range(len(sol)):
    #     if sol[i] < 1:
    #         solar_vars = 1
    #     else:
    #         solar_vars = 0

    ax[3].set_title('Daylight hours')
    ax[3].plot(dec_day, sol, color='gold')

    ax[3].set_ylabel('Daylight')
    ax[3].set_xlabel("Julian Day")
#            ax[3].set_xlim([startdate, enddate])
#            plt.tight_layout(pad=0.4, w_pad=0.4, h_pad=0.4)
    fig.tight_layout(pad=0.4, w_pad=0.4, h_pad=0.5)
    fig.savefig(figurepath +str('Solar.png'))

if __name__ == '__main__':
    print('This script should be run in conjunction with TidalLight_Model.py')


## Aşağıdaki iç içe döngüyü tek satırda yazma.
liste = []
for i in range(2,10):
    for j in range (1,10):
            liste.append(i*j)
print(liste)

# İç içe döngüleri kullanırken yukarıdan başlayarak yan yana yazılır. en içerideki en başa yazılır!
liste2 = [(i*j) for i in range(2,10) for j in range(1,10)]
print(liste2)

if liste == liste2:
    print("Listeler Aynı")

minik problemler/Problem - 1/Problem - 1.py


#409. Longest Palindrome

import collections

def longestPalindrome(self, s):
    if len(s) <= 1:
        return len(s)
    s_c = collections.Counter(s)
    count, odds = 0, 0
    for ch, freq in s_c.items():
        if freq % 2 != 0:
            odds += 1
        count += freq

    return count-odds+1 if odds != 0 else count

s = "abccccdd"
print(longestPalindrome(0, s))


#ExcersiseSixNotSatan
#Sam Luther
#9/13/16
##_________      ______________________
##\_____  |      |  __________________/
##    \ | |      | | /
##     \| |______| |/
##     /            \
##    |--||||-||||---|
##    |  \||/ \||/   |
##     |     oo     |
##      \___/\/\___/
##         |____|
##        /      \
##       /        \
##      /          \
##     /  |      |  \
##    |   |      |   |
##    |   |      |   |
##    /___\______/___\
##       |        |       (\ /)
##       |   __   |       (^_^)
##      /____\/____\      C(")(")
import turtle

bgcolor = input("Please Enter a valid color for your background:")
str(bgcolor)
fgcolor = input("Please Enter a valid color for your foreground:")
str(fgcolor)
pensize = input("Please Enter a valid amount for your pensize(1-100):")
int(pensize)
sides = input("Please Enter a valid amount for the length of your sides:")
int(sides)
nsides = input("Please Enter a valid amount for the number of sides you would like:")
int(nsides)
asides = input("Please Enter a valid amount for angle you wish each side to turn from the       previous side:")
int(asides)
fillq = input("Would you like the shape to be filled after completion? Please answer fill or no fill:")
str(fillq)
turing = turtle.Turtle()
if(fillq == "fill"):
    fillcq = input("Please enter a valid color to fill the shape with:")
    str(fillcq)
    turing.fillcolor(str(fillcq))
    turing.begin_fill()
    for i in range(int(nsides)):
            wn=turtle.Screen()
            wn.bgcolor(bgcolor)

            turing.shape("turtle")
            turing.pencolor(fgcolor)
            turing.pensize(pensize)
            turing.forward(int(sides))
            turing.left(int(asides))
    turing.end_fill()
else :
    for i in range(int(nsides)):
            wn=turtle.Screen()
            wn.bgcolor(bgcolor)

            turing.shape("turtle")
            turing.pencolor(fgcolor)
            turing.pensize(pensize)
            turing.forward(int(sides))
            turing.left(int(asides))


_base_ = [
    'models/universenet_swin.py',
    '_base_/datasets/coco_detection_16.py',
    '_base_/schedules/schedule_2x.py', '_base_/default_runtime.py'
]

data_root = '../../dataset/'
data = dict(samples_per_gpu=2,
            train=dict(ann_file=data_root +  'cv3_train_pesudo.json'),
            val=dict(ann_file=data_root +  'cv2_val_3.json'))

optimizer = dict(
    _delete_=True,
    type='AdamW',
    lr=0.0001,
    betas=(0.9, 0.999),
    weight_decay=0.05,
    paramwise_cfg=dict(
        custom_keys={
            'absolute_pos_embed': dict(decay_mult=0.),
            'relative_position_bias_table': dict(decay_mult=0.),
            'norm': dict(decay_mult=0.)
        }))
optimizer_config = dict(
    _delete_=True, grad_clip=dict(max_norm=35, norm_type=2))
lr_config = dict(
    _delete_=True,
    policy='CosineAnnealing',
    warmup='linear',
    warmup_iters=1000,
    warmup_ratio=1.0 / 10,
    min_lr_ratio=1e-5)
fp16 = dict(loss_scale=512.)

log_config = dict(
    interval=500,
    hooks=[
        dict(type='TextLoggerHook', interval=50),
        # dict(type='TensorboardLoggerHook')
        dict(type='WandbLoggerHook',interval=500,
            init_kwargs=dict(
                project='objectdetection',
                entity = 'boostcampaitech3',
                name = 'Univ2008d_21_3_swin'
            ))
    ])


#!/usr/bin/env python3

import re
import sys
import pysam
import tabix
import cyvcf2
import natsort
import argparse
import multiprocessing as mp
from collections import defaultdict
from typing import Dict, List, Tuple, Set

def parse_args(args):
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "-i",
        "--bam",
        type=str,
        required=True,
        help="BAM file to read",
    )
    parser.add_argument(
        "--sbs",
        type=str,
        required=True,
        help="himut VCF file to read",
    )
    parser.add_argument(
        "--vcf",
        type=str,
        required=True,
        help="deepvariant VCF file to read",
    )
    parser.add_argument(
        "--min_bq",
        type=int,
        default=93,
        required=False,
        help="minimum base quality score",
    )
    parser.add_argument(
        "--min_mapq",
        type=int,
        default=60,
        required=False,
        help="minimum mapping quality score",
    )
    parser.add_argument(
        "-t",
        "--threads",
        type=int,
        default=1,
        required=False,
        help="maximum number of threads",
    )
    parser.add_argument(
        "-o",
        "--out",
        type=str,
        required=True,
        help="file to write",
    )
    args = args[1:]
    return parser.parse_args(args)

class BAM:
    def __init__(self, line):
        # target
        self.tname = line.reference_name
        self.tstart = line.reference_start
        self.tend = line.reference_end
        self.target_alignment_length = self.tend - self.tstart
        # query
        self.qname = line.query_name.replace("/ccs", "")
        self.qstart = line.query_alignment_start
        self.qend = line.query_alignment_end
        self.qseq = line.query_sequence
        self.qlen = len(self.qseq)
        self.mapq = line.mapping_quality
        self.bq_int_lst = line.query_qualities
        self.qv = sum(self.bq_int_lst)/self.qlen
        self.query_alignment_length = self.qend - self.qstart
        self.query_alignment_proportion = self.query_alignment_length/float(self.qlen)
        self.cs_tag = line.get_tag("cs") if line.has_tag("cs") else "."
        if line.has_tag("tp"):
            if line.get_tag("tp") == "P":
                self.is_primary = True
            else:
                self.is_primary = False
        else:
            self.is_primary = False

    def cs2lst(self):
        cslst = [cs for cs in re.split("(:[0-9]+|\*[a-z][a-z]|[=\+\-][A-Za-z]+)", self.cs_tag)]
        cslst = [cs.upper() for cs in cslst if cs != ""]
        return cslst

    def cs2tuple(self) -> List[Tuple[int, str, str, int, int]]:
        qpos = self.qstart
        self.cstuple_lst = []
        cs_lst = self.cs2lst()
        for cs in cs_lst:
            m = cs[1:]
            mlen = len(m)
            qstart = qpos
            if cs.startswith("="):  # match # --cs=long
                cs = ":{}".format(mlen)
                t = (1, m, m, mlen, mlen)
            elif cs.startswith(":"):  # match # --cs=short
                mlen = int(m)
                qend = qpos + mlen
                m = self.qseq[qstart:qend]
                t = (1, m, m, mlen, mlen)
            elif cs.startswith("*"):  # snp # target and query
                mlen = 1
                ref, alt = list(m)
                t = (2, ref, alt, 1, 1)
            elif cs.startswith("+"):  # insertion # query
                ref = self.qseq[qpos - 1]
                alt = ref + m
                t = (3, ref, alt, 0, mlen)
            elif cs.startswith("-"):  # deletion # target
                alt = self.qseq[qpos - 1]
                ref = alt + m
                t = (4, ref, alt, mlen, 0)
                mlen = 0
            self.cstuple_lst.append(t)
            qpos += mlen

    def cs2subindel(self):
        self.cs2tuple()
        tpos = self.tstart
        qpos = self.qstart
        self.tsbs_lst = []
        self.qsbs_lst = []
        self.qsbs_bq_lst = []
        self.tindel_lst = []
        for cstuple in self.cstuple_lst:
            state, ref, alt, ref_len, alt_len, = cstuple
            if state == 2:  # snp
                if ref == "N":
                    continue
                self.qsbs_bq_lst.append(self.bq_int_lst[qpos])
                self.tsbs_lst.append((tpos + 1, ref, alt))
                self.qsbs_lst.append((qpos + 1, alt, ref))
            elif state == 3 or state == 4:  # insertion
                self.tindel_lst.append((tpos, ref, alt))
            tpos += ref_len
            qpos += alt_len

class VCF:
    def __init__(self, line):
        arr = line.strip().split()
        self.chrom = arr[0]
        self.pos = int(arr[1])
        self.id = arr[2]
        self.ref = arr[3]
        self.alt_lst = arr[4].split(",")
        self.qual = arr[5]
        self.qual = float(self.qual) if self.qual != "." else self.qual
        self.is_pass = True if arr[6] == "PASS" else False
        self.info = arr[7]
        self.format_lst = arr[8].split(":")
        self.sample_format_lst = arr[9].split(":")
        hsh = {i:j for i,j in zip(self.format_lst, self.sample_format_lst)}
        if "GT" in hsh: self.sample_gt = hsh["GT"]
        if "PS" in hsh: self.sample_phase_set = hsh["PS"]
        if "AD" in hsh:
            arr = hsh["AD"].split(",")
            self.ref_count = arr[0]
            self.alt_count_arr = arr[1:]

        self.is_snp = False
        self.is_dbs = False
        self.is_indel = False
        if len(self.alt_lst) == 1:  # bi-allelic
            self.is_biallelic = True
            self.alt = self.alt_lst[0]
            if len(self.ref) == 1 and len(self.alt) == 1:  # snp
                self.is_snp = True
            elif len(self.ref) == len(self.alt) == 2:
                self.is_dbs = True
            elif len(self.ref) > len(self.alt): # del
                self.is_indel = True
            elif len(self.ref) < len(self.alt): # ins
                self.is_indel = True
        else:
            self.is_biallelic = False

def get_tname2tsize(bam_file: str) -> Tuple[List[str], Dict[str, int]]:

    tname2tsize = {}
    alignments = pysam.AlignmentFile(bam_file, "rb")
    bam_header_lst = str(alignments.header).strip().split("\n")
    for h in bam_header_lst:
        if h.startswith("@SQ"):
            _tag, tname, tsize = h.split("\t")
            tname = tname.replace("SN:", "")
            tsize = tsize.replace("LN:", "")
            tname2tsize[tname] = int(tsize)
    alignments.close()
    return tname2tsize

def load_vcf_file(
    chrom: str,
    chrom_len: int,
    vcf_file: str
) -> Set[Tuple[int, str, str]]:

    snp_set = set()
    if vcf_file.endswith(".vcf"):
        for line in open(vcf_file):
            if line.startswith("#"):
                continue
            v = VCF(line)
            if v.chrom != chrom:
                continue
            if v.is_pass:
                if v.is_biallelic:
                    if v.is_snp:
                        snp_set.add((v.pos, v.ref, v.alt))
                else:
                    for alt in v.alt_lst:
                        if len(v.ref) == 1 and len(alt) == 1:
                            snp_set.add((v.pos, v.ref, alt))
    elif vcf_file.endswith(".bgz"):
        tb = tabix.open(vcf_file)
        records = tb.query(chrom, 0, chrom_len)
        for record in records:
            v = VCF("\t".join(record))
            if v.is_snp and v.is_pass:
                if v.is_biallelic:
                    if v.is_snp:
                        snp_set.add((v.pos, v.ref, v.alt))
                else:
                    for alt in v.alt_lst:
                        if len(v.ref) == 1 and len(alt) == 1:
                            snp_set.add((v.pos, v.ref, alt))
    return snp_set

def load_sbs_file(vcf_file: str):

    chrom2sbs_lst = defaultdict(list)
    if vcf_file.endswith(".vcf"):
        for line in open(vcf_file).readlines():
            if line.startswith("#"):
                continue
            v = VCF(line)
            if v.is_pass:
                chrom2sbs_lst[v.chrom].append((v.pos, v.ref, v.alt))
    elif vcf_file.endswith(".bgz"):
        for i in cyvcf2.VCF(vcf_file):
            v = VCF(str(i))
            if v.is_pass:
                chrom2sbs_lst[v.chrom].append((v.pos, v.ref, v.alt))
    return chrom2sbs_lst

def get_ccs(
    chrom: str,
    chrom_len: int,
    bam_file: str,
    vcf_file: str,
    sbs_lst: List[Tuple[int, str, str]],
    min_bq: int,
    min_mapq: int,
    chrom2ccs_lst: Dict[str, str]
):

    ccs_lst = []
    himut_set = set(sbs_lst)
    alignments = pysam.AlignmentFile(bam_file, "rb")
    deepvariant_set = load_vcf_file(chrom, chrom_len, vcf_file)
    for sbs in sbs_lst:
        pos, ref, alt = sbs
        for line in alignments.fetch(chrom, pos, pos + 1):
            ccs = BAM(line)
            if not ccs.is_primary:
                continue
            if ccs.mapq >= min_mapq:
                ccs.cs2subindel()
                tsbs_set = set(ccs.tsbs_lst)
                if sbs in tsbs_set:

                    himut_sbs_lst = []
                    deepvariant_snp_lst = []
                    himut_candidate_lst = []
                    himut_q93_candidate_lst = []
                    for tsbs, qbq in zip(ccs.tsbs_lst, ccs.qsbs_bq_lst):
                        if tsbs in deepvariant_set:
                            deepvariant_snp_lst.append(tsbs)
                        else:
                            himut_candidate_lst.append(tsbs)
                            if qbq >= min_bq:
                                himut_q93_candidate_lst.append(tsbs)

                        if tsbs in himut_set and qbq >= min_bq:
                            himut_sbs_lst.append(tsbs)

                    ccs_lst.append(
                        "\t".join(
                            [
                            "{}:{}_{}/{}".format(chrom, pos, ref, alt),
                            ccs.qname,
                            str(ccs.qv),
                            str(ccs.qlen),
                            str(ccs.mapq),
                            str(len(himut_sbs_lst)),
                            str(len(himut_q93_candidate_lst)),
                            str(len(himut_candidate_lst)),
                            str(len(deepvariant_snp_lst)),
                            str(len(ccs.tsbs_lst)),
                            ]
                        )
                    )
    chrom2ccs_lst[chrom] = ccs_lst

def sbs2ccs(
    bam_file: str,
    sbs_file: str,
    vcf_file: str,
    min_bq: int,
    min_mapq: int,
    threads: int,
    out_file: str
):

    tname2tsize = get_tname2tsize(bam_file)
    chrom2sbs_lst = load_sbs_file(sbs_file)
    chrom_lst = natsort.natsorted(chrom2sbs_lst.keys())

    p = mp.Pool(threads)
    manager = mp.Manager()
    chrom2ccs_lst = manager.dict()
    get_ccs_arg_lst = [
        (
            chrom,
            tname2tsize[chrom],
            bam_file,
            vcf_file,
            chrom2sbs_lst[chrom],
            min_bq,
            min_mapq,
            chrom2ccs_lst
        )
        for chrom in chrom_lst
    ]
    p.starmap(
        get_ccs, get_ccs_arg_lst,
    )
    p.close()
    p.join()

    o = open(out_file, "w")
    o.write("{}\n".format("\t".join(["sbs", "qname", "qv", "qlen", "mapq", "himut_count", "himut_q93_candidate_count", "himut_candidate_count", "snp_count", "sbs_count"])))
    for chrom in chrom_lst:
        for p in chrom2ccs_lst[chrom]:
            o.write("{}\n".format(p))
    o.close()

def main():
    options = parse_args(sys.argv)
    sbs2ccs(
        options.bam,
        options.sbs,
        options.vcf,
        options.min_bq,
        options.min_mapq,
        options.threads,
        options.out
    )
    sys.exit(0)

if __name__ == "__main__":
    main()


def analyze_tip_perc(df, gcs_output_path=None):

    pickup_tip_avg = df.groupBy("PULocationID").avg("tip_amount").withColumnRenamed("avg(tip_amount)", "avg_pickup_tip")
    dropoff_tip_avg = df.groupBy("DOLocationID").avg("tip_amount").withColumnRenamed("avg(tip_amount)", "avg_dropoff_tip")

    tip_analysis = pickup_tip_avg.join(dropoff_tip_avg, pickup_tip_avg.PULocationID == dropoff_tip_avg.DOLocationID, "outer") \
                                .select(
                                    pickup_tip_avg.PULocationID,
                                    pickup_tip_avg.avg_pickup_tip,
                                    dropoff_tip_avg.avg_dropoff_tip
                                )

    tip_analysis.repartition(1) \
    .write \
    .mode("overwrite") \
    .format("csv") \
    .option("header", "true") \
    .save(f"{gcs_output_path}/tip_analysis")


################################################################################
# GitKraken Glo のカードの一覧を CSV に出力します。
################################################################################

import csv
import json
import os
import py_glo_boards_api

# 作業ディレクトリのパス
WORKING_DIRECTORY_PATH = os.path.dirname(os.path.abspath(__file__))

# 設定をロード
config = json.load(open(f"{WORKING_DIRECTORY_PATH}/config.json", "rt"))

# 各ボード、カードから取得するフィールド
board_fields = ["name", "columns"]
card_fields = ["name", "description", "column_id", "labels"]

# 出力先のディレクトリが無ければ作成
dest_dir_path = f"{WORKING_DIRECTORY_PATH}/dest"
if not os.path.exists(dest_dir_path):
    os.makedirs(dest_dir_path)

# GloBoard オブジェクトを取得
globoard = py_glo_boards_api.GloBoard(config["personal_access_token"])

# 各ボードに対して処理を行う
for board in globoard.get_boards(board_fields):
    # カラムの ID がキー、名前が値の辞書を作成
    columns = {}
    for column in board.columns:
        columns[column.id] = column.name

    # 全カードのリストを作成
    cards = []
    for card in globoard.get_cards(board.id, card_fields, per_page=300):
        # カードデータ保存用の辞書を用意
        row = {}

        # 名前
        row["name"] = card.name

        # 説明
        # 改行は "\\n" に置換する
        if card.description is not None:
            row["description"] = card.description["text"].replace("\n", "\\n")

        # カラム
        row["column"] = columns[card.column_id]

        # ラベル
        # 複数ある場合は "|" で区切る
        row["label"] = ""
        if card.labels is not None:
            for label in card.labels:
                row["label"] += label.name + "|"
        row["label"] = row["label"].rstrip("|")

        # カードデータをリストに追加
        cards.append(row)

    # 全カードのリストを CSV に出力
    with open(
        f"{dest_dir_path}/{board.name}.csv", "wt", encoding="utf_8", newline=""
    ) as file:
        writer = csv.DictWriter(file, ["name", "description", "column", "label"])
        writer.writeheader()
        writer.writerows(cards)


load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

visibility("private")

_ALL_ABIS = ["arm64", "x86_64"]

def _build_with_abi(name, build_rule, path_prefix, abis, visibility, **kwargs):
    build_rule(name = name, visibility = visibility, **kwargs)

    for abi in abis:
        copy_file(
            name = "{name}_{abi}".format(name = name, abi = abi),
            src = ":{name}".format(name = name),
            out = paths.join(path_prefix, abi, name),
            allow_symlink = True,
            visibility = visibility,
        )

def cc_binary_with_abi(name, path_prefix = None, abis = None, visibility = None, **kwargs):
    """A cc_binary replacement that generates output in each subdirectory named by abi.

    For example:
    ```
      cc_binary_with_abi(
        name = "a_binary",
        abis = ["x86_64", "arm64"],
        path_prefix = "my/path",
      )
    ```
    generates 2 rules:
    * Rule a_binary_x86_64: Builds the cc_binary and put output in my/path/x86_64/a_binary.
    * Rule a_binary_arm64: Builds the cc_binary and put output in my/path/arm64/a_binary.

    Args:
        name: the name of the build rule.
        path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
          The path prefix to attach to output.
        abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
          The intended abis to generate.
        visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
          The visibility attribute on a target controls whether the target can be used in other packages.
        **kwargs: the rest args that cc_binary uses.
    """
    if not path_prefix:
        path_prefix = ""
    if not abis:
        abis = _ALL_ABIS

    _build_with_abi(
        name = name,
        build_rule = native.cc_binary,
        path_prefix = path_prefix,
        abis = abis,
        visibility = visibility,
        **kwargs
    )


import numpy as np
import re
from sklearn.datasets import fetch_20newsgroups
from sklearn.datasets import load_files

def clean_str(string):
    """
    Tokenization/string cleaning for all datasets except for SST.
    Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
    """
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip().lower()

def batch_iter(data, batch_size, num_epochs, shuffle=False):
    """
    Generates a batch iterator for a dataset.
    """
    data = np.array(data)
    data_size = len(data)
    num_batches_per_epoch = int((len(data)-1)/batch_size) + 1
    for epoch in range(num_epochs):
        # Shuffle the data at each epoch
        if shuffle:
            shuffle_indices = np.random.permutation(np.arange(data_size))
            shuffled_data = data[shuffle_indices]
        else:
            shuffled_data = data
        for batch_num in range(num_batches_per_epoch):
            start_index = batch_num * batch_size
            end_index = min((batch_num + 1) * batch_size, data_size)
            yield shuffled_data[start_index:end_index]

def get_datasets_20newsgroup(subset='train', categories=None, shuffle=True, random_state=42):
    """
        Retrieve data from 20 newsgroups
        :param subset: train, test or all
        :param categories: List of newsgroup name
        :param shuffle: shuffle the list or not
        :param random_state: seed integer to shuffle the dataset
        :return: data and labels of the newsgroup
        """
    datasets = fetch_20newsgroups(subset=subset, categories=categories, shuffle=shuffle, random_state=random_state)
    #print(datasets)
    return datasets

def get_datasets_abstract(dir):
    print('+++++++++abstract+++++++++')
    #dir = 'datatrain/'
    task_examples = list(open(dir + 'task_data_file', "r").readlines())
    task_examples = [s.strip() for s in task_examples]
    model_examples = list(open(dir + 'model_data_file', "r").readlines())
    model_examples = [s.strip() for s in model_examples]
    dataset_examples = list(open(dir + 'dataset_data_file', "r").readlines())
    dataset_examples = [s.strip() for s in dataset_examples]
    result_examples = list(open(dir + 'result_data_file', "r").readlines())
    result_examples = [s.strip() for s in result_examples]
    other_examples = list(open(dir + 'other_data_file', "r").readlines())
    other_examples = [s.strip() for s in other_examples]
    task_model_examples = list(open(dir + 'task_model_file', "r").readlines())
    task_model_examples = [s.strip() for s in task_model_examples]
    data_result_examples = list(open(dir + 'data_result_file', "r").readlines())
    data_result_examples = [s.strip() for s in data_result_examples]
    all_examples = list(open(dir + 'all_file', "r").readlines())
    all_examples = [s.strip() for s in all_examples]

    data_train = dict()
    data_train['data'] = all_examples
    target = [0 for x in all_examples]
    data_train['target'] = target
    data_train['target_names'] = ['all_examples']
    return data_train

def get_datasets_intents(dir):

    expert_examples = list(open(dir + 'expert.txt', "r").readlines())
    expert_examples = [s.strip() for s in expert_examples]
    paper_examples = list(open(dir + 'paper.txt', "r").readlines())
    paper_examples = [s.strip() for s in paper_examples]
    conference_examples = list(open(dir + 'conference.txt', "r").readlines())
    conference_examples = [s.strip() for s in conference_examples]

    data_train = dict()
    data_train['data'] = expert_examples + paper_examples + conference_examples
    target = [0 for x in expert_examples] + [1 for x in paper_examples] + [2 for x in conference_examples]
    data_train['target'] = target
    data_train['target_names'] = ['expert', 'paper', 'conference']

    return data_train

def get_datasets_intentst(dir):

    expert_examples = list(open(dir + 'expertt.txt', "r").readlines())
    expert_examples = [s.strip() for s in expert_examples]
    paper_examples = list(open(dir + 'papert.txt', "r").readlines())
    paper_examples = [s.strip() for s in paper_examples]
    conference_examples = list(open(dir + 'conferencet.txt', "r").readlines())
    conference_examples = [s.strip() for s in conference_examples]

    data_train = dict()
    data_train['data'] = expert_examples + paper_examples + conference_examples
    target = [0 for x in expert_examples] + [1 for x in paper_examples] + [2 for x in conference_examples]
    data_train['target'] = target
    data_train['target_names'] = ['expert', 'paper', 'conference']

    return data_train

def get_datasets_mrpolarity(positive_data_file, negative_data_file):
    """
    Loads MR polarity data from files, splits the data into words and generates labels.
    Returns split sentences and labels.
    """
    # Load data from files
    positive_examples = list(open(positive_data_file, "r").readlines())
    positive_examples = [s.strip() for s in positive_examples]
    negative_examples = list(open(negative_data_file, "r").readlines())
    negative_examples = [s.strip() for s in negative_examples]

    datasets = dict()
    datasets['data'] = positive_examples + negative_examples
    target = [0 for x in positive_examples] + [1 for x in negative_examples]
    datasets['target'] = target
    datasets['target_names'] = ['positive_examples', 'negative_examples']
    return datasets

def get_datasets_localdata(container_path=None, categories=None, load_content=True,
                       encoding='utf-8', shuffle=True, random_state=42):
    """
    Load text files with categories as subfolder names.
    Individual samples are assumed to be files stored a two levels folder structure.
    :param container_path: The path of the container
    :param categories: List of classes to choose, all classes are chosen by default (if empty or omitted)
    :param shuffle: shuffle the list or not
    :param random_state: seed integer to shuffle the dataset
    :return: data and labels of the dataset
    """
    datasets = load_files(container_path=container_path, categories=categories,
                          load_content=load_content, shuffle=shuffle, encoding=encoding,
                          random_state=random_state)
    return datasets

def load_data_labels(datasets):
    """
    Load data and labels
    :param datasets:
    :return:
    """
    # Split by words
    x_text = datasets['data']
    x_text = [clean_str(sent) for sent in x_text]
    # Generate labels
    labels = []
    for i in range(len(x_text)):
        label = [0 for j in datasets['target_names']]
        label[datasets['target'][i]] = 1
        labels.append(label)
    y = np.array(labels)
    return [x_text, y]

def load_embedding_vectors_word2vec(vocabulary, filename, binary):
    # load embedding_vectors from the word2vec
    encoding = 'utf-8'
    with open(filename, "rb") as f:
        header = f.readline()
        vocab_size, vector_size = map(int, header.split())
        # initial matrix with random uniform
        embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), vector_size))
        if binary:
            binary_len = np.dtype('float32').itemsize * vector_size
            for line_no in range(vocab_size):
                word = []
                while True:
                    ch = f.read(1)
                    if ch == b' ':
                        break
                    if ch == b'':
                        raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
                    if ch != b'\n':
                        word.append(ch)
                word = str(b''.join(word), encoding=encoding, errors='strict')
                idx = vocabulary.get(word)
                if idx != 0:
                    embedding_vectors[idx] = np.fromstring(f.read(binary_len), dtype='float32')
                else:
                    f.seek(binary_len, 1)
        else:
            for line_no in range(vocab_size):
                line = f.readline()
                if line == b'':
                    raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
                parts = str(line.rstrip(), encoding=encoding, errors='strict').split(" ")
                if len(parts) != vector_size + 1:
                    raise ValueError("invalid vector on line %s (is this really the text format?)" % (line_no))
                word, vector = parts[0], list(map('float32', parts[1:]))
                idx = vocabulary.get(word)
                if idx != 0:
                    embedding_vectors[idx] = vector
        f.close()
        return embedding_vectors

def load_embedding_vectors_glove(vocabulary, filename, vector_size):
    # load embedding_vectors from the glove
    # initial matrix with random uniform
    embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), vector_size))
    f = open(filename)
    for line in f:
        values = line.split()
        word = values[0]
        vector = np.asarray(values[1:], dtype="float32")
        idx = vocabulary.get(word)
        if idx != 0:
            embedding_vectors[idx] = vector
    f.close()
    return embedding_vectors

def load_char_embedding(vocabulary):
    alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}"
    one_hot = np.eye(26, dtype='int64')
    embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), 26))
    zero = np.zeros(26)
    for i in range(len(alphabet)):
        idx = vocabulary.get(alphabet[i])
        if i < 26:
            embedding_vectors[idx] = one_hot[i]
        else:
            embedding_vectors[idx] = zero
    return embedding_vectors


from flask import Blueprint, json
from flask_restful import Api
from werkzeug.exceptions import HTTPException
from api.tracks.controllers import Tracks, Coordinates, TrackInfo, Sensors

api_tracks_bp = Blueprint("api", __name__)
api_tracks = Api(api_tracks_bp)

# Add resources
api_tracks.add_resource(Tracks, "/tracks")
api_tracks.add_resource(TrackInfo, "/tracks/<uuid:track_uuid>",
                        endpoint="track_info")
api_tracks.add_resource(Coordinates, "/tracks/<uuid:track_uuid>/coordinates")
api_tracks.add_resource(Sensors, "/tracks/<uuid:track_uuid>/sensors")

# JSON format for error
@api_tracks_bp.errorhandler(HTTPException)
def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    response = e.get_response()
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response


"""
1、学习目标
    掌握name元素定位方法
2、操作步骤（语法）
    driver.find_element_by_name("name的属性值")
"""

from selenium import webdriver
import time

driver = webdriver.Chrome()

url = "http://192.168.0.90/zfcapp/index.html#login"
driver.get(url)

time.sleep(2)

companyID = driver.find_element_by_name("企业ID")

print(companyID.get_attribute("outerHTML"))

companyID.send_keys("2")

time.sleep(3)

driver.quit()


import argparse
import pickle

import numpy as np
import torch

parser = argparse.ArgumentParser()
parser.add_argument('--query', type=str, default='submit_query.csv', help='48-bit hash code file for query set.')
parser.add_argument('--gallery', type=str, default='submit_gallery.csv', help='48-bit hash code file for gallery set.')
parser.add_argument('--submit', type=str, default='submit.csv', help='Final submit csv file.')
parser.add_argument('--k', type=int, default=20, help='Topk == 20.')
args = parser.parse_args()

k = args.k
query_code_path = args.query
gallery_code_path = args.gallery
submit_path = args.submit

with open('/home/hdd/ct_RecallatK_surrogate/src/df_query0_swinL_ddp_192-48.pkl', 'rb') as f:
    df_query = pickle.load(f)
with open('/home/hdd/ct_RecallatK_surrogate/src/df_gallery0_swinL_ddp_192-48.pkl', 'rb') as f:
    df_gallery = pickle.load(f)

query_code =torch.Tensor(list(df_query.values()))
query_code = torch.nn.functional.normalize(query_code, p=2, dim=1)
query_images = list(df_query.keys())

gallery_code = torch.Tensor(list(df_gallery.values()))
gallery_code = torch.nn.functional.normalize(gallery_code, p=2, dim=1)
gallery_images = list(df_gallery.keys())

with open(submit_path, 'w') as f:
    f.write('Id,Predicted\n')
    for i, q in enumerate(query_code):
        cos_sim = q@gallery_code.T
        _,index = torch.topk(cos_sim, k, largest=True)
        retrieval_images = [gallery_images[j][:-4] for j in index]
        f.write(query_images[i] + ',')
        f.write(' '.join(retrieval_images) + '\n')
        print(f'\r writing {i + 1}/{query_code.shape[0]}', end='')


# Importing the necessary Libraries
from flask_cors import CORS,cross_origin
from imagescrapperservice.ImageScrapperService import ImageScrapperService
from imagescrapper.ImageScrapper import ImageScrapper
from flask import Flask, render_template, request,jsonify
import os

# import request
app = Flask(__name__) # initialising the flask app with the name 'app'

#response = 'Welcome!'

@app.route('/')  # route for redirecting to the home page
@cross_origin()
def home():
    return render_template('index.html')

@app.route('/showImages') # route to show the images on a webpage
@cross_origin()
def show_images():
    scraper_object=ImageScrapper() #Instantiating the object of class ImageScrapper
    list_of_jpg_files=scraper_object.list_only_jpg_files('static') # obtaining the list of image files from the static folder
    print(list_of_jpg_files)
    try:
        if(len(list_of_jpg_files)>0): # if there are images present, show them on a wen UI
            return render_template('showImage.html',user_images = list_of_jpg_files)
        else:
            return "Please try with a different string" # show this error message if no images are present in the static folder
    except Exception as e:
        print('no Images found ', e)
        return "Please try with a different string"

@app.route('/searchImages', methods=['GET','POST'])
def searchImages():
    if request.method == 'POST':
        print("entered post")
        keyWord = request.form['keyword'] # assigning the value of the input keyword to the variable keyword

    else:
        print("did not enter post")
    print('printing = ' + keyWord)

    scraper_object = ImageScrapper() # instantiating the class
    list_of_jpg_files = scraper_object.list_only_jpg_files('static') # obtaining the list of image files from the static folder
    scraper_object.delete_existing_image(list_of_jpg_files) # deleting the old image files stored from the previous search
    # splitting and combining the keyword for a string containing multiple words
    image_name = keyWord.split()
    image_name = '+'.join(image_name)
    # adding the header metadata
    header = {
        'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}

    service = ImageScrapperService  # instantiating the object of class ImageScrapperService
    # (imageURLList, header, keyWord, fileLoc)
    masterListOfImages = service.downloadImages(keyWord, header) # getting the master list from keyword
    imageList = masterListOfImages[0] # extracting the list of images from the master list
    # imageTypeList = masterListOfImages[1] # extracting the list of type of images from the masterlist

    response = "We have downloaded ", len(imageList), "images of " + image_name + " for you"

    return show_images() # redirect the control to the show images method

port = int(os.getenv("PORT"))
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=port)
    # app.run(debug=True)


from collections import deque
import sys

n = int(sys.stdin.readline())   # 노드의 수
m = int(sys.stdin.readline())   # 연결된 노드
graph = [ [] for x in range(n+1)]
visited = [False] * (n+1)
# 1번 컴퓨터에서 시작해 7번 컴퓨터까지 표시하기 위해 +1을 진행
# ==> 0번부터 7번까지 생성되어 1번 인덱스부터 사용

for i in range(m):
    a, b = map(int, sys.stdin.readline().split())
    graph[a] += [b]
    graph[b] += [a]
    # 무향 그래프 생성 => 서로 연결

def BFS(Graph, start):
    deq = deque([start])
    visited[start] = True

    while deq:
        v = deq.popleft()
        for i in Graph[v]:
            if not visited[i]:
                visited[i] = True
                deq.append(i)

BFS(graph, 1)

print(visited.count(True)-1)


import sys
import logging
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from genotypes import PRIMITIVES, Genotype
from operations import *
from utils import process_step_vector, process_step_matrix, prune
from torch.distributions.dirichlet import Dirichlet
from torch.distributions.kl import kl_divergence

def channel_shuffle(x, groups):
    batchsize, num_channels, height, width = x.data.size()
    channels_per_group = num_channels // groups
    # reshape
    x = x.view(batchsize, groups,
               channels_per_group, height, width)
    x = torch.transpose(x, 1, 2).contiguous()
    # flatten
    x = x.view(batchsize, -1, height, width)
    return x

class MixedOp(nn.Module):

  def __init__(self, C, stride, k):
    super(MixedOp, self).__init__()
    self.k = k
    self.C = C
    self._ops = nn.ModuleList()
    self.mp = nn.MaxPool2d(2,2)

    for primitive in PRIMITIVES:
      op = OPS[primitive](C //self.k, stride, False)
      # if 'pool' in primitive:
      #   op = nn.Sequential(op, nn.BatchNorm2d(C //self.k, affine=False))
      self._ops.append(op)

  def forward(self, x, weights):
    dim_2 = x.shape[1]
    xtemp = x[ : , :  dim_2//self.k, :, :]
    xtemp2 = x[ : ,  dim_2//self.k:, :, :]
    temp1 = sum(w * op(xtemp) for w, op in zip(weights, self._ops) if not w == 0)
    if self.k == 1:
      return temp1
    #reduction cell needs pooling before concat
    if temp1.shape[2] == x.shape[2]:
      ans = torch.cat([temp1,xtemp2],dim=1)
    else:
      ans = torch.cat([temp1,self.mp(xtemp2)], dim=1)
    ans = channel_shuffle(ans,self.k)
    #ans = torch.cat([ans[ : ,  dim_2//4:, :, :],ans[ : , :  dim_2//4, :, :]],dim=1)
    #except channe shuffle, channel shift also works
    return ans

  def wider(self, k):
    self.k = k
    for op in self._ops:
      op.wider(self.C//k, self.C//k)

class Cell(nn.Module):

  def __init__(self, steps, multiplier, C_prev_prev, C_prev, C, reduction, reduction_prev, k):
    super(Cell, self).__init__()
    self.reduction = reduction
    self.k = k

    if reduction_prev:
      self.preprocess0 = FactorizedReduce(C_prev_prev, C, affine=False)
    else:
      self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1, 1, 0, affine=False)
    self.preprocess1 = ReLUConvBN(C_prev, C, 1, 1, 0, affine=False)
    self._steps = steps
    self._multiplier = multiplier

    self._ops = nn.ModuleList()
    self._bns = nn.ModuleList()
    for i in range(self._steps):
      for j in range(2+i):
        stride = 2 if reduction and j < 2 else 1
        op = MixedOp(C, stride, self.k)
        self._ops.append(op)

  def forward(self, s0, s1, weights):
    s0 = self.preprocess0(s0)
    s1 = self.preprocess1(s1)

    states = [s0, s1]
    offset = 0
    for i in range(self._steps):
      s = sum(self._ops[offset+j](h, weights[offset+j]) for j, h in enumerate(states))
      offset += len(states)
      states.append(s)

    return torch.cat(states[-self._multiplier:], dim=1)

  def wider(self, k):
    self.k = k
    for op in self._ops:
      op.wider(k)

class Network(nn.Module):

  def __init__(self, C, num_classes, layers, criterion, steps=4, multiplier=4, stem_multiplier=3, k=4,
               reg_type='l2', reg_scale=1e-3):
    super(Network, self).__init__()
    self._C = C
    self._num_classes = num_classes
    self._layers = layers
    self._criterion = criterion
    self._steps = steps
    self._multiplier = multiplier
    self.k = k

    C_curr = stem_multiplier*C
    self.stem = nn.Sequential(
      nn.Conv2d(3, C_curr, 3, padding=1, bias=False),
      nn.BatchNorm2d(C_curr)
    )

    C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
    self.cells = nn.ModuleList()
    reduction_prev = False
    for i in range(layers):
      if i in [layers//3, 2*layers//3]:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      cell = Cell(steps, multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, k)
      reduction_prev = reduction
      self.cells += [cell]
      C_prev_prev, C_prev = C_prev, multiplier*C_curr

    self.global_pooling = nn.AdaptiveAvgPool2d(1)
    self.classifier = nn.Linear(C_prev, num_classes)

    self._initialize_alphas()

    #### reg
    self.reg_type = reg_type
    self.reg_scale = reg_scale
    self.anchor_normal = Dirichlet(torch.ones_like(self.alphas_normal).cuda())
    self.anchor_reduce = Dirichlet(torch.ones_like(self.alphas_reduce).cuda())

  def new(self):
    model_new = Network(self._C, self._num_classes, self._layers, self._criterion).cuda()
    for x, y in zip(model_new.arch_parameters(), self.arch_parameters()):
        x.data.copy_(y.data)
    return model_new

  def show_arch_parameters(self):
    with torch.no_grad():
      logging.info('alphas normal :\n{:}'.format(process_step_matrix(self.alphas_normal, 'softmax', self.mask_normal).cpu()))
      logging.info('alphas reduce :\n{:}'.format(process_step_matrix(self.alphas_reduce, 'softmax', self.mask_reduce).cpu()))
      logging.info('concentration normal:\n{:}'.format((F.elu(self.alphas_normal) + 1).cpu()))
      logging.info('concentration reduce:\n{:}'.format((F.elu(self.alphas_reduce) + 1).cpu()))

  def pruning(self, num_keep):
    with torch.no_grad():
      self.mask_normal = prune(self.alphas_normal, num_keep, self.mask_normal)
      self.mask_reduce = prune(self.alphas_reduce, num_keep, self.mask_reduce)

  def wider(self, k):
    self.k = k
    for cell in self.cells:
      cell.wider(k)

  def forward(self, input):
    s0 = s1 = self.stem(input)

    weights_normal = process_step_matrix(self.alphas_normal, 'dirichlet', self.mask_normal)
    weights_reduce = process_step_matrix(self.alphas_reduce, 'dirichlet', self.mask_reduce)
    if not self.mask_normal is None:
      assert (weights_normal[~self.mask_normal] == 0.0).all()
    if not self.mask_reduce is None:
      assert (weights_reduce[~self.mask_reduce] == 0.0).all()

    for i, cell in enumerate(self.cells):
      if cell.reduction:
        weights = weights_reduce
      else:
        weights = weights_normal
      s0, s1 = s1, cell(s0, s1, weights)
    out = self.global_pooling(s1)
    logits = self.classifier(out.view(out.size(0),-1))
    return logits

  def _loss(self, input, target):
    logits = self(input)
    loss = self._criterion(logits, target)
    if self.reg_type == 'kl':
      loss += self._get_kl_reg()
    return loss

  def _get_kl_reg(self):
    cons_normal = (F.elu(self.alphas_normal) + 1)
    cons_reduce = (F.elu(self.alphas_reduce) + 1)
    q_normal = Dirichlet(cons_normal)
    q_reduce = Dirichlet(cons_reduce)
    p_normal = self.anchor_normal
    p_reduce = self.anchor_reduce
    kl_reg = self.reg_scale * (torch.sum(kl_divergence(q_reduce, p_reduce)) + \
                               torch.sum(kl_divergence(q_normal, p_normal)))
    return kl_reg

  def _initialize_alphas(self):
    k = sum(1 for i in range(self._steps) for n in range(2+i))
    num_ops = len(PRIMITIVES)

    self.alphas_normal = Variable(1e-3*torch.randn(k, num_ops).cuda(), requires_grad=True)
    self.alphas_reduce = Variable(1e-3*torch.randn(k, num_ops).cuda(), requires_grad=True)
    self._arch_parameters = [
      self.alphas_normal,
      self.alphas_reduce,
    ]
    self.mask_normal = None
    self.mask_reduce = None

  def arch_parameters(self):
    return self._arch_parameters

  def genotype(self):

    def _parse(weights):
      gene = []
      n = 2
      start = 0
      for i in range(self._steps):
        end = start + n
        W = weights[start:end].copy()
        # edges = sorted(range(i + 2), key=lambda x: -max(W[x][k] for k in range(len(W[x])) if k != PRIMITIVES.index('none')))[:2]
        edges = sorted(range(i + 2), key=lambda x: -max(W[x][k] for k in range(len(W[x]))))[:2]
        for j in edges:
          k_best = None
          for k in range(len(W[j])):
            # if k != PRIMITIVES.index('none'):
            if k_best is None or W[j][k] > W[j][k_best]:
              k_best = k
          gene.append((PRIMITIVES[k_best], j))
        start = end
        n += 1
      return gene

    # gene_normal = _parse(F.softmax(self.alphas_normal, dim=-1).data.cpu().numpy())
    # gene_reduce = _parse(F.softmax(self.alphas_reduce, dim=-1).data.cpu().numpy())
    gene_normal = _parse(process_step_matrix(self.alphas_normal, 'softmax',
                                             self.mask_normal).data.cpu().numpy())
    gene_reduce = _parse(process_step_matrix(self.alphas_reduce, 'softmax',
                                             self.mask_reduce).data.cpu().numpy())

    concat = range(2+self._steps-self._multiplier, self._steps+2)
    genotype = Genotype(
      normal=gene_normal, normal_concat=concat,
      reduce=gene_reduce, reduce_concat=concat
    )
    return genotype