# In this example, we assume that the active document in SAMSON
# contains a number of structural models representing a trajectory of a bio molecule

import pandas as pd
from compute_rmsd import compute_rmsd_2mol

indexer = SAMSON.getNodes('n.t molecule')                       # get all molecules in the active document

df = pd.DataFrame()                                             # create pandas data frame

if indexer.size < 2:
	print("less than two molecules are present in the data graph")
else:                                                           # we assume here that we need to compute RMSD between the first two molecules
	goal = indexer[-1]                                      	# get the goal molecule [assume the last molecule to be the goal one]
	indexer.removeNode(goal)                                	# remove the target node from the indexer
	for t in range(indexer.size):
		mol = indexer[t]
		rmsd, rmsd_info = compute_rmsd_2mol(goal, mol)  		# compute RMSD between two conformations
		# fill in the data frame
		for j in range(len(rmsd)):
			df = df.append( pd.DataFrame({'name': [mol.name], 'info': rmsd_info[j], 'RMSD': rmsd[j], 'timestep': t }) )

df.head()                                                       # show the first several rows

#######################
# plot using matplotlib
import matplotlib.pyplot as plt

if df.size > 0 :
	df_all = df.groupby('info').get_group('all')            	# group by info of RMSD and get RMSD computed based on all atoms
	df_bb  = df.groupby('info').get_group('backbone')       	# group by info of RMSD and get RMSD computed based on backbone atoms
	df_CA  = df.groupby('info').get_group('Calpha')         	# group by info of RMSD and get RMSD computed based on C-alpha atoms
	line_RMSD_all, = plt.plot(df_all["timestep"], df_all["RMSD"], label="all")
	line_RMSD_bb,  = plt.plot(df_bb ["timestep"], df_bb ["RMSD"], label="backbone")
	line_RMSD_CA,  = plt.plot(df_CA ["timestep"], df_CA ["RMSD"], label="C-alpha")
	plt.ylabel('RMSD, Angstrom')
	plt.xlabel('timestep')
	plt.legend(handles = [line_RMSD_all, line_RMSD_bb, line_RMSD_CA])
	plt.grid()
	plt.show()

#######################
# plot using bokeh
# the plot will be openned in a default browser
from bokeh.plotting import figure, show

if df.size > 0 :
	p = figure(title="RMSD")
	p.line(x=df_all["timestep"], y=df_all["RMSD"], color='red',   legend_label='all')
	p.line(x=df_bb ["timestep"], y=df_bb ["RMSD"], color='blue',  legend_label='backbone')
	p.line(x=df_CA ["timestep"], y=df_CA ["RMSD"], color='green', legend_label='C-alpha')
	show(p)


# ImgurToFolder Configuration
configuration = {
    # Client ID here
    'client_id': '',

    # Client Secret here
    'client_secret': '',

    # Refresh Token here.
    # If no refresh token, one could be generated for you by using
    # '--download-all-favorites' argument
    'refresh_token': '',

    # Path to destination folder.
    # If left alone, it will download to current folder
    # when application is running
    'download_folder_path': r'.',

    # This argument will put single images within a folder called
    # 'Single Images'. If you want this setting off replace True with False
    'single_images_folder': True
}


# coding:utf-8
__author__ = 'Dalaoshu'

from PyQt5 import QtWidgets

import sys

class win(QtWidgets.QtWidget):
    def __init__(self):
        super(win,self).__init__()

app = QtWidgets.QApplication(sys.argv)
windows = win
win.show()
sys.exit(app.exec_())
# if __name__ == '__main__':


from typing import List
from logger import logger
from ..file_utils import file_exists, dir_exists
from ..path_utils import get_dirpath_from_filepath

def check_type(item, valid_type_list: list, var_name: str=None):
    if type(item) not in valid_type_list:
        if var_name:
            logger.error(f"Variable Name: {var_name}")
        logger.error(f"Invalid type: {type(item)}")
        logger.error(f"Valid types: {valid_type_list}")
        raise TypeError

def check_type_from_list(item_list: list, valid_type_list: list, var_names: List[str]=None):
    check_type(item_list, valid_type_list=[list])
    if var_names:
        for item, var_name in zip(item_list, var_names):
            check_type(item=item, valid_type_list=valid_type_list, var_name=var_name)
    else:
        for item in item_list:
            check_type(item=item, valid_type_list=valid_type_list)

def check_issubclass(item, valid_parent_class_list: list, var_name: str=None):
    class_type = item if type(item) is type else type(item)
    if not issubclass(class_type, tuple(valid_parent_class_list)):
        if var_name:
            logger.error(f'Variable Name: {var_name}')
        logger.error(f'Invalid Class: {item.__class__}')
        logger.error(f'Class must be a subclass of one of the following:\n{valid_parent_class_list}')
        raise TypeError

def check_issubclass_from_list(item_list: list, valid_parent_class_list: list, var_names: List[str]=None):
    check_type(item_list, valid_type_list=[list])
    if var_names:
        for item, var_name in zip(item_list, var_names):
            check_issubclass(item=item, valid_parent_class_list=valid_parent_class_list, var_name=var_name)
    else:
        for item in item_list:
            check_issubclass(item=item, valid_parent_class_list=valid_parent_class_list)

def check_value(item, valid_value_list: list, var_name: str=None):
    if item not in valid_value_list:
        if var_name:
            logger.error(f"Variable Name: {var_name}")
        logger.error(f"Invalid value: {item}")
        logger.error(f"Valid values: {valid_value_list}")
        raise TypeError

def check_value_from_list(item_list: list, valid_value_list: list, var_names: List[str]=None):
    if var_names:
        for item, var_name in zip(item_list, var_names):
            check_value(item=item, valid_value_list=valid_value_list, var_name=var_name)
    else:
        for item in item_list:
            check_value(item=item, valid_value_list=valid_value_list)

def check_file_exists(filepath: str):
    if not file_exists(filepath):
        logger.error(f"File not found: {filepath}")
        raise Exception

def check_filepath_list_exists(filepath_list: list):
    for filepath in filepath_list:
        check_type(item=filepath, valid_type_list=[str])
        check_file_exists(filepath=filepath)

def check_dir_exists(dirpath: str):
    if not dir_exists(dirpath):
        logger.error(f"Directory not found: {dirpath}")
        raise Exception

def check_dirpath_list_exists(dirpath_list: list):
    for dirpath in dirpath_list:
        check_type(item=dirpath, valid_type_list=[str])
        check_dir_exists(dirpath=dirpath)

def check_input_path_and_output_dir(input_path: str, output_path: str):
    check_file_exists(filepath=input_path)
    output_dir = get_dirpath_from_filepath(filepath=output_path)
    check_dir_exists(dirpath=output_dir)

def check_list_length(item_list: list, correct_length: int, ineq_type: str='eq'):
    list_length = len(item_list)
    if ineq_type.lower() in ['eq', 'equal', 'e', 'equal_to', 'et']:
        if not list_length == correct_length:
            logger.error(f"Expected list length == {correct_length}. Encountered {list_length}")
            raise Exception
    elif ineq_type.lower() in ['gt', 'greater', 'g', 'greater_than']:
        if not list_length > correct_length:
            logger.error(f"Expected list length > {correct_length}. Encountered {list_length}")
            raise Exception
    elif ineq_type.lower() in ['lt', 'less', 'l', 'less_than']:
        if not list_length < correct_length:
            logger.error(f"Expected list length < {correct_length}. Encountered {list_length}")
            raise Exception
    elif ineq_type.lower() in ['ge', 'greater_than_or_equal_to']:
        if not list_length >= correct_length:
            logger.error(f"Expected list length >= {correct_length}. Encountered {list_length}")
            raise Exception
    elif ineq_type.lower() in ['le', 'less_than_or_equal_to']:
        if not list_length <= correct_length:
            logger.error(f"Expected list length <= {correct_length}. Encountered {list_length}")
            raise Exception
    else:
        logger.error(f"Invalid ineq_type: {ineq_type}")
        raise Exception

def check_key_in_dict(item_dict: dict, key):
    if key not in item_dict:
        logger.error(f"Key {key} not found in dictionary.")
        raise Exception

def check_key_not_in_dict(item_dict: dict, key):
    if key in item_dict:
        logger.error(f"Key {key} already exists in dictionary.")
        raise Exception

def check_required_keys(item_dict: dict, required_keys: list, var_name: str=None):
    missing_keys = []
    provided_keys = []
    for required_key in required_keys:
        if required_key not in item_dict.keys():
            missing_keys.append(required_key)
        else:
            provided_keys.append(required_key)
    if len(missing_keys) > 0:
        if var_name:
            logger.error(f"Variable Name: {var_name}")
        logger.error(f"Required keys are missing from item_dict.")
        logger.error(f"provided_keys: {provided_keys}")
        logger.error(f"missing_keys: {missing_keys}")
        raise KeyError


secret_word = "giraffe"

guess = ""

i = 1
while guess != secret_word:
    if i <= 1:
        i += 1
        guess = input("guess the secret work \n Hint: Its an animal with a long neck \n Enter:")
    elif i > 1:
        i += 1
        guess = input("WRONG \n guess the secret work \n Hint: Its an animal with a long neck \n Enter:")
print("You win!")


import calendar
import datetime

from gasofo import func_as_provider

class Clock(object):
    """Shared clock."""

    @staticmethod
    def get_current_ts():
        now = datetime.datetime.utcnow()
        ts = calendar.timegm(now.utctimetuple())
        return ts

def get_clock_provider():
    return func_as_provider(func=Clock.get_current_ts, port='get_current_ts')


#  Training and test
#  Codes have been tested successfully on Python 3.6.0 with TensorFlow 1.14.0.
from __future__ import print_function
import tensorflow as tf
import numpy as np
import scipy.io as sio
from scipy import integrate
import time
import math
from PENN import MLP, standard_scale, get_random_block_from_data

# constant for caculating SOP
def f(x):
    return 1/(1+pow(x,3.7/2))
Lamd = 5/pow(250,2)/math.pi
Lamd_u = 5/pow(250,2)/math.pi
R0 = 2 * pow(10,6)
W= 20 * pow(10,6)
gama0 = pow(2,R0*(1+1.28*Lamd_u/Lamd)/W)-1
p0 = pow(1+Lamd_u/3.5/Lamd,-3.5)
quad_z1,err = integrate.quad(f,pow(gama0,-2/3.7),1000)
Z1 = pow(gama0,2/3.7)*quad_z1
Z2 = math.gamma(1-2/3.7)*math.gamma(1+2/3.7)/(math.gamma(1))*pow(gama0,2/3.7)

# Functions for deep neural network training
def run(X_ini, Y_ini, X_test,Y_test,pf_test,num_H,num_val,N_mont, training_epochs=300, batch_size=5000, LR=0.001, LRdecay=0, K=1, layernum=[2]):
    global mlp
    x = tf.placeholder("float", [None, K])
    y = tf.placeholder("float", [None, K])
    is_train = tf.placeholder("bool")
    block_onehot_train = np.ones([num_H, K])
    block_onehot_val = np.ones([num_val, K])

    total_batch = int(num_H / batch_size)
    N_ini = X_ini.shape[1]
    MSE_train = np.zeros((training_epochs,N_mont))
    MSE_val = np.zeros((training_epochs,N_mont))
    Time = np.zeros((N_mont))
    Ratio = np.zeros((N_mont))
    Ypred = np.zeros((1000,K,N_mont))
    X_test = np.transpose(X_test)
    pf_test = np.transpose(pf_test)
    Y_test = np.transpose(Y_test)
    Ora_meansop = SOP(pf_test,Y_test,0.1*K)
    for i_mont in range(N_mont):

        start_time = time.time()
        # randomly selecting training and validation samples
        flag=np.random.randint(0,N_ini-num_H-num_val)
        X = X_ini[:,flag:flag+num_H]
        Y = Y_ini[:,flag:flag+num_H]
        X_val_ = X_ini[:,flag+num_H:flag+num_H+num_val]
        Y_val_ = Y_ini[:,flag+num_H:flag+num_H+num_val]
        X_train = np.transpose(X)
        Y_train = np.transpose(Y)
        X_val = np.transpose(X_val_)
        Y_val = np.transpose(Y_val_)

        # initilazing neural network
        mlp = MLP(layernum, [1, 10, 1],
                  0.1, 5*K, K, transfer_function=tf.nn.softplus,
                  optimizer=tf.train.AdamOptimizer(LR, 0.9), isdimgeneral=False)
        # training
        for epoch in range(training_epochs):
            for i in range(total_batch):
                idx = np.random.randint(num_H, size=batch_size)
                mlp.optimizer_MSE.run({mlp.x: X_train[idx, :], mlp.y_: Y_train[idx, :],
                                       mlp.block_onehot: block_onehot_train[idx, :],
                                       mlp.keep_prob: 1, mlp.is_train: 1})
                c = mlp.getcost(X=X_train[idx, :], Y=Y_train[idx, :], block_onehot=block_onehot_train[idx, :], keep_prob=1, is_train=0)

            MSE_train[epoch,i_mont] = mlp.getcost(X=X_train, Y=Y_train, block_onehot=block_onehot_train, keep_prob=1, is_train=0) / num_H
            MSE_val[epoch,i_mont] = mlp.getcost(X=X_val, Y=Y_val, block_onehot=block_onehot_val, keep_prob=1, is_train=0) / num_val

            if epoch % (500) == 0:
                print('i_mont:%d, ' % i_mont,'epoch:%d, ' % epoch, 'MSE_train:%f, ' %(MSE_train[epoch,i_mont]),
                      'MSE_val:%f.' %(MSE_val[epoch,i_mont] ))

        Time[i_mont] = time.time() - start_time
        print("training time: %0.2f s" % (Time[i_mont]))

        # testing
        y_pred = mlp.getoutputs(X=X_test,block_onehot=np.ones([X_test.shape[0],K]),keep_prob=1,is_train=0)
        Ypred[:,:,i_mont] = y_pred

        NN_meansop = SOP(pf_test,y_pred,0.1*K)
        Ratio[i_mont] = NN_meansop/Ora_meansop
        print('i_mont:%d, ' % i_mont,'Sop_ratio:%f.' %(Ratio[i_mont]))
        sio.savemat('../Experiments/PENN/NN_Nf'+str(K)+'.mat', {'Ratio': Ratio, 'Pol': Ypred,'MSE_train':MSE_train,'MSE_val':MSE_val,'y_pred': y_pred, 'Y_test':Y_test})
    return Ratio, Time

def SOP(pf,Y,Nc):
    N = Y.shape[0]
    ps = np.zeros((1,N))
    for i in range(N):
        pol = Y[i,:]
        if sum(pol)>Nc:
            pol = pol/sum(pol)*Nc
        pf_one = pf[i,:]
        ps[0,i] = sum( pf_one*pol / ((1-p0)*pol*Z1+(1-p0)*(1-pol)*Z2+pol))
    Mean_SOP = np.mean(ps)
    return Mean_SOP


# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""train"""
import ast
import shutil
import argparse
import os
import time
import numpy as np
import mindspore.nn as nn
import mindspore
from mindspore import Tensor, export
from mindspore import context
from mindspore.common import set_seed
from mindspore.context import ParallelMode
from mindspore.communication.management import init, get_rank
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from src.save_callback import SaveCallback
from src.config import common_config, VehicleNet_train, VeRi_train, VeRi_test
from src.dataset import data_to_mindrecord, create_vehiclenet_dataset
from src.VehicleNet_resnet50 import VehicleNet
from src.lr_generator import lr_steps, lr_steps_2

set_seed(1)

def get_base_param(load_ckpt_path):
    """filter parameters"""
    par_dict = load_checkpoint(load_ckpt_path)
    new_params_dict = {}
    for name in par_dict:
        if 'classifier' not in name:
            new_params_dict[name] = par_dict[name]
    return new_params_dict

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='VehicleNet train.')
    parser.add_argument("--run_distribute", type=ast.literal_eval, default=False,
                        help="Run distribute, default is false.")
    parser.add_argument('--device_id', type=int, default=None,
                        help='device id of GPU or Ascend. (Default: None)')
    parser.add_argument('--device_num', type=int, default=1, help='Number of device.')
    parser.add_argument('--is_modelarts', type=ast.literal_eval, default=True, help='Train in Modelarts.')
    parser.add_argument('--eval_training', type=ast.literal_eval, default=True, help='Eval in training.')
    parser.add_argument('--ckpt_url', type=str, default=None, help='Checkpoint file path')
    parser.add_argument('--data_url', default=None, help='Location of data.')
    parser.add_argument('--train_url', default=None, help='Location of training outputs.')
    parser.add_argument("--file_name", type=str, default="vehiclenet", help="output file name.")
    parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"],
                        default='AIR', help='file format')
    args_opt = parser.parse_args()

    cfg = common_config
    VehicleNet_cfg = VehicleNet_train
    VeRi_cfg = VeRi_train
    VeRi_test_cfg = VeRi_test

    device_target = cfg.device_target
    context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
    if args_opt.run_distribute:
        if device_target == "Ascend":
            device_id = int(os.getenv('DEVICE_ID'))
            context.set_context(device_id=device_id, enable_auto_mixed_precision=True)
            context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL,
                                              gradients_mean=True,
                                              auto_parallel_search_mode="recursive_programming")
            init()
        else:
            raise ValueError("Unsupported platform.")
    else:
        if device_target == "Ascend":
            if args_opt.device_id is not None:
                context.set_context(device_id=args_opt.device_id)
            else:
                context.set_context(device_id=cfg.device_id)
        else:
            raise ValueError("Unsupported platform.")

    train_dataset_path = cfg.dataset_path
    pre_trained_file = cfg.pre_trained_file
    if args_opt.is_modelarts:
        import moxing as mox
        mox.file.copy_parallel(src_url=args_opt.data_url,
                               dst_url='/cache/dataset_train/device_' + os.getenv('DEVICE_ID'))
        zip_command = "unzip -o /cache/dataset_train/device_" + os.getenv('DEVICE_ID') \
                      + "/VehicleNet_mindrecord_v3.zip -d /cache/dataset_train/device_" + os.getenv('DEVICE_ID')
        os.system(zip_command)
        train_dataset_path = '/cache/dataset_train/device_' + os.getenv('DEVICE_ID') + '/VehicleNet/'
        pre_trained_file = '/cache/dataset_train/device_' + os.getenv('DEVICE_ID') + '/resnet50.ckpt'

    mindrecord_dir = cfg.mindrecord_dir
    prefix = "first_train_VehicleNet.mindrecord"
    mindrecord_file = os.path.join(mindrecord_dir, prefix)
    print(mindrecord_file)
    if not os.path.exists(mindrecord_file):
        if not os.path.isdir(mindrecord_dir):
            os.makedirs(mindrecord_dir)
        print("Create mindrecord for train.")
        data_to_mindrecord(train_dataset_path, True, True, False, mindrecord_file)
        print("Create mindrecord done, at {}".format(mindrecord_dir))
    while not os.path.exists(mindrecord_file + ".db"):
        time.sleep(5)

    dataset = create_vehiclenet_dataset(mindrecord_file, batch_size=VehicleNet_cfg.batch_size,
                                        device_num=args_opt.device_num, is_training=True)

    step_per_epoch_first = dataset.get_dataset_size()

    mindrecord_dir = cfg.mindrecord_dir
    prefix = "test_VehicleNet.mindrecord"
    test_mindrecord_file = os.path.join(mindrecord_dir, prefix)
    if not os.path.exists(test_mindrecord_file):
        if not os.path.isdir(mindrecord_dir):
            os.makedirs(mindrecord_dir)
        print("Create mindrecord for test.")
        data_to_mindrecord(eval_dataset_path, False, False, True, test_mindrecord_file)
        print("Create mindrecord done, at {}".format(mindrecord_dir))
    while not os.path.exists(test_mindrecord_file + ".db"):
        time.sleep(5)

    prefix = "query_VehicleNet.mindrecord"
    query_mindrecord_file = os.path.join(mindrecord_dir, prefix)
    if not os.path.exists(query_mindrecord_file):
        if not os.path.isdir(mindrecord_dir):
            os.makedirs(mindrecord_dir)
        print("Create mindrecord for query.")
        data_to_mindrecord(eval_dataset_path, False, False, False, query_mindrecord_file)
        print("Create mindrecord done, at {}".format(mindrecord_dir))
    while not os.path.exists(query_mindrecord_file + ".db"):
        time.sleep(5)

    test_dataset = create_vehiclenet_dataset(test_mindrecord_file, batch_size=1, device_num=1, is_training=False)
    query_dataset = create_vehiclenet_dataset(query_mindrecord_file, batch_size=1, device_num=1, is_training=False)
    test_data_num = test_dataset.get_dataset_size()
    query_data_num = query_dataset.get_dataset_size()

    net = VehicleNet(class_num=VehicleNet_cfg.num_classes)

    net_test = VehicleNet(class_num=VeRi_test_cfg.num_classes)
    net_test.classifier.classifier = nn.SequentialCell()

    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')

    lr_base = lr_steps(0.1 * VehicleNet_cfg.lr_init, VehicleNet_cfg.epoch_size, step_per_epoch_first)
    lr_ignored = lr_steps(VehicleNet_cfg.lr_init, VehicleNet_cfg.epoch_size, step_per_epoch_first)

    base_params = list(filter(lambda x: 'classifier' not in x.name, net.trainable_params()))
    ignored_params = list(filter(lambda x: 'classifier' in x.name, net.trainable_params()))
    train_params = [{'params': base_params, 'lr': Tensor(lr_base, mindspore.float32)},
                    {'params': ignored_params, 'lr': Tensor(lr_ignored, mindspore.float32)}]
    opt = nn.SGD(train_params, momentum=VehicleNet_cfg.momentum, weight_decay=VehicleNet_cfg.weight_decay)

    model = Model(net, loss_fn=loss, optimizer=opt)

    time_cb = TimeMonitor(data_size=step_per_epoch_first)
    loss_cb = LossMonitor()
    cb = [time_cb, loss_cb]
    if cfg.save_checkpoint:
        config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_epochs * step_per_epoch_first,
                                     keep_checkpoint_max=cfg.keep_checkpoint_max)
        if args_opt.is_modelarts:
            save_checkpoint_path = '/cache/train_output/checkpoint'
            if args_opt.device_num == 1:
                ckpt_cb = ModelCheckpoint(prefix='first_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
            if args_opt.device_num > 1 and get_rank() % 8 == 0:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 10, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='first_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
        else:
            save_checkpoint_path = cfg.checkpoint_dir
            if not os.path.isdir(save_checkpoint_path):
                os.makedirs(save_checkpoint_path)

            if args_opt.device_num == 1:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 10, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='first_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
            if args_opt.device_num > 1 and get_rank() % 8 == 0:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 10, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='first_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]

    model.train(VehicleNet_cfg.epoch_size, dataset, callbacks=cb)
    time.sleep(120)

    if args_opt.is_modelarts:
        mox.file.copy_parallel(src_url='/cache/train_output', dst_url=args_opt.train_url)

    mindrecord_dir = cfg.mindrecord_dir
    prefix = "second_train_VehicleNet.mindrecord"
    mindrecord_file = os.path.join(mindrecord_dir, prefix)
    if not os.path.exists(mindrecord_file):
        if not os.path.isdir(mindrecord_dir):
            os.makedirs(mindrecord_dir)
        print("Create mindrecord for train.")
        data_to_mindrecord(train_dataset_path, True, False, False, mindrecord_file)
        print("Create mindrecord done, at {}".format(mindrecord_dir))
    while not os.path.exists(mindrecord_file + ".db"):
        time.sleep(5)

    dataset = create_vehiclenet_dataset(mindrecord_file, batch_size=VeRi_cfg.batch_size,
                                        device_num=args_opt.device_num, is_training=True)
    step_per_epoch_second = dataset.get_dataset_size()

    net = VehicleNet(class_num=VeRi_cfg.num_classes)

    first_trained_file = '/cache/train_output/checkpoint/first_train_vehiclenet-1_' + \
                      str(step_per_epoch_first)  + '.ckpt'
    # first_trained_file = '../../checkpoint/first_train_vehiclenet-80_' + str(step_per_epoch_first) + '.ckpt'

    param_dict = get_base_param(first_trained_file)
    load_param_into_net(net, param_dict)

    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')

    lr_base = lr_steps_2(0.1 * VeRi_cfg.lr_init, VeRi_cfg.epoch_size, step_per_epoch_second)
    lr_ignored = lr_steps_2(VeRi_cfg.lr_init, VeRi_cfg.epoch_size, step_per_epoch_second)

    base_params = list(filter(lambda x: 'classifier' not in x.name, net.trainable_params()))
    ignored_params = list(filter(lambda x: 'classifier' in x.name, net.trainable_params()))
    train_params = [{'params': base_params, 'lr': Tensor(lr_base, mindspore.float32)},
                    {'params': ignored_params, 'lr': Tensor(lr_ignored, mindspore.float32)}]

    opt = nn.SGD(train_params, momentum=VeRi_cfg.momentum, weight_decay=VeRi_cfg.weight_decay)

    model = Model(net, loss_fn=loss, optimizer=opt)

    time_cb = TimeMonitor(data_size=step_per_epoch_second)
    loss_cb = LossMonitor()
    cb = [time_cb, loss_cb]
    if cfg.save_checkpoint:
        config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_epochs * step_per_epoch_second,
                                     keep_checkpoint_max=cfg.keep_checkpoint_max)
        if args_opt.is_modelarts:
            save_checkpoint_path = '/cache/train_output/checkpoint'
            if args_opt.device_num == 1:
                ckpt_cb = ModelCheckpoint(prefix='second_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
            if args_opt.device_num > 1 and get_rank() % 8 == 0:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 10, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='second_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
        else:
            save_checkpoint_path = cfg.checkpoint_dir
            if not os.path.isdir(save_checkpoint_path):
                os.makedirs(save_checkpoint_path)

            if args_opt.device_num == 1:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 5, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='second_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]
            if args_opt.device_num > 1 and get_rank() % 8 == 0:
                if args_opt.eval_training:
                    save_cb = SaveCallback(net_test, test_dataset, query_dataset, 10, VeRi_test_cfg)
                    cb.append(save_cb)
                ckpt_cb = ModelCheckpoint(prefix='second_train_vehiclenet',
                                          directory=save_checkpoint_path,
                                          config=config_ck)
                cb += [ckpt_cb]

    model.train(VeRi_cfg.epoch_size, dataset, callbacks=cb)
    cfg = common_config
    dataset_cfg = VeRi_test
    save_checkpoint_path = '/cache/train_output/checkpoint'
    net = VehicleNet(dataset_cfg.num_classes)
    net.classifier.classifier = nn.SequentialCell()
    file_name1 = "first_train_vehiclenet-1_18697.ckpt"
    param_dict = load_checkpoint(os.path.join(save_checkpoint_path, file_name1))
    load_param_into_net(net, param_dict)
    net.set_train(False)
    input_data = Tensor(np.zeros([1, 3, 384, 384]).astype(np.float32))
    export(net, input_data, file_name=args_opt.file_name, file_format=args_opt.file_format)
    shutil.copy('vehiclenet.air', save_checkpoint_path)
    if args_opt.is_modelarts:
        mox.file.copy_parallel(src_url='/cache/train_output', dst_url=args_opt.train_url)


# !/usr/bin/python
# -*- coding: utf-8 -*-

# 1. 运行游戏后, 玩家需要需要选择语言环境, 1: 英文, 2: 中文 , 其他默认是英文
# 2. 根据玩家的选择, 进入相应的语言环境
# 3. 游戏规则:
# 玩家输入0~9之间的数字, 系统根据玩家的数字, 打印出数字信息
# 如果玩家输入的范围不在0~9, 系统会报错.
# 4. 退出游戏, 游戏会提示退出系统信息

__author__ = 'chenyuan'

language_option = '''
    Language: Choose the language for System[OPTION]
    [1]                    Choose English Language
    [2]                    Choose Chinese Language
'''

enter_str = 'please enter an integer:'

# 开始游戏前, 文字说明
en_game_start_str = 'You choose English language!,Now，Game Start!'
en_game_over_str = 'Game Over!'
en_game_rule_str = 'you should enter a number that from 0 to 9,then the \nSystem will print the information of the number'

cn_game_start_str = '你选择的中文模式！现在，开始游戏!'
cn_game_over_str = '游戏结束！'
cn_game_rule_str = '你输入一个0-9的数字，系统会打印出该数字的信息'

# 方法定义

# 打印信息
def print_info(num):
    if num == 0:
        print('0 zero 零')
    elif num == 1:
        print('1 one 壹')
    elif num == 2:
        print('2 two 贰')
    elif num == 3:
        print('3 three 叁')
    elif num == 4:
        print('4 four 肆')
    elif num == 5:
        print('5 five 伍')
    elif num == 6:
        print('6 six 陆')
    elif num == 7:
        print('7 seven 柒')
    elif num == 8:
        print('8 eight 捌')
    elif num == 9:
        print('9 nine 玖')
    else:
        print('Error!')

# 开始游戏
def start_game(num):
    if num == 1:
        print(en_game_rule_str)
    elif num == 2:
        print(cn_game_rule_str)
    else:
        print(en_game_rule_str)
    n = int(input(enter_str))
    print_info(n)

# 主函数
def main():
    # 提示 language_option
    print(language_option)
    number = int(input(enter_str))
    if number == 1:
        print(en_game_start_str)
        start_game(number)
        print(en_game_over_str)
        exit()
    elif number == 2:
        print(cn_game_start_str)
        start_game(number)
        print(cn_game_over_str)
        exit()
    else:
        print(en_game_start_str)
        start_game(number)
        print(en_game_over_str)
        exit()

# 入口
if __name__ == '__main__':
    main()


import torch
import numpy as np
from sklearn.utils import shuffle

class DataLoader:
    def __init__(self, dataset, batch_size, word2id, shuffle, endless_loop):
        self.dataset = dataset

        self.batch_size = batch_size
        self.word2id = word2id
        self.shuffle = shuffle
        self.endless_loop = endless_loop

        self.reset()

    def __iter__(self):
        return self

    def __next__(self):
        if self.pointer >= len(self.dataset):
            self.reset()
            if not self.endless_loop:
                raise StopIteration()

        batch_x = self.dataset[self.pointer:self.pointer+self.batch_size]
        batch_x = self.pad_batch(batch_x)

        batch_x = torch.tensor(batch_x, dtype=torch.long)

        self.pointer += self.batch_size

        return batch_x

    def reset(self):
        if self.shuffle:
            self.dataset = shuffle(self.dataset)
        self.pointer = 0

    def pad_batch(self, batch_x):
        batch_x = sorted(batch_x, key=lambda x: len(x), reverse=True)

        maximum_length = max([len(x) for x in batch_x])
        batch_x = np.array([x + [self.word2id['<pad>']] * (maximum_length - len(x)) for x in batch_x])

        return batch_x


from dataclasses import dataclass
from typing import List, Set, Optional

from bson import ObjectId

from flags import ProfilePermission
from mongodb.factory import ProfileManager, ChannelManager
from mongodb.helper import IdentitySearcher
from models import ChannelProfileModel

@dataclass
class ProfileControlEntry:
    root_oid: ObjectId
    name: str
    removable: bool

@dataclass
class ChannelProfileEntry:
    profile: ChannelProfileModel
    owner_names: List[str]

    def __post_init__(self):
        self.owner_names = sorted(self.owner_names)

class ProfileHelper:
    @staticmethod
    def get_user_profile_controls(
            channel_model, profile_oid: ObjectId, requester_oid: ObjectId, permissions: Set[ProfilePermission]) \
            -> List[ProfileControlEntry]:
        ret = []

        names = IdentitySearcher.get_batch_user_name(
            ProfileManager.get_profile_user_oids(profile_oid), channel_model)

        remove_self = ProfilePermission.PRF_CONTROL_SELF in permissions
        remove_member = ProfilePermission.PRF_CONTROL_MEMBER in permissions
        is_default = channel_model.config.default_profile_oid == profile_oid

        for uid, name in sorted(names.items(), key=lambda item: item[1]):
            if not name:
                name = str(uid)

            removable = False
            if not is_default:
                if uid == requester_oid:
                    removable = remove_self
                else:
                    removable = remove_member

            ret.append(ProfileControlEntry(root_oid=uid, name=name, removable=removable))

        return ret

    @staticmethod
    def get_channel_profiles(channel_oid: ObjectId, partial_name: Optional[str] = None) -> List[ChannelProfileEntry]:
        ret = []

        # Get channel profiles. Terminate if no available profiles
        profs = list(ProfileManager.get_channel_profiles(channel_oid, partial_name))
        if not profs:
            return ret

        # Get channel data. Terminate if no channel data found
        channel_model = ChannelManager.get_channel_oid(channel_oid)
        if not channel_model:
            return ret

        # Get user names, and the prof-channel dict
        user_oids_dict = ProfileManager.get_profiles_user_oids([prof.id for prof in profs])
        user_oids = []
        for k, v in user_oids_dict.items():
            user_oids.extend(v)
        user_names = IdentitySearcher.get_batch_user_name(user_oids, channel_model)

        for prof in profs:
            uids = user_oids_dict.get(prof.id, [])

            ret.append(ChannelProfileEntry(prof, [user_names.get(uid) for uid in uids]))

        return ret


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 16 20:07:24 2018

@author: wolai
"""
from openpyxl import load_workbook
from openpyxl import Workbook

class ExcelPro:
    def __init__(self,filepath = ''):
        self.filepath = filepath
        if filepath != '':
            self.wb = load_workbook(filepath)
        else:
            self.wb = Workbook()
            self.wb.active
        self.ws = []
        for sheetname in self.wb.sheetnames:
            self.ws.append(self.wb[sheetname])
        self.setsheet(0)

    def creatsheet(self,pos = 0,name = ''):
        if name == '':
            self.ws.append(self.wb.create_sheet())
        else:
            if pos <= len(self.ws):
                self.ws.insert(pos,self.wb.create_sheet(name,pos))
            else:
                self.ws.append(self.wb.create_sheet(name,pos))

    def changesheet(self,pos,name):
        if pos <= len(self.ws):
            del self.wb[str(self.ws[pos])[12:-2]]
            self.ws[pos] = self.wb.create_sheet(name,pos)

    def deletesheet(self,*,pos = 0,name = ''):
        if pos != 0 and pos < len(self.ws):
            del self.wb[str(self.ws[pos])[12:-2]]
            del self.ws[pos]
            return 0
        if name != '':
            del self.wb[name]
            i = 0
            while i < len(self.ws):
                if str(self.ws[i])[12:-2] == name:
                    del self.ws[i]
                    i = i-1
                i = i+1

    def changetabcolor(self,*,pos = 0,color = '888888'):
        if pos < len(self.ws):
            self.ws[pos].sheet_properties.tabColor = color

    def setsheet(self,pos):
        if pos < len(self.ws):
            self.sheetpos = pos
            self.max_row = self.ws[self.sheetpos].max_row
            self.max_column = self.ws[self.sheetpos].max_column
            return 0
        else:
            return -1

    def readrow(self, row):
        rowdata = [0 for x in range(self.max_column)]
        if row <= self.max_row:
            for col in range(self.max_column):
                rowdata[col] = self.readcell(row,col+1)
            return rowdata
        else:
            return -1

    def readcolumn(self, column):
        rowdata = [0 for x in range(self.max_row)]
        if column <= self.max_column:
            for row in range(self.max_row):
                rowdata[row] = self.readcell(row+1,column)
            return rowdata
        else:
            return -1

    def writerow(self, row = 0, *, rowlist = [], rowdict = {}):
        try:
            if row == 0 and rowdict != {}:
                self.ws[self.sheetpos].append(rowdict)
            elif row != 0 and rowlist != []:
                for col in range(len(rowlist)):
                    self.writecell(row,col+1,rowlist[col])
        except:
            return -1

    def readcell(self, row, column):
        try:
            return self.ws[self.sheetpos].cell(row = row, column = column).value
        except:
            return -1

    def writecell(self, row, column, value):
        try:
            self.ws[self.sheetpos].cell(row = row,column = column).value = value
#           sheet.cell(row=3,column=5,value=100)
        except:
            return -1

    def savefile(self,filepath = ''):
        try:
            if filepath == '' and self.filepath != '':
                self.wb.save(self.filepath)
            elif filepath != '':
                self.wb.save(filepath)
        except:
            return -1


"""
1569. Number of Ways to Reorder Array to Get Same BST

Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.

For example, given nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array [2,3,1] also yields the same BST but [3,2,1] yields a different BST.

Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.

Since the answer may be very large, return it modulo 10^9 + 7.

Example 1:

Input: nums = [2,1,3]
Output: 1
Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
Example 2:

Input: nums = [3,4,5,1,2]
Output: 5
Explanation: The following 5 arrays will yield the same BST:
[3,1,2,4,5]
[3,1,4,2,5]
[3,1,4,5,2]
[3,4,1,2,5]
[3,4,1,5,2]
Example 3:

Input: nums = [1,2,3]
Output: 0
Explanation: There are no other orderings of nums that will yield the same BST.
Example 4:

Input: nums = [3,1,2,5,4,6]
Output: 19
Example 5:

Input: nums = [9,4,2,1,3,6,5,7,8,14,11,10,12,13,16,15,17,18]
Output: 216212978
Explanation: The number of ways to reorder nums to get the same BST is 3216212999. Taking this number modulo 10^9 + 7 gives 216212978.

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= nums.length
All integers in nums are distinct.

"""

class WaysReorderArray:

    """
    First, we can bot change the root, otherwise we will get different trees.

    Second, nodes in the left < root, nodes in the rigth subtree > root.

    l = [x in nums if x < nums[0]]
    r = [x in nums if x > nums[0]]

    Interleaving l and r will get the same tree.
    And there are C(|l| + |r|m |l|) ways.

    Do it recursively for l and r

    Total ways = C(|l| + |r|, |l|) * ways(l) * ways(r)
    ans = total_watys(nums) - 1

    for [4, 2, 1, 3, 6, 5]

    coulod be :
    [4, 2, 3, 1, 6, 5]
    [4, 2, 3, 6, 5, 1]...

    Sequence for 2, 1, and 3, 6, 5, would be changed.

    For a given root (first element of the array), we can split the array into left children (nums[i] < nums[0]) and
    right children (nums[i] > nums[0]). Assuming there are l nodes for the left and r nodes for the right.

    We have C(l + r, l) different ways to insert l elements into a (l + r) sized array.
    Within node l / r nodes, we have ways(left) / ways(right) different ways to re-arrange those nodes. So the total # of ways is:

    C(l + r, l) * ways(l) * ways(r)
    Don’t forget to minus one for the final answer.

    Time complexity: O(n^2)
    Space complexity: O(n^2)

    Time complexity: (0(nlogn)) -> O(n^2)
    Space complexity: O(nlog(n)) -> O(n^2)
    """
    def doit_math(self, nums: list) -> int:
        n, hMod = len(nums), 10 ** 9 + 7
        cnk = [[1 for _ in range(n + 1)] for _ in range(n + 1)]
        for i in range(1, n + 1):
            for j in range(1, i):
                cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % hMod

        def search(nums):
            if len(nums) <= 2:
                return 1

            left, right = [], []
            for i in range(1, len(nums)):
                if nums[i] < nums[0]:
                    left.append(nums[i])
                else:
                    right.append(nums[i])

            ans = cnk[len(nums) - 1][len(left)]
            ans = ans * search(left) % hMod
            ans = ans * search(right) % hMod

            return ans

        return search(nums) - 1


import pandas as pd

data = [('Apple', 'Coles', 1.50),
        ('Apple', 'Woolworths', 1.60),
        ('Apple', 'IGA', 1.70),
        ('Banana', 'Coles', 0.50),
        ('Banana', 'Woolworths', 0.60),
        ('Banana', 'IGA', 0.70),
        ('Cherry', 'Coles', 5.00),
        ('Date', 'Coles', 2.00),
        ('Date', 'Woolworths', 2.10),
        ('Elderberry', 'IGA', 10.00)]
df = pd.DataFrame(data, columns=['Fruit', 'Shop', 'Price'])
table1 = df.pivot_table(columns='Shop', index='Fruit', values='Price', aggfunc='sum', fill_value=0)
print(table1)

table1 = df.pivot_table(columns='Shop', index='Fruit', values='Price', aggfunc='count', fill_value=0)
print(table1)


# coding=utf-8
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import GeneradorNumeros as GenNums

# Semilla: Parte entera del promedio de los padrones
semilla = (80560 + 85977) // 2  # type: int
totalValues = 6000
generador = GenNums.GeneradorNumeros()
valores = generador.generar_numeros(semilla, totalValues)

#Tomo los valores de las posiciones pares
array_X = valores[::2]
#Tomo los valores de las posiciones impares
array_Y = valores[1:][::2]

plt.plot(array_X, array_Y, 'ro')
plt.show()

array_X = []
array_Y = []
array_Z = []
index = 0
for value in valores:
    if index == 0:
        array_X.append(value)
        index += 1
    elif index == 1:
        array_Y.append(value)
        index += 1
    else:
        array_Z.append(value)
        index = 0

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(array_X, array_Y, array_Z)
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 23:05:47 2017

@author: mhaa
"""

# Study https://www.analyticsvidhya.com/blog/2017/09/understaing-support-vector-machine-example-code/
# Other references:
#   http://scikit-learn.org/stable/auto_examples/svm/plot_rbf_parameters.html
#   https://www.quora.com/What-are-C-and-gamma-with-regards-to-a-support-vector-machine

import numpy as np
import matplotlib.pyplot as plt

from sklearn import svm, datasets

from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV

def study_linear_and_rbf(X, y):
    C=1
    svc = svm.SVC(kernel='linear', C=C).fit(X, y)
    make_plot(X, y, svc)

    # C:     regularization term for soft margin cost function
    # gamma: small gamma: large influence of x_i on x_j

    gamma=1.0
    C=1
    svc = svm.SVC(kernel='rbf', gamma=gamma, C=C).fit(X, y)
    print(svc)
    make_plot(X, y, svc)

def make_plot(X, y, svc):
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    h = (x_max / x_min)/100
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    plt.subplot(1, 1, 1)
    Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)

    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
    plt.xlabel('Sepal length')
    plt.ylabel('Sepal width')
    plt.xlim(xx.min(), xx.max())
    plt.title('SVC')
    plt.show()

def find_best_hyperparameters(X, y):
    # Cross validation
    C_range = np.logspace(-2, 10, 3)  # was: 13
    gamma_range = np.logspace(-9, 3, 3)
    print('C_range:', C_range)
    print('gamma_range:', gamma_range)
    param_grid = dict(gamma=gamma_range, C=C_range)
    cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
    grid = GridSearchCV(svm.SVC(kernel='rbf'), param_grid=param_grid, cv=cv)
    grid.fit(X, y)

    print("The best parameters are %s with a score of %0.2f"
          % (grid.best_params_, grid.best_score_))

def main():
    iris = datasets.load_iris()
    X = iris.data[:, :2]
    y = iris.target

    # Study some examples by setting gamma and C by hand
    study_linear_and_rbf(X, y)

    # Find best gamma and C by cross validation
    find_best_hyperparameters(X, y)

    print('Done')

if __name__ == '__main__':
    main()


import graphene
from django.core.exceptions import ValidationError
from django.db import transaction

from ...attribute import AttributeInputType
from ...attribute import models as attribute_models
from ...core.permissions import PagePermissions, PageTypePermissions
from ...core.tracing import traced_atomic_transaction
from ...page import models
from ..core.mutations import BaseBulkMutation, ModelBulkDeleteMutation
from ..core.types import NonNullList, PageError
from ..plugins.dataloaders import load_plugin_manager
from .types import Page, PageType

class PageBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = NonNullList(
            graphene.ID, required=True, description="List of page IDs to delete."
        )

    class Meta:
        description = "Deletes pages."
        model = models.Page
        object_type = Page
        permissions = (PagePermissions.MANAGE_PAGES,)
        error_type_class = PageError
        error_type_field = "page_errors"

    @classmethod
    @traced_atomic_transaction()
    def perform_mutation(cls, _root, info, ids, **data):
        try:
            pks = cls.get_global_ids_or_error(ids, only_type=Page, field="pk")
        except ValidationError as error:
            return 0, error
        cls.delete_assigned_attribute_values(pks)
        return super().perform_mutation(_root, info, ids, **data)

    @staticmethod
    def delete_assigned_attribute_values(instance_pks):
        attribute_models.AttributeValue.objects.filter(
            pageassignments__page_id__in=instance_pks,
            attribute__input_type__in=AttributeInputType.TYPES_WITH_UNIQUE_VALUES,
        ).delete()

class PageBulkPublish(BaseBulkMutation):
    class Arguments:
        ids = NonNullList(
            graphene.ID, required=True, description="List of page IDs to (un)publish."
        )
        is_published = graphene.Boolean(
            required=True, description="Determine if pages will be published or not."
        )

    class Meta:
        description = "Publish pages."
        model = models.Page
        object_type = Page
        permissions = (PagePermissions.MANAGE_PAGES,)
        error_type_class = PageError
        error_type_field = "page_errors"

    @classmethod
    def bulk_action(cls, info, queryset, is_published):
        queryset.update(is_published=is_published)

class PageTypeBulkDelete(ModelBulkDeleteMutation):
    class Arguments:
        ids = NonNullList(
            graphene.ID,
            description="List of page type IDs to delete",
            required=True,
        )

    class Meta:
        description = "Delete page types."
        model = models.PageType
        object_type = PageType
        permissions = (PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,)
        error_type_class = PageError
        error_type_field = "page_errors"

    @classmethod
    @traced_atomic_transaction()
    def perform_mutation(cls, _root, info, ids, **data):
        try:
            pks = cls.get_global_ids_or_error(ids, only_type=PageType, field="pk")
        except ValidationError as error:
            return 0, error
        cls.delete_assigned_attribute_values(pks)
        return super().perform_mutation(_root, info, ids, **data)

    @classmethod
    def bulk_action(cls, info, queryset):
        page_types = list(queryset)
        queryset.delete()
        manager = load_plugin_manager(info.context)
        for pt in page_types:
            transaction.on_commit(lambda: manager.page_type_deleted(pt))

    @staticmethod
    def delete_assigned_attribute_values(instance_pks):
        attribute_models.AttributeValue.objects.filter(
            pageassignments__assignment__page_type_id__in=instance_pks,
            attribute__input_type__in=AttributeInputType.TYPES_WITH_UNIQUE_VALUES,
        ).delete()


from wtforms import Form, StringField, RadioField, SelectField, TextAreaField, validators,FileField,BooleanField, FloatField
from wtforms.fields.html5 import DateField,EmailField,TimeField,IntegerField
import shelve
from datetime import date

def list_of_clinics():
    clinics_list = ['']
    list_of_clinics.clinic_id_list = ['']
    try:
        clinic_db = shelve.open('clinic_storage.db', 'r')
        users_dict = clinic_db['Users']
        clinic_db.close()
    except:
        print('No database created yet')
        users_dict = {}
    for clinic_id in users_dict:
        clinics_list.append(users_dict[clinic_id].get_name())
        list_of_clinics.clinic_id_list.append(clinic_id)
    print('hello')
    print(clinics_list)
    return clinics_list

def list_of_clinics_bylocation():
    clinic_location_dict = {}
    try:
        clinic_db = shelve.open('clinic_storage.db', 'r')
        users_dict = clinic_db['Users']
    except:
        print('No database created yet')
        users_dict = {}
    locations = ['Ang Mo Kio', 'Bedok', 'Bishan', 'Braddell', 'Buangkok', 'Bugis', 'Canberra', 'Changi', 'Chinatown', 'Clementi', 'Harbourfront', 'Hougang', 'Jurong', 'Khatib', 'Kovan', 'Marina', 'Orchard', 'Pasir Ris', 'Punggol', 'Sembawang', 'Sengkang', 'Tampines', 'Woodlands', 'Yio Chu Kang', 'Yishun']
    for i in locations:
        clinics_list = ['']
        list_of_clinics_bylocation.clinic_id_list = ['']
        value = [clinics_list, list_of_clinics_bylocation.clinic_id_list]
        clinic_location_dict[i] = [value]
        for clinic_id in users_dict:
            location = users_dict[clinic_id].get_area()
            if location == i:
                clinics_list.append(users_dict[clinic_id].get_name())
                list_of_clinics_bylocation.clinic_id_list.append(clinic_id)
                value = [clinics_list, list_of_clinics_bylocation.clinic_id_list]
                clinic_location_dict[location] = value
        # print(clinics_list)
    clinic_db.close()
    input_location = input('Location: ')
    return(clinic_location_dict[input_location][0])
    # clinic_db.close()
    # return clinics_list

class CreateUserForm(Form):
    name = StringField('Name', [validators.Length(min=1, max=150), validators.DataRequired()])
    nric = StringField('NRIC', [validators.Length(min=9,max=9), validators.DataRequired()])
    email= EmailField('Email',[validators.Length(min=1, max=150), validators.DataRequired()])
    phone= StringField('Contact Number',[validators.Length(min=8,max=8), validators.DataRequired()])
    purpose = SelectField('Purpose', [validators.DataRequired()], choices=[('',''),('M', 'Medical Test/Examination'), ('D', 'Doctor Consultation'), ('C', 'Collection of Documents')], default='')
    date_of_arrival=DateField('Date', format='%Y-%m-%d')
    message = TextAreaField('Message (elaborate on condition or any symptoms)', [validators.Optional()])
    time= TimeField('Time',[validators.DataRequired()])
    clinic = SelectField('Choose Clinic', [validators.DataRequired()], choices=list_of_clinics(), default='')

    def validate_date_of_arrival(form, field):
        today_date = date.today()
        if field.data < today_date:
            raise validators.ValidationError("Appointment date must not be earlier than today's date.")

class UpdateAndCancel(Form):
    name = StringField('Name', [validators.Length(min=1, max=150), validators.DataRequired()])
    nric = StringField('NRIC', [validators.Length(min=9,max=9), validators.DataRequired()])
    email= EmailField('Email',[validators.Length(min=1, max=150), validators.DataRequired()])
    phone= StringField('Contact Number',[validators.Length(min=8,max=8), validators.DataRequired()])
    purpose = SelectField('Purpose', [validators.DataRequired()], choices=[('M', 'Medical Test/Examination'), ('D', 'Doctor Consultation'), ('C', 'Collection of Documents')], default='')
    date_of_arrival=DateField('Date', format='%Y-%m-%d')
    message = TextAreaField('Message (elaborate on condition or any symptoms)', [validators.Optional()])
    time= TimeField('Time',[validators.DataRequired()])
    clinic = SelectField('Choose Clinic', [validators.DataRequired()], choices=list_of_clinics(), default='')

    def validate_date_of_arrival(form, field):
        today_date = date.today()
        if field.data < today_date:
            raise validators.ValidationError("Appointment date must not be earlier than today's date.")

class update_items(Form):
    quantities = IntegerField('Enter Quantity',[validators.DataRequired()])
    qty=StringField('Enter Quantity',[validators.optional()])
    price=IntegerField('',[validators.DataRequired()])
    pricing=StringField('Total Price',[validators.optional()])

class CreateShoppingItems(Form):
    quantity = IntegerField('Enter Product Quantity',[validators.DataRequired(),validators.NumberRange(min=1,max=1000)])
    product_name = SelectField('Select Product', [validators.DataRequired()], choices=[('Paracetamol','Paracetamol'),('Aspirin', 'Aspirin'), ('Antiseptic Cream', 'Antiseptic Cream'), ('Strepsils', 'Strepsils'),('Zyrtec-R', 'Zyrtec-R'),('Bandages', 'Bandages')], default='')
    product_description=TextAreaField('Enter Product Description and Clinic Name',[validators.Length(min=8,max=150), validators.DataRequired()])
    price=FloatField('Enter Product Price',[validators.DataRequired()])

if __name__ == '__main__':
    x = list_of_clinics_bylocation()


import cv2
import glob
from helpers_lane import grayscale
import numpy as np

def camera_calibration(show_images=False):

    nx = 9
    ny = 6

    objPoints = []
    imgPoints = []
    shape = None
    images = glob.glob('./camera_cal/calibration*.jpg')

    # 3D Coordinates
    objP = np.zeros((nx*ny, 3), np.float32)
    objP[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)

    for filename in images:
        image = cv2.imread(filename)
        gray = grayscale(image)
        shape = gray.shape[::-1]
        ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
        if show_images == True:
            image = cv2.drawChessboardCorners(
                image, (nx, ny), corners, ret)
            print(filename)
            # cv2.imshow('Cal', image)
            cv2.waitKey()

        if ret == True:
            imgPoints.append(corners)
            objPoints.append(objP)

    # print(objPoints)
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
        objPoints, imgPoints, shape, None, None)

    return mtx, dist

def undistort_image(img, mtx, dist):
    return cv2.undistort(img, mtx, dist, None, mtx)

def test_calibration():
    mtx, dist = camera_calibration()
    # test Camera Calibration
    testImage = cv2.imread('./camera_cal/calibration2.jpg')
    undistImage = undistort_image(testImage, mtx, dist)
    cv2.imshow("Image", testImage)
    cv2.imshow("Teste Cal", undistImage)
    cv2.imwrite('undistortImage.png', undistImage)
    cv2.waitKey()


class Edge:
    def __init__(self):
        self.Node1 = "1"
        self.Node2 = "2"
        self.Edge = "X"

class CAM:
    def convertToMatrix(self, edges):
        vertices = []
        edge_dict = dict()
        for edge in edges:
            if str(edge[0]) == str(edge[1]):
                continue  # skip iteration because node 1 and node 2 cannot be the same. Doesn't make sense.
            if str(edge[0]) not in vertices:
                vertices.append(str(edge[0]))  # add this to vertex which will be then be used in diagonal order.
            if str(edge[1]) not in vertices:
                vertices.append(str(edge[1]))  # add second node to vertex list
            obj = Edge()
            obj.Node1 = str(edge[0]).strip()
            obj.Node2 = str(edge[1]).strip()
            obj.Edge = str(edge[2]).strip()
            obj1 = Edge()  # there's gotta be a better implementation but no time to fix it...
            obj1.Node1 = str(edge[1]).strip()
            obj1.Node2 = str(edge[0]).strip()
            obj1.Edge = str(edge[2]).strip()
            edge_dict.setdefault(obj.Node1,[]).append(obj)  # add obj to dictionary so it can be found easily.
            edge_dict.setdefault(obj1.Node1,[]).append(obj1)

        # sort the unique vertices
        vertices = sorted(vertices) # python sort by lexicographic order by default. See python documentation

        # initialize 2d matrix
        unique_nodes = len(vertices)
        matrix = [[0 for i in range(unique_nodes)] for i in range(unique_nodes)]

        # setting nodes across diagonally
        x = 0
        for vertex in vertices:
            matrix[x][x] = vertex
            x += 1

        #setting edges now.
        for x in range(1,unique_nodes):
            node2 = matrix[x][x]
            for y in range(0,unique_nodes):
                node1 = matrix[y][y]
                if x == y:
                    break
                if node1 in edge_dict:
                    for item in edge_dict[node1]:
                        if node2 == item.Node2:
                            matrix[x][y] = item.Edge

        # getting awesome Code
        CamCode = ''
        for x in range(0, unique_nodes):
            for y in range(0,unique_nodes):
                if y <= x:
                    CamCode += str(matrix[x][y])
        return matrix, CamCode

    def generateCAM(self,cam1, cam2):

        if(len(cam2) > len(cam1)):  # if cam2 is bigger than swap it.. this will help with case 2
            cam2_matrix = cam2
            cam1_matrix = cam1
        else:
            cam1_matrix = cam2
            cam2_matrix = cam1

        case_used = "No case was used. Return Empty Array."

        core_size_length = min(len(cam1_matrix), len(cam2_matrix)) # this will use smaller matrix size to compare submatrix
        case = 0
        cam1_last_row_edge_count = -1  # to not count node
        cam2_last_row_edge_count = -1  # to not count node
        result_combined_CAM = []

        # Case 1: both A and B have at least two edge entries in the last row.
        # Case 2: A has at least two edge entries but B only has at least one.
        # Case 3: both A and B have one edge entry in the last row.

        for x in cam1_matrix[len(cam1_matrix) - 1]:  # loop through last row to count edges cam1
            if str(x) != "0":
                cam1_last_row_edge_count += 1
        for y in cam2_matrix[len(cam2_matrix) - 1]:  # loop through last row to count edges cam2
            if str(y) != "0":
                cam2_last_row_edge_count += 1

        # If Both A and B have at least two edge entries in last row
        if cam1_last_row_edge_count >= 2 and cam2_last_row_edge_count >= 2:
            # If both sub matrix are the same
            temp_code_cam1 = ''
            temp_code_cam2 = ''
            for x in range(0,core_size_length-1):
                for y in range(0,core_size_length-1):
                    if y <= x:
                        temp_code_cam1 += str(cam1_matrix[x][y])
                        temp_code_cam2 += str(cam2_matrix[x][y])

            if temp_code_cam1 == temp_code_cam2:
                case = 1  # set as case 1
            else:
                # return empty array if sub matrix is not the same.
                result = []
                result_combined_CAM.append(result)

        # If A has at least two edge entries in last row but B only has one
        elif (cam1_last_row_edge_count >= 2 or cam2_last_row_edge_count >= 2) and (cam2_last_row_edge_count == 1 or cam1_last_row_edge_count == 1):
            # If both sub matrix are the same
            temp_code_cam1 = ''
            temp_code_cam2 = ''
            for x in range(0,core_size_length-1):
                for y in range(0,core_size_length-1):
                    if y <= x:
                        temp_code_cam1 += str(cam1_matrix[x][y])
                        temp_code_cam2 += str(cam2_matrix[x][y])

            if temp_code_cam1 == temp_code_cam2:
                case = 2  # set as case 2
            else:
                # return empty array if sub matrix is not the same.
                result = []
                result_combined_CAM.append(result)

            # If both A and B have one edge entry in the last row
        elif cam1_last_row_edge_count == 1 and cam2_last_row_edge_count == 1:
            case = 3
            # If both sub matrix are the same and core is the same
            if cam1_matrix[:core_size_length - 1] == cam2_matrix[:core_size_length - 1]:
                # If last node for both matrix are the same
                if str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) == str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):
                    case_used = 'Case 3a:'
                elif str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) != str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):
                    case_used = 'Cannot join because it is not from the same Graph. '
            elif len(cam1_matrix) != len(cam2_matrix):
                case_used = 'Case 3b: '
            else:
                # return empty array if sub matrix is not the same
                result = []
                result_combined_CAM.append(result)

        if case == 1:
            case_used = 'Case 1:'

            temp_row = []
            # add last row to result.
            # if same size, same node.    (Checked)
            if len(cam1_matrix) == len(cam2_matrix) and str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) == str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):

                # add submatrix to result.
                for row in cam1_matrix[:core_size_length - 1]:
                    result_combined_CAM.append(row)

                for x in range(0, len(cam1_matrix)):
                    if str(cam1_matrix[len(cam1_matrix) - 1][x]) != "0":
                        temp_row.append(cam1_matrix[len(cam1_matrix) - 1][x])
                    elif str(cam1_matrix[len(cam1_matrix) - 1][x]) == "0":
                        temp_row.append(cam2_matrix[len(cam2_matrix) -1][x])
                result_combined_CAM.append(temp_row)

            #if same size, different node. Therefore append new row in matrix with new node
            elif len(cam1_matrix) == len(cam2_matrix) and str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) != str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):
                case_used = "Cannot be joined because last nodes cannot be different or else it is not originating from the same graph."
                temp_row = []
                result_combined_CAM.append(temp_row)
            else:
                case_used = "Cannot be joined because both matrix does not have the same number of edges."
                temp_row = []
                result_combined_CAM.append(temp_row)

        elif case == 2:
            case_used = 'Case 2:'

            temp_row = []
            # add last row to result.
            # if same size, same node.    (Checked)
            if len(cam1_matrix) == len(cam2_matrix) and str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) == str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):

                # add submatrix to result.
                for row in cam1_matrix[:core_size_length - 1]:
                    result_combined_CAM.append(row)

                for x in range(0, len(cam1_matrix)):
                    if str(cam1_matrix[len(cam1_matrix) - 1][x]) != "0":
                        temp_row.append(cam1_matrix[len(cam1_matrix) - 1][x])
                    elif str(cam1_matrix[len(cam1_matrix) - 1][x]) == "0":
                        temp_row.append(cam2_matrix[len(cam2_matrix) - 1][x])
                result_combined_CAM.append(temp_row)

            # if same size, different node. Therefore append new row in matrix with new node
            elif len(cam1_matrix) == len(cam2_matrix) and str(cam1_matrix[len(cam1_matrix) - 1][len(cam1_matrix) - 1]) != str(cam2_matrix[len(cam2_matrix) - 1][len(cam2_matrix) - 1]):
                # create vertices
                vertex = []
                temp_dict_cam1 = {}
                temp_dict_cam2 = {}
                for x in range(0, len(cam1_matrix)):
                    vertex.append(cam1_matrix[x][x])
                    temp_dict_cam1[cam1_matrix[x][x]] = cam1_matrix[x]
                for y in range(0, len(cam2_matrix)):
                    temp_dict_cam2[cam2_matrix[y][y]] = cam2_matrix[y]

                    if cam2_matrix[y][y] not in vertex:
                        vertex.append(cam2_matrix[y][y])

                # generate matrix
                matrix = [[0 for i in range(len(cam2_matrix) + 1)] for i in range(len(cam2_matrix) + 1)]

                # setting nodes diagonally
                x = 0
                for vertex in vertex:
                    matrix[x][x] = vertex
                    x += 1

                for x in range(0, len(matrix)):
                    for y in range(0, len(matrix)):
                        if x == y:
                            break
                        if str(matrix[x][x]) in temp_dict_cam1:
                            if str(temp_dict_cam1.get(matrix[x][x])[y]) != "0":
                                matrix[x][y] = temp_dict_cam1.get(matrix[x][x])[y]
                            else:
                                if str(temp_dict_cam1.get(matrix[x][x])[y]) not in temp_dict_cam2:
                                    continue
                                matrix[x][y] = temp_dict_cam2.get(matrix[x][x])[y]
                        elif str(matrix[x][x]) in temp_dict_cam2:
                            if temp_dict_cam2.get(matrix[x][x])[y] == matrix[x][x]:
                                break
                            matrix[x][y] = temp_dict_cam2.get(matrix[x][x])[y]

                # add matrix to result_combined_CAM
                for row in matrix:
                    result_combined_CAM.append(row)

            # two matrix are not the same size
            else:
                # add matrix 1 to temp dict so it can be used in iteration below...
                temp_dict_cam1 = {}
                temp_dict_cam2 = {}
                for x in range(0, core_size_length):
                    temp_dict_cam1[cam1_matrix[x][x]] = cam1_matrix[x]
                for x in range(0, core_size_length):
                    temp_row = []
                    count = 0
                    for y in cam2_matrix[x]:
                        if cam2_matrix[x][x] in temp_dict_cam1:
                            if count < x:
                                count += 1
                                if str(y) == "0":
                                    # print(y)
                                    # print(temp_dict_cam1.get(cam2_matrix[x][x])[y])
                                    temp_row.append(temp_dict_cam1.get(cam2_matrix[x][x])[y])
                                else:
                                    temp_row.append(y)
                            else:
                                temp_row.append(y)
                    result_combined_CAM.append(temp_row)

                # add unexisted node back in
                for x in range(core_size_length, len(cam2_matrix)):
                    temp_dict_cam2[cam2_matrix[x][x]] = cam2_matrix[x]

                for item in sorted(temp_dict_cam2):
                    result_combined_CAM.append(temp_dict_cam2[item])

        # getting CAM Code
        CamCode = ''
        if case == 3:
            CamCode = "not required to implement functionality so no CAM code will be provided."
        elif len(result_combined_CAM) > 1:
            for x in range(0, len(result_combined_CAM)):
                for y in range(0,len(result_combined_CAM)):
                    if y <= x:
                        CamCode += str(result_combined_CAM[x][y])

        else:
            CamCode = 'No Matrix Joined.'

        return case_used, result_combined_CAM, CamCode

if __name__ == "__main__":
    cam = CAM()

    graph3 = [['B', 'C', 2], ['A', 'B', 1], ['A', 'C', 3], ['D', 'B', 'X'], ['D', 'C', 'X'], ['B', 'E', 'X'], ['E', 'C', 'Y']]
    graph4 = [['B', 'C', 2], ['A', 'B', 1], ['A', 'C', 3], ['D', 'B', 'X'], ['D', 'C', 'X'], ['B', 'E', 'X'], ['E', 'A', 'Y']]

    result_matrix1, result_code1 = cam.convertToMatrix(graph3)
    print("CAM 1:")
    for x in result_matrix1:
        print(x)
    print('CAM Code: ' + result_code1)
    result_matrix2, result_code2 = cam.convertToMatrix(graph4)
    print("CAM 2:")
    for x in result_matrix2:
        print(x)
    print('The resulting code for the 2d matrix: ' + result_code2)
    case, result, code = cam.generateCAM(result_matrix1, result_matrix2)
    print(case)
    for x in result:
        print(x)
    print('CAM Code: ' + code)
    print('\n\n')


# Rigol instruments
# Just add each specialization as needed
from .mockdmm import *
from .mockps import *
from .mockoscope import *

# USB Vendor ID
# There may be better sources but this one was a good start on USB Vendor IDs
# https://devicehunt.com/all-usb-vendors
USB_VID = '0000'

# Dictionary of product/model ID (PID) and model name to allow look up of derived Device types

dmms =                  {   MOCKDMM.USB_PID     : MOCKDMM,
                            'MOCKDMM'           : MOCKDMM
                        }

logic_analyzers =       {
                        }

oscilloscopes =         {   MOCKOSCOPE.USB_PID  : MOCKOSCOPE,
                            'MOCKOSCOPE'        : MOCKOSCOPE
                        }

power_supplies       =  {   MOCKPS.USB_PID : MOCKPS,
                            'MOCKPS'       : MOCKPS,
                            'MOCKPS_MAIN'  : MOCKPS,
                            'MOCKPS_AUX'   : MOCKPS,
                            'MOCKPS_VAR'   : MOCKPS
                        }


# coding=utf-8
"""
腾讯云相关API

Tencent cloud API
"""
import json
from .. import requirePackage
from . import pre_check, user_lang, dir_char

class TxCOS:
    qcloud_cos = requirePackage('qcloud_cos', real_name='cos-python-sdk-v5')

    def __init__(self):
        """
        初始化并登陆腾讯云对象存储

        Initialize and log in Tencent Cloud object storage
        """
        scid = pre_check('txyun_scid')
        sckey = pre_check('txyun_sckey')
        self.region = pre_check('txyun_df_region')
        self.df_bucket = pre_check('txyun_cos_df_bucket')
        self.cdn_url = pre_check('txyun_df_cdn_url', ext=False)
        if self.cdn_url and not self.cdn_url.endswith('/'):
            self.cdn_url += '/'
        config = TxCOS.qcloud_cos.CosConfig(Region=self.region,
                                            SecretId=scid,
                                            SecretKey=sckey)
        self.client = TxCOS.qcloud_cos.CosS3Client(config)

    def get_func_table(self):
        """
        获取对象支持的函数表

        Gets a table of functions supported by the object

        :return: 函数表
        """
        return {
            '-up': self.upload,
            '-rm': self.remove,
            '-dl': self.download,
            '-ls': self.list_bucket
        }

    def upload(self, filePath: str, bucket: str = None):
        """
        上传文件

        Upload file

        :param filePath: 文件地址
        :param bucket: 桶名称，缺省使用self.df_bucket
        :return: None
        """
        bucket = bucket if bucket else self.df_bucket
        filename = filePath.strip()
        self.client.upload_file(Bucket=bucket, LocalFilePath=filename, Key=filename.replace(dir_char, '/'))

    def download(self, filename: str, bucket: str = None):
        """
        下载文件

        Download file

        :param filename: 文件在桶中的路径
        :param bucket: 桶名称，缺省使用self.df_bucket
        :return: None
        """
        from ..NetTools.NormalDL import normal_dl
        bucket = bucket if bucket else self.df_bucket

        url = self.cdn_url + filename \
            if bucket == self.df_bucket and self.cdn_url else \
            'https://' + bucket + '.cos.' + self.region + '.myqcloud.com/' + filename
        normal_dl(url)  # * 由于腾讯云没有提供可调用的下载sdk，因此使用qs下载引擎下载

    def remove(self, filePath: str, bucket: str = None):
        """
        删除桶中的文件

        Delete files in the bucket

        :param filePath: 文件在桶中的路径
        :param bucket: 桶名称，缺省使用self.df_bucket
        :return: None
        """
        bucket = bucket if bucket else self.df_bucket
        self.client.delete_object(bucket, filePath)

    def list_bucket(self, bucket: str = None):
        """
        展示桶中的全部文件信息

        Displays all file information in the bucket

        :param bucket: 桶名称，缺省使用self.df_bucket
        :return: None
        """
        from .. import qs_default_console, qs_info_string
        from ..NetTools.NormalDL import size_format
        from ..TuiTools.Table import qs_default_table
        from rich.text import Text
        bucket = bucket if bucket else self.df_bucket
        ls = self.client.list_objects(Bucket=bucket)['Contents']
        tb = qs_default_table(['File', 'Size'] if user_lang != 'zh' else ['文件', '体积'], title='Tencnet COS')
        for obj in ls:
            tb.add_row(Text(obj['Key'], justify='left'), Text(size_format(int(obj['Size']), align=True), justify='right'))
        qs_default_console.print(qs_info_string, 'Bucket Url:' if user_lang != 'zh' else '桶链接:',
                                 self.cdn_url if bucket == self.df_bucket and self.cdn_url else
                                 'https://' + bucket + '.cos.' + self.region + '.myqcloud.com/')
        qs_default_console.print(tb, justify="center")

class Translate:
    requirePackage('tencentcloud', real_name='tencentcloud-sdk-python')
    detect = requirePackage('langdetect', 'detect')
    DetectorFactory = requirePackage('langdetect', 'DetectorFactory')

    from tencentcloud.common import credential
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from tencentcloud.tmt.v20180321 import tmt_client, models

    def __init__(self, scid: str = pre_check('txyun_scid'), sckey: str = pre_check('txyun_sckey'),
                 region: str = pre_check('txyun_df_region')):
        """
        初始化并登陆腾讯翻译接口

        Initialize and log in Tencent translation interface

        :param scid: secret id
        :param sckey: secret key
        :param region: 地区
        """
        Translate.DetectorFactory.seed = 0
        cred = Translate.credential.Credential(scid, sckey)
        http_profile = Translate.HttpProfile()
        http_profile.endpoint = "tmt.tencentcloudapi.com"
        clientProfile = Translate.ClientProfile()
        clientProfile.httpProfile = http_profile
        self.client = Translate.tmt_client.TmtClient(cred, region, clientProfile)

    @staticmethod
    def langdetect(text: str) -> str:
        """
        获取文本的语言类型

        Gets the language type of the text

        :param text: 待识别文本
        :return: 语言类型
        """
        return Translate.detect(text)

    def translate(self, text: str, from_lang: str = None, to_lang: str = user_lang) -> str:
        """
        翻译文本至默认语言

        Translate text to the default language

        :param from_lang: 文本语言
        :param to_lang: 目标语言
        :param text: 文本
        :return: 翻译结果
        """
        req = Translate.models.TextTranslateRequest()
        req.from_json_string(json.dumps({
            "SourceText": text,
            "Source": self.langdetect(text) if not from_lang else from_lang,
            "Target": to_lang,
            "ProjectId": 0
        }))
        return json.loads(self.client.TextTranslate(req).to_json_string())['TargetText']

def translate(text: str, from_lang: str = None, to_lang: str = user_lang) -> str:
    return Translate().translate(text, from_lang, to_lang)


#!/usr/bin/python3

import numpy as np
import csv
import sys

sys.path.append('Class')
from Dataset import *
from MultiClassifier import *
from IOHelper import *

# np.set_printoptions(precision=4)
np.set_printoptions(threshold='nan')
np.set_printoptions(suppress=True)

def csvToArray(file):
	file = open(file, "r")
	arr = csv.reader(file, delimiter=',')

	X = []
	Y = []

	for i,line in enumerate(arr):
		tmp = []
		for i in line[1:]:
			tmp.append(float(i))

		X.append(tmp)
		Y.append(line[:1][0])

	if len(X) != len(Y):
		print("Error")
		exit(1)

	return (X, Y)

#############################################################

def getInputInDataset(d, index, featuresId, inFloat=False):
	X = []
	if inFloat == True:
		for i in featuresId:
			tmp = d.getDataset(index, i)
			if len(tmp) > 0:
				X.append(float(tmp))
			else:
				X.append(float(1))
	else:
		for i in featuresId:
			X.append(d.getDataset(index, i))

	return np.array(X)

def generateDataset(d, featuresId, index=-1):

	X = []

	houseArray = d.getFeature(1, uniq=True)

	for i in range(d.getLength()):
		x = getInputInDataset(d, i, featuresId, inFloat=True)

		if x is not None:
			if index == -1 or (y == index):
				if len(x) > 0:
					X.append(x)

	X = np.array(X)
	if len(X) <= 0:
		print("Error Empty dataset")
		exit(1)
	return X

##############################
############ MAIN ############
##############################

def main():

	nbOutput = 4

	file = IOHelper().checkArg(sys.argv)
	if (len(file) < 2):
		print("Missing file")
		exit(1)

	d = Dataset()
	d.loadFile(file[0])

	featuresId = range(7, 19)
	# nbInput = len(featuresId)
	X = generateDataset(d, featuresId)

	X, nbInput = d.featureExpand(d, X)
	X = d.featureRescale(d, X)

	allWeight, AllOutput = csvToArray(file[1])

	allclassifier = MultiClassifier(nbInput, AllOutput)
	allclassifier.initWeight(allWeight)

	with open('houses.csv', 'w') as file:
		file.write("Index,Hogwarts House\n")
		for i,d in enumerate(X):
			name = allclassifier.predict(d)
			file.write(str(i) + "," + name + "\n")

main()


from lfp_parameters import *
from matplotlib.mlab import griddata
from matplotlib import rcParams
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np

rcParams.update({'font.size': 8})

def get_all_src_pos(h, pop_names, total_cmpts):
    """Function to compute the positions for a list of populations"""
    all_srcs = np.zeros((sum(total_cmpts), 3))
    for jj, pop_name in enumerate(pop_names):
        all_srcs[np.sum(total_cmpts[:jj]):np.sum(total_cmpts[:jj + 1]),
                 :] = fetch_mid_pts(h, pop_name)
    return all_srcs

def update_contour_plot(i, data, ax, xi, yi, levels):
    ax.cla()
    im = ax.contourf(xi.reshape(num_x, num_y),
                     yi.reshape(num_x, num_y),
                     data[:, i].reshape(num_x, num_y), 15,
                     levels=levels, cmap=plt.cm.PRGn)
    plt.title('Time=' + str(i))
    plt.ylim(ymin=-2400, ymax=550)
    plt.xlim(xmin=-450, xmax=450)
    return im,

def grid(x, y, z, resX=100, resY=100):
    "Convert 3 column data to matplotlib grid"
    z = z.flatten()
    xi = np.linspace(min(x), max(x), resX)
    yi = np.linspace(min(y), max(y), resY)
    Z = griddata(x, y, z, xi, yi)
    X, Y = np.meshgrid(xi, yi)
    return X, Y, Z

lfp = np.load(pot_filename)
fig = plt.figure()  # figsize=(4,6))
plt.subplot(121, aspect='equal')
src_pos = get_all_src_pos(h, pop_names, total_cmpts)
# for pop_name in pop_names:
#    print pop_name
#    src_pos = fetch_mid_pts(h, pop_name)
plt.scatter(
    src_pos[:,
            0],
    src_pos[:,
            1],
    marker='.',
    alpha=1,
    color='k',
    lw=1.,
    s=0.5)
plt.scatter(
    ele_pos[:,
            0],
    ele_pos[:,
            1],
    marker='o',
    alpha=1.,
    color='r',
    lw=1.,
    s=0.4)
plt.xlabel('X ($\mu$m)')
plt.ylabel('Y ($\mu$m)')
plt.title('Morphology and electrodes')
plt.ylim(ymin=-2400, ymax=550)
plt.xlim(xmin=-450, xmax=450)

if ele_config == '1D':
    ax = plt.subplot(122)
    # for ele in range(num_ele):
    #     plt.plot(lfp[ele, :]+ele/2.)
    # ax.yaxis.set_ticks(ele_pos[:, 1])
    plt.imshow(
        lfp[::-1],
        aspect='auto',
        interpolation='nearest',
        cmap=plt.cm.PRGn)

    plt.yticks(range(num_ele), ele_pos[:, 1].astype(int)[::-1])
    plt.xticks(np.arange(2500, 4500, 500), np.arange(250, 450, 50))
    # plt.xlim(xmin=2200, xmax=4700)
    plt.ylabel('Electrode depth ($\mu$m)')
    plt.xlabel('Time (ms)')
    plt.title('Extracellular potentails (mV)')
    # plt.xlim(xmin=2500, xmax=4500)
    plt.colorbar()

elif ele_config == '2D':
    ax2 = plt.subplot(122, aspect='equal')
    levels = np.linspace(-2., 2., 15)
    # Contour plot at an instance of time!
    im2 = plt.contourf(ele_pos[:, 0].reshape(num_x, num_y),
                       ele_pos[:, 1].reshape(num_x, num_y),
                       lfp[:, 1105].reshape(num_x, num_y), 15,
                       levels=levels, cmap=plt.cm.PRGn)
    plt.colorbar()
    plt.title('Time=110.5ms')
    plt.ylim(ymin=-2400, ymax=550)
    plt.xlim(xmin=-450, xmax=450)
    if ANIMATE:
        ani = animation.FuncAnimation(
            fig, update_contour_plot, frames=xrange(1100, 1200),
            fargs=(lfp, ax2, ele_pos[:, 0], ele_pos[:, 1], levels), interval=50)
plt.tight_layout()
# plt.subplots_adjust(wspace = 0.001)
# plt.setp(ax2.xaxis.get_majorticklabels(), rotation=70 )
# plt.setp(ax.xaxis.get_majorticklabels(), rotation=70 )
# plt.savefig('1D_1105_traub.png', dpi=600)
plt.show()
h.close()


import random
import json
import shutil
import os

def split_dataset(infor_path, save_path):
    positive_result = []
    negative_resilt = []
    for file in os.listdir(infor_path):
        f = open(infor_path + "/" + file)
        data = json.load(f)
        f.close()
        c = data["class"]
        id = data["id"]
        if c == 1:
            positive_result.append(id)
        else:
            negative_resilt.append(id)
    for i in range(random.randint(0, 15)):
        random.shuffle(positive_result)
        random.shuffle(negative_resilt)
    neg_test = negative_resilt[0:30]
    pos_test = positive_result[0:95]
    neg_train = negative_resilt[30:]
    pos_train = positive_result[95:]
    for i in neg_train:
        oldname = infor_path + "/" + i + ".info"
        newname = save_path + "/train/neg/" + i + ".info"
        shutil.copyfile(oldname, newname)
    for i in neg_test:
        oldname = infor_path + "/" + i + ".info"
        newname = save_path + "/test/neg/" + i + ".info"
        shutil.copyfile(oldname, newname)
    for i in pos_train:
        oldname = infor_path + "/" + i + ".info"
        newname = save_path + "/train/pos/" + i + ".info"
        shutil.copyfile(oldname, newname)
    for i in pos_test:
        oldname = infor_path + "/" + i + ".info"
        newname = save_path + "/test/pos/" + i + ".info"
        shutil.copyfile(oldname, newname)


#Alem Snyder
#print items in a deceonary
Items = {"Milk":6.50,
         "Bread": 4.99,
         "Butter": 7.35,
         "Bananas":.19,
         "Oatmeal (1lb)":2.00,
         "Chicken":12.35,}

for name,price in Items.items():
    print(f'{" "*(10-(len(name)+len(str(price)))/2)}{name}      {str(price)}')

#Should print

#       Milk     6.5
#      Bread     4.99
#     Butter     7.35
#     Bananas     0.19
#  Oatmeal (1lb)     2.0
#    Chicken     12.35


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

from alipay.aop.api.response.AlipayResponse import AlipayResponse
from alipay.aop.api.domain.InsPreOrderDTO import InsPreOrderDTO

class AlipayInsSceneEcommercePreorderquoteRefreshResponse(AlipayResponse):

    def __init__(self):
        super(AlipayInsSceneEcommercePreorderquoteRefreshResponse, self).__init__()
        self._pre_order = None

    @property
    def pre_order(self):
        return self._pre_order

    @pre_order.setter
    def pre_order(self, value):
        if isinstance(value, InsPreOrderDTO):
            self._pre_order = value
        else:
            self._pre_order = InsPreOrderDTO.from_alipay_dict(value)

    def parse_response_content(self, response_content):
        response = super(AlipayInsSceneEcommercePreorderquoteRefreshResponse, self).parse_response_content(response_content)
        if 'pre_order' in response:
            self.pre_order = response['pre_order']


def next_higher(value):
    ones_num = bin(value).count('1')
    target = value + 1
    while bin(target).count('1') != ones_num:
        target += 1
    return target

# // Test
for n in [129, 127, 1, 323423]:
    print(next_higher(n))


class Solution:
    def partitionLabels(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        if S == '':
            return []
        checkdict = {}
        res = []
        for i,char in enumerate(S):
            if char in S[i+1:]:
                checkdict[char] = 1
            else:
                checkdict[char] = 0
            if sum(list(checkdict.values())) == 0:
                checkdict.clear()
                if res == []:
                    res.append(i+1)
                else:
                    res.append(i-sum(res[:])+1)
        return res

###second method:::
class Solution:
    def partitionLabels(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        dicts = {j:i for i,j in enumerate(S)}
        start = j = 0
        res = []
        for i, char in enumerate(S):
            j = max(j, dicts[char])
            if i == j:
                res.append(j-start+1)
                start = i + 1
        return res


# This is an example of implementing a search algorithm using the generic Node and problem class.
# Note that an explored list is not maintained in this version of BFS.

from copy import deepcopy
from collections import deque

from node import Node

def LocalBeamKC(problem, k=123900):

	stateRecord = {}

	nodesGenerated = 1
	maxNodesInFrontier = 0

	# Generate the initial (root) node
	node = Node(problem.initial)

	# Record state to avoid looping
	stateRecord[node.getState().evaluate()] = 1

	# For efficiency, we check if the node is a goal state BEFORE putting on the Q
	if problem.isGoal(node.getState()):
		print("Total nodes generated: " + nodesGenerated)
		print("Max nodes in memory: " + maxNodesInFrontier)
		return node

	frontier = []

	# Start queue to hold number of unvisited children at each level
	numChildren = []

	# Continue to expand node and find a next eligible child node until a solution is found
	# or until all possible solutions have been eliminated
	while(True):
		# Generate children and push them onto frontier stack
		children = node.expand(problem)

		for child in children:
			frontier.append(child)
			nodesGenerated += 1

		maxNodesInFrontier = max(maxNodesInFrontier, len(frontier))

		numChildren.append(len(children))

		while(len(frontier) > k):
			frontier.pop(0)
			if numChildren[0] == 1:
				numChildren.pop(0)
			elif numChildren[0] == 0:
				while(numChildren[0] == 0):
					numChildren.pop(0)
				numChildren[0] = numChildren[0] - 1
			else:
				numChildren[0] = numChildren[0] - 1

		# Select a child to set as 'node'. Keep iterating until one is found or until we arrive back at the root node (in which case there is no solution)
		while(True):

			if len(frontier) < 2:
				print("frontier length: " + str(len(frontier)))
				print(frontier)
				print("numChildren length: " + str(len(numChildren)))
				print(numChildren)

			if len(frontier) == 0:
				print("No solution found")
				print("Total nodes generated: " + str(nodesGenerated))
				print("Max nodes in memory: " + str(maxNodesInFrontier))
				return None

			# Get number of nodes to pull off of frontier
			childrenInQueue = numChildren.pop()
			numChildren.append(childrenInQueue)

			if childrenInQueue == 0: # No children. Go back to parent node
				if node.depth == 0:
					print("No solution found")
					print("Total nodes generated: " + str(nodesGenerated))
					print("Max nodes in memory: " + str(maxNodesInFrontier))
					return None
				numChildren.pop()
				node = node.parent
				continue

			# Pull nodes off of frontier
			children = []
			for i in range(0,childrenInQueue):
				children.append(frontier.pop())

			# Use heurstic function to determine the most promising child node
			mostPromisingChild = children[0]
			mostPromisingHeuristic = problem.heuristic(children[0].state)

			for child in children:
				if problem.heuristic(child.state) < mostPromisingHeuristic:
					mostPromisingChild = child
					mostPromisingHeuristic = problem.heuristic(child.state)

			# Put the nodes that weren't selected back onto the frontier stack
			for child in children:
				if child != mostPromisingChild:
					frontier.append(child)

			# Decrement top value on stack
			childrenInQueue = numChildren.pop()
			numChildren.append(childrenInQueue - 1)

			node = mostPromisingChild

			# Check if selected child's state is in state record:
			if node.getState().evaluate() in stateRecord:
				# If it is, no need to explore it. Return to parent node
				if node.depth == 0:
					print('No solution found')
					print("Total nodes generated: " + str(nodesGenerated))
					print("Max nodes in memory: " + str(maxNodesInFrontier))
					return None
				node = node.parent

			# Otherwise add selected child's state to state record then BREAK
			else:
				stateRecord[node.getState().evaluate()] = 1

				# Check if we've reached our goal
				# If so, assemble solution
				if problem.isGoal(node.getState()):
					solution = [node.getState().state]
					nodePtr = node
					while nodePtr.depth != 0:
						nodePtr = nodePtr.parent
						solution.append(nodePtr.getState().state)
					solution.reverse()
					print("Total nodes generated: " + str(nodesGenerated))
					print("Max nodes in memory: " + str(maxNodesInFrontier))
					return solution

				break


from zope.component import adapts
from zope.interface import implements
from archetypes.schemaextender.interfaces import IBrowserLayerAwareExtender
from Products.Archetypes.atapi import StringField, SelectionWidget
from archetypes.schemaextender.field import ExtensionField
from wm.showhidecolumns.interfaces import IControlColumns, IShowHideColumnsLayer
from wm.showhidecolumns import ShowHideColumnsMessageFactory as _
from Products.Archetypes.utils import DisplayList

class ExtensionStringField(ExtensionField, StringField):
    """A string field"""

COLUMN_VOCABULARY=DisplayList((
   ('0', _(u'vocab_show', default=u"Show column in any case")),
   ('1', _(u'vocab_hide', default=u"Hide column in any case")),
   ('default', _(u'vocab_default', default=u"Show column if not empty (default behaviour)")),
   ))

class ColumnsSchemaExtender(object):
    adapts(IControlColumns)
    implements(IBrowserLayerAwareExtender)

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

    layer = IShowHideColumnsLayer

    def getFields(self):

        fields = [
            ExtensionStringField('leftPortletslot',
                         required = False,
                         languageIndependent = False,
                         schemata = 'settings',
                         vocabulary=COLUMN_VOCABULARY,
                         enforce_vocabulary=True,
                         widget = SelectionWidget(
                                  description=_(u'help_column', default=u"allows to show or hide the portlet column"),
                                  label = _(u"label_column_left", default=u"Left portlet slot visibility"),
                                  visible={'view' : 'hidden',
                                           'edit' : 'visible'},
                                  ),
            ),

            ExtensionStringField('rightPortletslot',
                         required = False,
                         languageIndependent = False,
                         schemata = 'settings',
                         vocabulary=COLUMN_VOCABULARY,
                         enforce_vocabulary=True,
                         widget = SelectionWidget(
                                  description=_(u'help_column', default=u"allows to show or hide the portlet column"),
                                  label = _(u"label_column_right", default=u"Right portlet slot visibility"),
                                  visible={'view' : 'hidden',
                                           'edit' : 'visible'},
                                  ),
            ),

        ]

        return fields


import glob
from pathlib import Path
import shutil
import sys

def run():
    for fn in glob.glob(f"/tmp/*") + glob.glob(f"/tmp/.*"):
        is_dir = Path(fn).is_dir()
        stat = Path(fn).stat()
        if "C001_collect_comment" in fn or "Gyotaku.py" in fn or ".com.google.Chrome" in fn or "ReflectHtml" in fn:
            if is_dir:
                try:
                    shutil.rmtree(fn)
                except OSError as exc:
                    print(exc, file=sys.stderr)
                continue
            else:
                try:
                    Path(fn).unlink()
                except Exception as exc:
                    print(exc, file=sys.stderr)
                continue
        print("skip", fn, stat)

if __name__ == "__main__":
    run()