import os
os.environ['TOKENIZERS_PARALLELISM']='false'

import sys
import torch
import time
import math
import shutil
import pandas as pd
from dataclasses import dataclass
from collections import defaultdict
from torch.cuda.amp import GradScaler
from torch.utils.data import DataLoader
from transformers import get_constant_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup
from transformers import AutoTokenizer

from retrieval.loss import InfoNCE
from retrieval.model import Net
from retrieval.trainer import train
from retrieval.utils import setup_system, Logger
from retrieval.evaluate import evaluate_val, evaluate_train
from retrieval.dataset import EqualDatasetTrain, EqualDatasetEval

@dataclass
class Configuration:

    #--------------------------------------------------------------------------
    # Models:
    #--------------------------------------------------------------------------
    # 'sentence-transformers/LaBSE'
    # 'microsoft/mdeberta-v3-base'
    # 'sentence-transformers/stsb-xlm-r-multilingual'
    # 'sentence-transformers/paraphrase-multilingual-mpnet-base-v2'
    # 'sentence-transformers/xlm-r-100langs-bert-base-nli-mean-tokens'
    #--------------------------------------------------------------------------

    # Transformer
    transformer: str = 'sentence-transformers/LaBSE'
    pooling: str = 'cls'                   # 'mean' | 'cls' | 'pooler'
    hidden_dropout_prob: float = 0.1
    attention_dropout_prob: float = 0.1
    proj = None                            # None | int for lower dimension

    margin: float = 0.16

    # Reduction of model size
    layers_to_keep = None                 # None -> org. model | (1,2,...,11) layers to keep

    # Model Destillation
    transformer_teacher: str ='sentence-transformers/LaBSE'
    use_teacher: bool = False             # use destillation
    pooling_teacher: str = 'cls'          # 'mean' | 'cls' | 'pooler'
    proj_teacher = None                   # None | int for lower dimension

    # Language Sampling
    init_pool = 0
    pool = (0,1,2,3)                      # (0,) for only train on original data without translation
    epoch_stop_switching: int = 36        # epochs no language switching more used (near end of training)

    # Debugging
    debug = None                          # False | 10000 for fast test

    # Training
    seed: int = 42
    epochs: int = 40
    batch_size: int = 512
    mixed_precision: bool = True          # using fp16
    gradient_accumulation: int = 1
    gradient_checkpointing: bool = False  # use gradient checkpointing
    verbose: bool = True                  # show progressbar
    gpu_ids: tuple = (0,1,2,3)            # GPU ids for training

    # Eval
    eval_every_n_epoch: int = 1
    normalize_features: bool = True
    zero_shot: bool = False               # eval before first epoch

    # Optimizer
    clip_grad = 100.                      # None | float
    decay_exclue_bias: bool = False

    # Loss
    label_smoothing: float = 0.1

    # Learning Rate
    lr: float = 0.0002
    scheduler: str = 'polynomial'        # 'polynomial' | 'constant' | None
    warmup_epochs: int = 2
    lr_end: float = 0.00005              #  only for 'polynomial'

    # Data
    language: str = 'all'                # 'all' | 'en', es', 'pt', 'fr', ....
    fold: int = 0                        # eval on fold x
    train_on_all: bool = False           # train on all data incl. data of fold x
    max_len: int = 96                    # max token lenght for topic and content

    # Sampling
    max_wrong: int = 128                 # limit for sampling of wrong content for specific topic
    custom_sampling: bool = True         # do custom shuffle to prevent having related content in batch
    sim_sample: bool = True              # upsample missing and combine hard negatives in batch
    sim_sample_start: int = 1            # if > 1 skip firt n epochs for sim_sampling

    # Save folder for model checkpoints
    model_path: str = './checkpoints'

    # Checkpoint to start from
    checkpoint_start = None              # pre-trained checkpoint for model we want to train
    checkpoint_teacher = None            # pre-trained checkpoint for teacher

    # set num_workers to 0 if on Windows
    num_workers: int = 0 if os.name == 'nt' else 4

    # train on GPU if available
    device: str = 'cuda' if torch.cuda.is_available() else 'cpu'

    # for better performance
    cudnn_benchmark: bool = True

    # make cudnn deterministic
    cudnn_deterministic: bool = False

#-----------------------------------------------------------------------------#
# Config                                                                      #
#-----------------------------------------------------------------------------#

config = Configuration()

if __name__ == '__main__':

    model_path = '{}/{}/{}'.format(config.model_path,
                                   config.transformer,
                                   time.strftime('%H%M%S'))

    if not os.path.exists(model_path):
        os.makedirs(model_path)
    shutil.copyfile(os.path.basename(__file__), '{}/train.py'.format(model_path))

    # Redirect print to both console and log file
    sys.stdout = Logger(os.path.join(model_path, 'log.txt'))

    setup_system(seed=config.seed,
                 cudnn_benchmark=config.cudnn_benchmark,
                 cudnn_deterministic=config.cudnn_deterministic)

    #-----------------------------------------------------------------------------#
    # Model                                                                       #
    #-----------------------------------------------------------------------------#

    print('\n{}[Model: {}]{}'.format(20*'-', config.transformer, 20*'-'))

    model = Net(transformer_name=config.transformer,
                gradient_checkpointing=config.gradient_checkpointing,
                hidden_dropout_prob=config.hidden_dropout_prob,
                attention_dropout_prob=config.attention_dropout_prob,
                pooling=config.pooling,
                projection=config.proj)

    print(model.transformer.config)

    # load pretrained Checkpoint
    if config.checkpoint_start is not None:
        print('Start from:', config.checkpoint_start)
        model_state_dict = torch.load(config.checkpoint_start)
        model.load_state_dict(model_state_dict, strict=True)

    #-----------------------------------------------------------------------------#
    # Drop Transformer Layers                                                     #
    #-----------------------------------------------------------------------------#

    if config.layers_to_keep is not None:
        print('Remove layers from model. Only keep these layers: {}'.format(config.layers_to_keep))
        new_layers = torch.nn.ModuleList([layer_module for i, layer_module in enumerate(model.transformer.encoder.layer) if i in config.layers_to_keep])
        model.transformer.encoder.layer = new_layers
        model.transformer.config.num_hidden_layers = len(config.layers_to_keep)

        print('\n{}[Reduced Model: {}]{}'.format(17*'-', config.transformer, 17*'-'))
        print(model.transformer.config)

    #-----------------------------------------------------------------------------#
    # DP and model to device                                                      #
    #-----------------------------------------------------------------------------#

    # Data parallel
    print('GPUs available:', torch.cuda.device_count())
    if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
        model = torch.nn.DataParallel(model, device_ids=config.gpu_ids)

    # Model to device
    model = model.to(config.device)

    #-----------------------------------------------------------------------------#
    # Model destillation                                                          #
    #-----------------------------------------------------------------------------#

    # Teacher for destillation
    if config.use_teacher:
        teacher = Net(transformer_name=config.transformer_teacher,
                      gradient_checkpointing=False,
                      hidden_dropout_prob=0.0,
                      attention_dropout_prob=0.0,
                      pooling=config.pooling_teacher,
                      projection=config.proj_teacher)

        print('\n{}[Teacher: {}]{}'.format(23*'-', config.transformer , 23*'-'))
        print(teacher.transformer.config)

        if config.checkpoint_teacher is not None:
            print('Load Teacher-Checkpoint:', config.checkpoint_teacher)
            model_state_dict = torch.load(config.checkpoint_teacher)
            teacher.load_state_dict(model_state_dict, strict=True)
        else:
            print('You are using a checkpoint for the Teacher-Model that was not trained on that task!!!')

        for name, p in teacher.named_parameters():
            p.requires_grad = False

        if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
            teacher = torch.nn.DataParallel(teacher, device_ids=config.gpu_ids)

        teacher = teacher.to(config.device)

    else:
        teacher = None

    #-----------------------------------------------------------------------------#
    # Tokenizer                                                                   #
    #-----------------------------------------------------------------------------#

    tokenizer = AutoTokenizer.from_pretrained(config.transformer)

    #-----------------------------------------------------------------------------#
    # Data                                                                        #
    #-----------------------------------------------------------------------------#

    df_correlations = pd.read_csv('./data/correlations.csv')
    topics = df_correlations['topic_id'].values
    content = df_correlations['content_ids'].values

    # GT dict for eval
    gt_dict = dict()

    for i in range(len(topics)):
        content_tmp = content[i].split(' ')
        topic_tmp = topics[i]
        gt_dict[topic_tmp] = content_tmp

    # split if not train on all data
    if config.train_on_all:
        df_correlations_train = df_correlations
    else:
        df_correlations_train = df_correlations[df_correlations['fold'] != config.fold]

    if config.debug:
        print(f'DEBUG MODE: use only {config.debug} topics for training')
        #df_correlations = df_correlations.sample(n=config.debug)

    topics = df_correlations_train['topic_id'].values
    content = df_correlations_train['content_ids'].values

    content2topic = defaultdict(set)

    for i in range(len(topics)):

        content_tmp = content[i].split(' ')
        topic_tmp = topics[i]

        for c in content_tmp:
            content2topic[c].add(topic_tmp)

    #-----------------------------------------------------------------------------#
    # DataLoader                                                                  #
    #-----------------------------------------------------------------------------#

    # Train
    train_dataset = EqualDatasetTrain(df_correlations=df_correlations_train,
                                      fold=config.fold,
                                      tokenizer=tokenizer,
                                      max_len=config.max_len,
                                      shuffle_batch_size=config.batch_size,
                                      pool=config.pool,
                                      init_pool=config.init_pool,
                                      train_on_all=config.train_on_all,
                                      language=config.language,
                                      debug=config.debug)

    train_loader = DataLoader(dataset=train_dataset,
                              batch_size=config.batch_size,
                              shuffle=not config.custom_sampling,
                              num_workers=config.num_workers,
                              pin_memory=True,
                              collate_fn=train_dataset.smart_batching_collate
                              )

    print('\nTrain Pairs:', len(train_dataset ))

    # Eval
    val_dataset_topic = EqualDatasetEval(mode='topic',
                                         typ='val',
                                         fold=config.fold,
                                         tokenizer=tokenizer,
                                         max_len=config.max_len,
                                         pool=config.pool,
                                         init_pool=config.init_pool,
                                         train_on_all=config.train_on_all,
                                         language=config.language,
                                         debug=config.debug)

    val_dataset_content = EqualDatasetEval(mode='content',
                                           typ='val',
                                           fold=config.fold,
                                           tokenizer=tokenizer,
                                           max_len=config.max_len,
                                           pool=config.pool,
                                           init_pool=config.init_pool,
                                           train_on_all=config.train_on_all,
                                           language=config.language,
                                           debug=config.debug)

    val_loader_topic = DataLoader(dataset=val_dataset_topic,
                                  batch_size=config.batch_size,
                                  shuffle=False,
                                  num_workers=config.num_workers,
                                  pin_memory=True,
                                  collate_fn=val_dataset_topic.smart_batching_collate
                                  )

    val_loader_content = DataLoader(dataset=val_dataset_content,
                                    batch_size=config.batch_size,
                                    shuffle=False,
                                    num_workers=config.num_workers,
                                    pin_memory=True,
                                    collate_fn=val_dataset_content.smart_batching_collate
                                    )

    print('\nTopics Val:', len(val_dataset_topic))
    print('Content Val:', len(val_dataset_content))

    #-----------------------------------------------------------------------------#
    # Sim Sample                                                                  #
    #-----------------------------------------------------------------------------#

    train_dataset_topic = EqualDatasetEval(mode='topic',
                                           typ='train',
                                           fold=config.fold,
                                           tokenizer=tokenizer,
                                           max_len=config.max_len,
                                           pool=config.pool,
                                           init_pool=config.init_pool,
                                           train_on_all=config.train_on_all,
                                           language=config.language,
                                           debug=config.debug)

    train_dataset_content = EqualDatasetEval(mode='content',
                                             typ='train',
                                             fold=config.fold,
                                             tokenizer=tokenizer,
                                             max_len=config.max_len,
                                             pool=config.pool,
                                             init_pool=config.init_pool,
                                             train_on_all=config.train_on_all,
                                             language=config.language,
                                             debug=config.debug)

    train_loader_topic = DataLoader(dataset=train_dataset_topic,
                                  batch_size=config.batch_size,
                                  shuffle=False,
                                  num_workers=config.num_workers,
                                  pin_memory=True,
                                  collate_fn=train_dataset_topic.smart_batching_collate
                                  )

    train_loader_content = DataLoader(dataset=train_dataset_content,
                                    batch_size=config.batch_size,
                                    shuffle=False,
                                    num_workers=config.num_workers,
                                    pin_memory=True,
                                    collate_fn=train_dataset_content.smart_batching_collate
                                    )

    print('\nTopics Train:', len(train_dataset_topic))
    print('Content Train:', len(train_dataset_content))

    #-----------------------------------------------------------------------------#
    # Loss                                                                        #
    #-----------------------------------------------------------------------------#

    loss_fn = torch.nn.CrossEntropyLoss(label_smoothing=config.label_smoothing)
    loss_function = InfoNCE(loss_function=loss_fn,
                             device=config.device,
                             )

    if config.mixed_precision:
        scaler = GradScaler(init_scale=2.**10)
    else:
        scaler = None

    #-----------------------------------------------------------------------------#
    # optimizer                                                                   #
    #-----------------------------------------------------------------------------#

    if config.decay_exclue_bias:
        param_optimizer = list(model.named_parameters())
        no_decay = ['bias', 'LayerNorm.bias']
        optimizer_parameters = [
            {
                'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
                'weight_decay': 0.01,
            },
            {
                'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],
                'weight_decay': 0.0,
            },
        ]
        optimizer = torch.optim.AdamW(optimizer_parameters, lr=config.lr)
    else:

        optimizer = torch.optim.AdamW(model.parameters(), lr=config.lr)

    #-----------------------------------------------------------------------------#
    # Scheduler                                                                   #
    #-----------------------------------------------------------------------------#

    train_steps = math.floor((len(train_loader) * config.epochs) / config.gradient_accumulation)
    warmup_steps = len(train_loader) * config.warmup_epochs

    if config.scheduler == 'polynomial':
        print('\nScheduler: polynomial - max LR: {} - end LR: {}'.format(config.lr, config.lr_end))
        scheduler = get_polynomial_decay_schedule_with_warmup(optimizer,
                                                              num_training_steps=train_steps,
                                                              lr_end = config.lr_end,
                                                              power=1.5,
                                                              num_warmup_steps=warmup_steps)

    elif config.scheduler == 'constant':
        print('\nScheduler: constant - max LR: {}'.format(config.lr))
        scheduler =  get_constant_schedule_with_warmup(optimizer,
                                                       num_warmup_steps=warmup_steps)

    else:
        scheduler = None

    print('Warmup Epochs: {} - Warmup Steps: {}'.format(str(config.warmup_epochs).ljust(2), warmup_steps))
    print('Train Epochs:  {} - Train Steps:  {}'.format(config.epochs, train_steps))

    #-----------------------------------------------------------------------------#
    # Zero Shot                                                                   #
    #-----------------------------------------------------------------------------#

    if config.zero_shot:
        print('\n{}[{}]{}'.format(30*'-', 'Zero Shot', 30*'-'))

        f2, precision, recall = evaluate_val(config,
                                            model,
                                            reference_dataloader=val_loader_content,
                                            query_dataloader=val_loader_topic,
                                            gt_dict=gt_dict,
                                            cleanup=True)

    #-----------------------------------------------------------------------------#
    # Shuffle                                                                     #
    #-----------------------------------------------------------------------------#
    # Initial values no sim_sampling for first or first n epochs
    missing_pairs, topic2wrong = None, None

    if config.custom_sampling:
        train_loader.dataset.shuffle(missing_list=missing_pairs,
                                     wrong_dict=topic2wrong,
                                     max_wrong=config.max_wrong)

    #-----------------------------------------------------------------------------#
    # Train                                                                       #
    #-----------------------------------------------------------------------------#
    t_train_start = time.time()

    start_epoch = 0
    best_score = 0

    # language switch pool without original position 0
    pools = config.pool[1:]
    current_pool_pointer = 0

    for epoch in range(1, config.epochs+1):

        print('\n{}[Epoch: {}]{}'.format(30*'-', epoch, 30*'-'))

        train_loss = train(config,
                           model,
                           dataloader=train_loader,
                           loss_function=loss_function,
                           optimizer=optimizer,
                           scheduler=scheduler,
                           scaler=scaler,
                           teacher=teacher)

        print('Epoch: {}, Train Loss = {:.3f}, Lr = {:.6f}'.format(epoch,
                                                                   train_loss,
                                                                   optimizer.param_groups[0]['lr']))

        print('\n{}[{}]{}'.format(30*'-', 'Evaluate (Val)', 30*'-'))

        f2, precision, recall = evaluate_val(config,
                                            model,
                                            reference_dataloader=val_loader_content,
                                            query_dataloader=val_loader_topic,
                                            gt_dict=gt_dict,
                                            cleanup=True)

        if f2 > best_score:
            best_score = f2

            best_checkpoint = '{}/weights_e{}_{:.4f}.pth'.format(model_path, epoch, f2)

            if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
                torch.save(model.module.state_dict(), best_checkpoint)
            else:
                torch.save(model.state_dict(), best_checkpoint)

        elif f2 < (0.8 * best_score):
            print('Something went wrong:')
            print(f'Resett to: {best_checkpoint} -> and continue training'  )
            model_state_dict = torch.load(best_checkpoint)
            if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
                model.module.load_state_dict(model_state_dict, strict=True)
            else:
                model.load_state_dict(model_state_dict, strict=True)

        if config.sim_sample:
            print('\n{}[{}]{}'.format(30*'-', 'Evaluate (Train)', 30*'-'))
            # Set pool for next epoch -> sim sample for that pool
            if len(config.pool) > 1:
                if epoch < config.epoch_stop_switching:

                    # come back to original pool 0 every uneven epoch
                    if epoch % 2 == 0:
                        next_pool = 0
                    else:
                        next_pool = pools[current_pool_pointer % len(pools)]
                        current_pool_pointer += 1

                    # set train data for next epoch
                    train_loader_content.dataset.set_pool(next_pool)
                    train_loader_topic.dataset.set_pool(next_pool)
                    train_loader.dataset.set_pool(next_pool)
                else:
                    train_loader_content.dataset.set_pool(0)
                    train_loader_topic.dataset.set_pool(0)
                    train_loader.dataset.set_pool(0)

            if epoch >= config.sim_sample_start:
                missing_pairs, topic2wrong = evaluate_train(config=config,
                                                            model=model,
                                                            reference_dataloader=train_loader_content,
                                                            query_dataloader=train_loader_topic,
                                                            gt_dict=gt_dict,
                                                            content2topic=train_loader.dataset.content2topic,
                                                            cleanup=True)

        if config.custom_sampling:
            train_loader.dataset.shuffle(missing_list=missing_pairs,
                                         wrong_dict=topic2wrong,
                                         max_wrong=config.max_wrong)

    if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
        torch.save(model.module.state_dict(), '{}/weights_end.pth'.format(model_path))
    else:
        torch.save(model.state_dict(), '{}/weights_end.pth'.format(model_path))


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import json

from .models import Notes
from .api import get_all_list

@login_required
def create_note(request):
    if request.method == 'POST':

        try:
            note_data = json.loads(request.body.decode('utf-8'))  # Parse the JSON data

        except json.JSONDecodeError:
            return HttpResponse("Invalid JSON data")

        userName = request.user.username
        textContent = note_data.get("textContent")
        title = note_data.get("title")

        if len(title) == 0:
            title = "Untitled Note"

        note = Notes(userName=userName, title=title, textContent=textContent)

        try:
            note.save()
            response_data = {"status": "success", "message": "Note saved to the database"}
        except Exception as e:
            response_data = {"status": "failure", "message": str(e)}

        return JsonResponse(response_data)

    return HttpResponse("Invalid Request")

@login_required
def edit_note(request):

    if request.method == "POST":
        try:
            edit_note = json.loads(request.body.decode('utf-8')) # Parse the JSON data

        except json.JSONDecodeError:
            return HttpResponse("Invalid JSON data request")

        noteId = edit_note.get('id')
        textContent = edit_note.get('textcontent')

        if noteId:
                try:
                    note = Notes.objects.get(noteId=noteId)
                    note.textContent = textContent
                    note.save()
                    return JsonResponse({'message': 'Note Updated successfully'})
                except Notes.DoesNotExist:
                    return JsonResponse({'error': 'Note not found'}, status=404)

    return JsonResponse({'error': 'Note ID not provided'}, status=400)

@login_required
def delete_note(request):
    if request.method == "DELETE":
        try:
            note_id = json.loads(request.body.decode('utf-8')).get("id") # Parse the JSON data

        except json.JSONDecodeError:
            return HttpResponse("Invalid JSON data request")

        if note_id:
            try:
                note = Notes.objects.get(noteId=note_id)
                note.delete()
                return JsonResponse({'message': 'Note deleted successfully'})
            except Notes.DoesNotExist:
                return JsonResponse({'error': 'Note not found'}, status=404)
        else:
            return JsonResponse({'error': 'Note ID not provided'}, status=400)

    return JsonResponse({'error': 'Not Valid request'}, status=400)

@login_required
def notes_list(request):
    username = request.user.username
    return render(request, 'notes.html',{"notes":get_all_list(username)})


from subprocess import call
import math
# S1 = 500
# S2 = 250
import sys

import numpy as np

import os
from joblib import Parallel, delayed
import multiprocessing

# def run(Para1, Para2, Para3, S2_amp):
def run(Para1, Popul_ID):
	# global mut
	#call(["./main","BCL", str(S1), "S2", str(S2), "Mutation", mut, "S1_number", "20", "ISO", ISO])  #
	# call(['./SAN', str(P1), str(P2)]) #"Mutation", mut, "S1_number", "20", "ISO", ISO])  #
	# call(['./NCZ_Model_New_Ito', '250', '14', '250', 'WT', 'Normal', '0', '0', str(P1), str(P2), str(P3),str(P4),str(P5),str(P6)], stdout=f)
	# call(['./NCZ_Model_New_Ito', '500', '14', '500', 'WT', 'Normal', '0', '0', str(P1), str(P2), str(P3),str(P4),str(P5),str(P6)], stdout=f)
	P=['./main_HAM_Signalling_cvode_new', str(BCL), str(Popul_ID), str(ISO),  str(CaMKII_inhb), str(CaMKII_db)]
	for i in range(len(Para1)):
		P.append(str(Para1[i]))
	call(P, stdout=f)

Popul_params = np.loadtxt('para_large_sigma.log.20000')

# f2 = open("para_log.dat", "w+")

BCL = float(sys.argv[1])
ISO = float(sys.argv[2])
CaMKII_inhb = int(sys.argv[3])
CaMKII_db = int(sys.argv[4])
# run(BCL,"Normal", 0,0,0,0);
# Mode="SimDrug"
f = open("AP.log.dat.20000", "w+")
# sys.stdout = open('file', 'w')

IDs_to_run = list(range(len(Popul_params)))

run_parallel = True #False

if not run_parallel:
	for i in IDs_to_run: #range(600):
		index = str(i)
		print ('processing ID ' +  index)
		run(Popul_params[i], i);
		call('mv HAM_wrap_out.dat AP.BCL.1000.ID.'+index, shell=True);
		call('mv Restart_ICs/ICs.bin Restart_ICs/ICs.bin.'+index, shell=True);

if run_parallel:
	num_cores = 40#multiprocessing.cpu_count() - 2

	results=Parallel(n_jobs=num_cores, prefer="threads")(
	 		delayed(run)
	 				(Popul_params[PopulID], PopulID)
	 				for PopulID in IDs_to_run)

f.close()


import numpy as np
from PIL import Image
from sys import argv
import side_by_side

L = 256

def histogram(im):
	return side_by_side.histogram_rgb(im)

def uniform_hist(im):
    histogram_r, accum_r, histogram_g, accum_g, histogram_b, accum_b  = histogram(im)
    def w_dot(r):
        wr = accum_r[r[0]]
        wg = accum_g[r[1]]
        wb = accum_b[r[2]]
        return list(map(lambda w: int(w * L - 0.5), [wr, wg, wb]))
    ret = im.copy()
    for i in range(ret.shape[0]):
        for j in range(ret.shape[1]):
            print(i,j)
            ret[i][j] = w_dot(ret[i][j])
    return ret

im1 = np.asarray(Image.open(argv[1]).convert('RGB'))
side_by_side.sbys_histogram([im1, uniform_hist(im1)], ['rgb', 'rgb'],argv=argv[2] if len(argv)>2 else None)


import requests
from bs4 import BeautifulSoup as bs
import time
import sqlite3

'''
由于网站反扒设置，此脚本仅能爬取部分章节
Summary:
    soup.get_text("|", strip=True)  获取tag包裹的内容并去除前后的空格
    a['href']  返回a标签下href属性的值
    快捷键：输入main敲回车即可快速设置主函数
    re.findall()加上re.S参数可以匹配到换行符，即把换行符包含进去
    for key, value in urlst.items():可以迭代字典的key和value
    判断语句l == []等价于not l
'''

def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
    res = requests.get(url, headers=headers)
    res.encoding = res.apparent_encoding
    html = res.text
    return html

def get_urlst():
    url = 'http://www.xbiquge.la/10/10489/'
    html = get_html(url)
    # print(html)
    soup = bs(html, 'html.parser')
    dd_lst = soup.find_all('dd')
    # print(ddlst)
    a_lst = [dd.find_all('a')[0] for dd in dd_lst]
    # print(alst)
    name_lst = [a.get_text() for a in a_lst]
    ref_lst = [a['href'] for a in a_lst]
    base_url = 'http://www.xbiquge.la'
    ref_lst = [base_url + i for i in ref_lst]
    # print(name_lst, ref_lst)
    urlst = dict(zip(name_lst, ref_lst))
    # print(urlst)
    return urlst

def get_content(url):
    html = get_html(url)
    soup = bs(html, 'html.parser')
    content = soup.find_all(id='content')
    if not content:
        content = '暂无'
    else:
        content = content[0].get_text()
    # print(content)
    return content

def save2db(data, dbpath):
    conn = sqlite3.connect(dbpath)
    cur = conn.cursor()
    row = ['"' + data[0] + '"', '"' + data[1] + '"']
    # 由于下文对row进行join方法后不带“”号，而sqlite语句需要加双引号，故需再此处加上双引号

    sql = '''
        insert into acrj(title, content)
        values(%s)
    ''' % ','.join(row)

    cur.execute(sql)
    conn.commit()
    cur.close()
    conn.close()

def init_db(dbpath):
    sql = '''
    create table acrj
    (
    id integer primary key autoincrement,
    title text,
    content text
    )
    '''
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()

def main():
    urlst = get_urlst()
    path = 'bqg.db'
    init_db(path)
    for key, value in urlst.items():
        content = get_content(value)
        data = (key, content)
        save2db(data, path)
        time.sleep(1)
        print('%s：完成！' % key)

if __name__ == '__main__':
    main()


from flask import jsonify, request
from app.models import Clinical_info, Token
from app import db

def deleteClinicalInfo(id):
    '''delete clinical info record'''

    token = request.headers['TOKEN']

    t=Token.query.filter_by(token=token).first()

    is_expired=t.status

    if id is not None:
        if token and is_expired == 'active':
            clinicalInfo=Clinical_info.query.filter_by(id=id).first()

            if clinicalInfo is not None:

                db.session.delete(clinicalInfo)
                db.session.commit()

            else:
                return jsonify('Clinical info record specified does not exist'),500
        else:
            return jsonify('no token provided or token has expired'),500
    else:
        return jsonify('No clinical info id provided'),500


import discord
import os
import requests
import json
import random
from replit import db
from keep_alive import keep_alive
from discord.ext import commands,tasks
from pytube import YouTube
from pytube import Search
import pafy
import asyncio
from discord import FFmpegPCMAudio

bot = commands.Bot(command_prefix = '//')

check = False

@bot.command(name = "hello")
async def hello(ctx):
  await ctx.send("Hello {}".format(ctx.author))

@bot.command(name = "kill")
async def kill(ctx, args):
  await ctx.send("{} has beeen eliminated".format(args))

@bot.command(name = "speechless")
async def speechless(ctx):
  await ctx.send(file=discord.File('speechless.gif'))

@bot.command(name = "UPcm")
async def upcm(ctx):
  await ctx.send(file=discord.File('upcm.jpg'))

@bot.command(name = "play" ,aliases=["p"])
async def play(ctx, *args):

  global check

  global s
  voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)

  if voice == None:
    if not ctx.message.author.voice:
      await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
      return
    else:
      channel = ctx.message.author.voice.channel
      await channel.connect()

  server = ctx.message.guild
  vc = server.voice_client

  if not check:
    check = not check
    s = Search(' '.join(args))
    j = 1
    output = "**Please select a track with the** `//play 1-5` **command:**\n"
    for i in s.results:
      title = (i.title).encode('utf8')
      output += '**' + str(j) + ': **'
      output += (title.decode('utf8')) + '\n'
      if j == 5:
        break
      j += 1

    global message
    message = await ctx.send(output)

  else:
    if not args[0].isnumeric():
      await ctx.send("Choose index number of song.")
      return

    check = not check
    if vc.is_playing():
      vc.stop()
    vdo_index = eval(args[0])
    vdo_id = s.results[vdo_index-1].video_id

    p = pafy.new(vdo_id)
    ba = p.getbestaudio()

    try :

      async with ctx.typing():
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

        vc.play(FFmpegPCMAudio(ba.url, **FFMPEG_OPTIONS))
        vc.is_playing()

        await message.edit(content ='**Now playing:** {}'.format(p.title))
    except:
        await ctx.send("The bot is not connected to a voice channel.")

@bot.command(name='pause', help='This command pauses the song')
async def pause(ctx):
    voice_client = ctx.message.guild.voice_client
    if voice_client.is_playing():
        voice_client.pause()
        await ctx.send("Paused. Use resume command to resume.")

    else:
        await ctx.send("The bot is not playing anything at the moment.")

@bot.command(name='resume', help='Resumes the song')
async def resume(ctx):
    voice_client = ctx.message.guild.voice_client
    if voice_client.is_paused():
        voice_client.resume()
        await ctx.send("Resumed")

    else:
        await ctx.send("The bot was not playing anything before this. Use play_song command")

@bot.command(name='stop', help='Stops the song')
async def stop(ctx):
    voice_client = ctx.message.guild.voice_client
    if voice_client.is_playing():
        voice_client.stop()
        await ctx.send("Stopped")
    else:
        await ctx.send("The bot is not playing anything at the moment.")

@bot.command(name = 'guessmybday', help = "Guesses your birthday")
async def guessmybday(ctx):
  count = 0
  msg = await ctx.send("**Is your birthday in the following set?**\n 1  3   5   7\n 9  11  13  15\n17  19  21  23\n25  27  29 31\n")

  check = lambda m: m.author == ctx.author and m.channel == ctx.channel

  try:
    confirm = await bot.wait_for("message", check=check, timeout=60)
  except asyncio.TimeoutError:
    await msg.edit(content="Guessing cancelled, timed out.")
    return

  if confirm.content == "yes" or confirm.content == "no":
    if confirm.content == "yes":
      count += 1
    await msg.edit(content = "**Is your birthday in the following set?**\n 2   3   6   7\n10  11  14  15\n18  19 22  23\n26  27  30  31\n")

  check = lambda m: m.author == ctx.author and m.channel == ctx.channel

  try:
    confirm = await bot.wait_for("message", check=check, timeout=60)
  except asyncio.TimeoutError:
    await msg.edit(content="Guessing cancelled, timed out.")
    return

  if confirm.content == "yes" or confirm.content == "no":
    if confirm.content == "yes":
      count += 2
    await msg.edit(content = "**Is your birthday in the following set?**\n 4   5   6   7\n12  13  14  15\n20  21  22  23\n28  29  30  31\n")

  check = lambda m: m.author == ctx.author and m.channel == ctx.channel

  try:
    confirm = await bot.wait_for("message", check=check, timeout=60)
  except asyncio.TimeoutError:
    await msg.edit(content="Guessing cancelled, timed out.")
    return

  if confirm.content == "yes" or confirm.content == "no":
    if confirm.content == "yes":
      count += 4
    await msg.edit(content = "**Is your birthday in the following set?**\n 8  9  10  11\n12  13  14  15\n24  25  26  27\n28  29 30  31\n")

  check = lambda m: m.author == ctx.author and m.channel == ctx.channel

  try:
    confirm = await bot.wait_for("message", check=check, timeout=60)
  except asyncio.TimeoutError:
    await msg.edit(content="Guessing cancelled, timed out.")
    return

  if confirm.content == "yes" or confirm.content == "no":
    if confirm.content == "yes":
      count += 8
    await msg.edit(content = "**Is your birthday in the following set?**\n16  17  18  19\n20  21  22  23\n24  25  26  27\n28  29  30  31\n")

  check = lambda m: m.author == ctx.author and m.channel == ctx.channel

  try:
    confirm = await bot.wait_for("message", check=check, timeout=60)
  except asyncio.TimeoutError:
    await msg.edit(content="Guessing cancelled, timed out.")
    return

  if confirm.content == "yes" or confirm.content == "no":
    if confirm.content == "yes":
      count += 16
    await msg.edit(content = "Your Birthday is on **" + str(count) + "**")
  return

keep_alive()
bot.run(os.getenv('TOKEN'))


import cgi
import logging
import os
import random
import string

from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

KEY_RANGE = range(random.randint(24,32))
KEY_CHARS = string.ascii_letters;

# Database containing all labels to be appended to the user's photo. The label
# id must be called image_id so that DbFunctions can work on both Labels and
# ImageDB.
class LabelsDb (db.Model):
  image_type = db.StringProperty()
  image_id = db.StringProperty()
  content = db.BlobProperty()

# Database containing all images uploaded by users. The date field is used by
# cronjobs to clean up the table. (TBD)
class ImageDb(db.Model):
  image_type = db.StringProperty()
  image_id= db.StringProperty()
  content = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)

# Helper functions for accessing DB data.
class DbFunctions:
  def getImage(self, database, image_id):
    imageDb = db.GqlQuery('SELECT * FROM %s WHERE image_id = :1' % database,
                          image_id)
    # We return the first image available with the given image_id
    for image in imageDb:
      return image
    return None

  def getImages(self, database):
    imageDb = db.GqlQuery('SELECT * FROM %s ORDER BY image_id ASC' % database)
    return imageDb

  def addImage(self, image_content, image_type = 'image/jpeg', key = None):
    if key == None:
      key = (''.join(random.choice(KEY_CHARS) for x in KEY_RANGE))
    imageDb = ImageDb()
    imageDb.image_id = key
    imageDb.image_type = "image/jpeg"
    imageDb.content = db.Blob(image_content)
    imageDb.put();
    return key;

# Serves photos given ids and the database to read from
class GetPhoto(webapp.RequestHandler):
  def get(self):
    image_id = self.request.get('image_id')
    image_db = self.request.get('image_db')
    if image_db == None:
      imabe_db = 'ImageDb'

    if image_db == 'LabelsDb' or image_db == 'ImageDb':
      image = DbFunctions().getImage(image_db, image_id)
      if not image == None:
        image_type = 'image/png'
        if not image.image_type == None:
          image_type = image.image_type
        self.response.headers['Content-Type'] = image_type
        self.response.out.write(image.content)
        return;

    self.response.headers['Content-Type'] = 'text/html'
    self.response.set_status(404, 'Not Found')
    self.response.out.write('Imagem nao encontrada')

# Main page rendering function. You can always do the following to render the
# main page:
#  self.response.out.write((RenderMainPage(error_message='Could not do this')))
#  or
#  self.response.out.write((RenderMainPage(success_content=my_success_content)))
def RenderMainPage(success_content = '', error_message = ''):
    labels = DbFunctions().getImages('LabelsDb')
    template_values = {
        'labels': [],
        'error_message': error_message,
        'success_content': success_content
    }
    i = 0
    for label in labels:
      label_img = images.Image(label.content)
      template_values['labels'].append({ 'name': 'label%d' % i,
                                         'id': label.image_id })
      i = i + 1
    path = os.path.join(os.path.dirname(__file__), 'index.template')
    return template.render(path, template_values)

# Simple URL handler for the main page.
class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write(RenderMainPage())

# Given the content type return the corresponding appengine type.
def getImageTypeFromContentType(content_type):
  if content_type == 'image/gif':
   # appengine supports gif type as jpeg
   return images.JPEG;
  if content_type == 'image/jpeg':
   return images.JPEG
  if content_type == 'image/png':
   return images.PNG
  return None

# Handler that adds a label to the user photo.
class Legendario(webapp.RequestHandler):
  def post(self):
    self.response.headers['Content-Type'] = 'text/html'
    uploaded_image = self.request.POST['source_image']
    if uploaded_image == None or uploaded_image == '':
      self.response.out.write(RenderMainPage(error_message='Selecione uma foto.'))
      return;

    # extracts the photo type from the uploaded image
    content_type = uploaded_image.type
    image_type = getImageTypeFromContentType(content_type)
    if image_type == None:
      self.response.out.write(
        RenderMainPage(error_message='Tipo de imagem desconhecido. Use imagens JPEG, PNG ou GIF'))
      return;

    image_content = self.request.get('source_image')
    if len(image_content) > (1 << 20): # 1M
      self.response.out.write(RenderMainPage(
            error_message='Sua foto deve ter menos de 1 MB.'))
      return;

    label_name = self.request.get('label_name')
    if label_name == None or label_name == '':
      self.response.out.write(RenderMainPage(error_message='Escolha um dos labels.'))
      return;

    label = DbFunctions().getImage('LabelsDb', label_name)
    if label == None:
      self.response.out.write(RenderMainPage(
        error_message='Label \'%s\' nao encontrado' % label_name))
      return;

    imageDb = ImageDb()
    image = images.Image(image_content)
    label_img = images.Image(label.content)

    # There is this limitation on the appengine images library that doesn't
    # allow tranformations whose height or width is > 4000, so lets reduce image
    # right away. The label width and height is always guaranteed to be < than
    # 1000 pixels so, if we need to resize something, this thing is the user
    # height. The width will never exceed this limitation because we always
    # scale down the bigger photo and label width is always < than 1000.
    if image.height + label_img.height > 4000:
      # Since we know that label height size is not the reason for the 4000
      # exceed, lets resize image down.
      image.resize(height=(4000 - label_img.height))
      image = images.Image(image.execute_transforms(image_type))

    # Make image and label to have the same width. Scale down the bigger one.
    if label_img.width > image.width:
      label_img.resize(width=image.width)
      label_img = images.Image(label_img.execute_transforms(
          getImageTypeFromContentType(label.image_type)))
    else:
      image.resize(width=label_img.width)
      image = images.Image(image.execute_transforms(image_type))

    # now images have the same width. Height will never exceed the 4000 limit.
    no_crop_image = images.composite([(image, 0, 0, 1.0, images.TOP_RIGHT),
                               (label_img, 0, image.height, 1.0,
                                images.TOP_RIGHT) ], image.width,
                               image.height + label_img.height, 0,
                               images.JPEG)

    crop_image = images.composite([(image, 0, 0, 1.0, images.TOP_RIGHT),
                               (label_img, 0, image.height - label_img.height, 1.0,
                                images.TOP_RIGHT) ], image.width,
                               image.height, 0, images.JPEG)

    squared_width = image.width;
    squared_height = image.height + label_img.height;

    if (squared_width > squared_height): squared_height = squared_width
    else: squared_width = squared_height

    woffset = (squared_width - image.width) / 2
    squared_xpos = -1 * woffset;
    hoffset = (squared_height - (image.height + label_img.height))/2
    squared_ypos = hoffset

    squared_image  = images.composite(inputs=[(image, squared_xpos , hoffset, 1.0, images.TOP_RIGHT),
                               (label_img, squared_xpos, image.height + hoffset, 1.0,
                                images.TOP_RIGHT) ], width=squared_width,
                                height=squared_height, color=0xffffffff,
                                output_encoding=images.JPEG)

    results = [ no_crop_image, crop_image,  squared_image ]
    # Due to some weird behaviour of the transformation library, it may be the
    # case that the result is bigger than the len(label_img) + len(image). Why,
    # why, why??
    for result in results:
      if len(result) > (1 << 20):
        self.response.out.write(RenderMainPage(error_message='''Sua imagem ficou
        muito grande depois de acrescentar a legenda. Reduza o tamanho da sua
        imagem original. Se isso nao resolver, tente reduzir suas dimensoes ou
        sua resolucao. Se nada funcionar, mande um email para ademirao@gmail.com'''))
        return;

    no_crop_image_key = DbFunctions().addImage(no_crop_image)
    crop_image_key = DbFunctions().addImage(crop_image)
    squared_image_key  = DbFunctions().addImage(squared_image)
    self.response.out.write(RenderMainPage({
      'images': [ {
        'image_id': no_crop_image_key,
        'image_descr': 'Sem Cortes',
        }, {
        'image_id': crop_image_key,
        'image_descr': 'Cortada',
        }, {
        'image_id': squared_image_key,
        'image_descr': 'Quadrada Sem Cortes'
        }] }))

class AddLabel(webapp.RequestHandler):
  def post(self):
    labelDb = LabelsDb()
    uploaded_label = self.request.POST['source_label']
    content_type = uploaded_label.type
    label_type = getImageTypeFromContentType(content_type)
    if label_type == None:
      self.response.out.write('Tipo de imagem desconhecido. Use imagens JPEG, PNG ou GIF')
      return;

    logging.info(content_type)
    label_img = images.Image(self.request.get('source_label'))
    label_data = self.request.get('source_label')
    # Make sure label width is < than 1000
    if label_img.width > 1000:
      label_img.resize(width=1000)
      label_data = label_img.execute_transforms(label_type)

    if label_img.height > 500 :
      label_img.resize(height=500)
      label_data = label_img.execute_transforms(label_type)

    labelDb.image_id = self.request.POST['label_name']
    labelDb.content = db.Blob(label_data)
    labelDb.image_type = content_type
    labelDb.put();
    self.response.out.write('Label added! Name %s' % labelDb.image_id)

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/add_label', AddLabel),
                                      ('/legendame', Legendario),
                                      ('/photo', GetPhoto)], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()


import collections
class Solution:
    """
    @param formula: a string
    @return: return a string
    """
    def countOfAtoms(self, formula):
        # write your code here
        if not formula:
            return ""
        stack,l,i = [collections.Counter()],len(formula), 0
        while i < l:
            if formula[i] == '(':
                stack.append(collections.Counter())
                i += 1
            elif formula[i] == ')':
                top = stack.pop()
                i += 1
                i_start = i
                while i < l and formula[i].isdigit():
                    i += 1
                multi = int(formula[i_start:i] or 1)
                for name, v in top.items():
                    stack[-1][name] += v * multi
            else:
                i_start = i
                i += 1
                while i < l and formula[i].islower():
                    i += 1
                name = formula[i_start:i]
                i_start = i
                while i < l and formula[i].isdigit():
                    i += 1
                multi = int(formula[i_start:i] or 1)
                stack[-1][name] += multi
        result = ""
        for name in sorted(stack[-1]):
            result += name + (str(stack[-1][name]) if stack[-1][name] > 1 else "")
        return result


import dash
from dash import dcc
from dash import html
from dash import dash_table
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc

from flask import Flask
from flask import render_template, Response

import pandas as pd

import edgeiq
import cv2
import time

# edgeIQ
camera = edgeiq.WebcamVideoStream(cam=0)
obj_detect = edgeiq.ObjectDetection("alwaysai/ssd_mobilenet_v1_coco_2018_01_28")
obj_detect.load(engine=edgeiq.Engine.DNN)

# Data
data = pd.DataFrame()
START_TIME = time.time()

# functions for rendering frame and performing object detection
def gen_video_feed():
    while True:
        frame = camera.read()
        if frame is not None:
            frame = perform_object_detection(frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

def perform_object_detection(frame):
    """Perform object detction on an image, update
    the table data, and returns a string.

    Args:
        frame (numpy array): The frame from the camera stream.

    Returns:
        string: The string representation of the image
    """
    if frame is not None:
        results = obj_detect.detect_objects(frame, confidence_level=.5)
        frame = edgeiq.markup_image(
                frame, results.predictions, colors=obj_detect.colors)
        frame = edgeiq.resize(frame, width=800, height=300)
        frame = cv2.imencode('.jpg', frame)[1].tobytes()

        # update data for table
        objects = {
            'timestamp': str(round((time.time() - START_TIME), 0)),
            'labels': ", ".join([p.label for p in results.predictions])
        }

        global data
        if data is None:
            data = pd.DataFrame({k: [v] for k, v in objects.items()})
        else:
            data = data.append(pd.DataFrame({k: [v] for k, v in objects.items()}))

        data = data.drop_duplicates()

    return frame

# Flask app
app = Flask(__name__, instance_relative_config=False)

# Flask routes (add as needed)
@app.route('/video_feed')
def video_feed():
    return Response(gen_video_feed(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route("/")
def home():
    return render_template("index.html")

# Dash Setup
dash_app = dash.Dash(
    __name__,
    server=app,  # associate Flask
    assets_folder="./static",
    url_base_pathname='/dash/',
    external_stylesheets=[dbc.themes.LUX]
)

# Dash Layout
dash_app.layout = dbc.Container(fluid=True, children=[
    # body
    dbc.Row([
        dbc.Col(
            # streamer content
            html.Img(
                src="/video_feed",
                style={'position': 'center', 'width': 600, 'height': 350}
            )
        ),
    ]),
    dash_table.DataTable(
        id="logs",
        data=[],
        columns=[],
        style_as_list_view=False,
        page_action="native",
        page_size=10,
        export_format="csv",
        style_header={
            'backgroundColor': 'rgba(0,0,0,0.2)',
            'border': '1px solid white',
            'font-family': 'Nunito Sans'
        },
        style_cell={
            'backgroundColor': 'rgba(0,0,0,0.2)',
            'color': 'black',
            'text-align': 'left',
            'font-size': '14px',
            'font-family': 'Nunito Sans'
        },
        style_data={
            'border': '1px solid white'
        },
        sort_by={
            'column_id': 'timestamp',
            'direction': 'desc'
        }),
    # automatically update periodically
    dcc.Interval(
        id='interval-component',
        interval=1*5000,  # in milliseconds
        n_intervals=0
    )
])

# Dash Callbacks
@dash_app.callback(
    output=[Output("logs", "data"), Output("logs", "columns")],
    inputs=[Input('interval-component', 'n_intervals')])
def render_log_table(n_intervals):
    df = data
    return df.to_dict('records'), [{"name": i, "id": i} for i in df.columns]

if __name__ == "__main__":
    camera.start()
    try:
        app.run(host='localhost', port=5001, debug=False)
    except Exception as e:
        print(e)
    finally:
        camera.stop()


import torch.nn as nn
import torch.nn.functional as F
import torch

from ..builder import LOSSES
from .utils import weight_reduce_loss

def cross_entropy(pred,
                  label,
                  weight=None,
                  reduction='mean',
                  avg_factor=None,
                  class_weight=None):
    """Calculate the CrossEntropy loss.
    Args:
        pred (torch.Tensor): The prediction with shape (N, C), C is the number
            of classes.
        label (torch.Tensor): The gt label of the prediction.
        weight (torch.Tensor, optional): Sample-wise loss weight.
        reduction (str): The method used to reduce the loss.
        avg_factor (int, optional): Average factor that is used to average
            the loss. Defaults to None.
        class_weight (torch.Tensor, optional): The weight for each class with
            shape (C), C is the number of classes. Default None.
    Returns:
        torch.Tensor: The calculated loss
    """
    # element-wise losses
    loss = F.cross_entropy(pred, label, weight=class_weight, reduction='none')

    # apply weights and do the reduction
    if weight is not None:
        weight = weight.float()
    loss = weight_reduce_loss(
        loss, weight=weight, reduction=reduction, avg_factor=avg_factor)

    return loss

def semantic_exactly_one(pred):
    """Semantic loss.
    Args:
        pred (torch.Tensor): The prediction with shape (N, \*).

    Returns:
        torch.Tensor: The calculated semantic loss
    """
    prob = F.sigmoid(pred)
    wmc_tmp = torch.zeros_like(prob)
    # exactly one semantic loss based on definition 1
    for i in range(pred.shape[1]):
        one_situation = torch.ones_like(pred).scatter_(1, torch.zeros_like(pred[:, 0]).fill_(i).unsqueeze(-1).long(), 0)
        wmc_tmp[:, i] = torch.abs((one_situation - prob).prod(dim=1))
    _log_wmc_tmp = -1.0 * torch.log(wmc_tmp.sum(dim=1))
    return _log_wmc_tmp

@LOSSES.register_module()
class CESemanticLoss(nn.Module):
    """Cross entropy plus Semantic loss.
    Args:
        cls_score (torch.Tensor): The prediction with shape (N, \*).
        label (torch.Tensor): The gt label with shape (N, \*).

    Returns:
        torch.Tensor: The calculated CE with semantic loss for labelled und unlablled samples
    """
def __init__(self, ):
    super(CESemanticLoss, self).__init__()

def forward(self,
            cls_score,
            label):
    labelled_examples = label.sum(dim=1)
    unlabelled_examples = 1.0 - labelled_examples
    CE = torch.multiply(labelled_examples, self.loss_weight * self.cross_entropy(cls_score, label))
    semantic = 0.0005 * torch.multiply(labelled_examples, semantic_exactly_one(cls_score)) + \
               0.0005 * torch.multiply(unlabelled_examples, semantic_exactly_one(cls_score))
    CE_Semantic_Loss = torch.mean(torch.sum(torch.add(CE, semantic)))
    return CE_Semantic_Loss


import cv2
import numpy as np
import random

#########################################################
#       FUNCTION TO FIND THE CONNECTED COMPONENTS
#########################################################

def drawComponents(image, adj, block_size):

    #ret, labels = cv2.connectedComponents(image)

    #print(ret)
    #print(labels)
    #cv2.imshow('test1', labels.astype(np.uint8))

    image = image.astype('uint8')
    #print (image.shape)

    block_w = block_size
    block_h = block_size

    nb = 0
    comp = []

    for r in range(0, image.shape[0] - block_w, block_h):
        for c in range(0, image.shape[1] - block_w, block_h):
            window = image[r:r+block_w, c:c+block_h]
            x = list(cv2.connectedComponents(window, adj))
            nb += x[0]
            x[1] = x[1] * random.randint(1, 16) * random.randint(1, 16)
            comp.append(x[1])

    bc = image.shape[0]//block_size
    br = image.shape[1]//block_size

    img = np.zeros(image.shape)
    #print (img.shape)

    for r in range(0, img.shape[0] - block_w, block_h):
        for c in range(0, img.shape[1] - block_w, block_h):
            for i in range(len(comp)):
                img[r:r+block_w, c:c+block_h] = comp[i]*255

    for k in range(len(comp)):
        for i in range(block_size):
            for j in range(block_size):
                if k%br == 0 and k!=0:
                    c = (((k+1)*block_size)//img.shape[1])*block_size + j
                else:
                    c = ((k*block_size)//img.shape[1])*block_size + j

                r = (k*block_size + i) % (br*block_size)

                img[c][r] = comp[k][j][i]

    cv2.imshow('Test Image', img)

    #image = image.astype('uint8')
    #nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, adj)
    #label_hue = (107*output%np.max(output)).astype(np.uint8)

    label_hue = (107*img%np.max(img)).astype(np.uint8)
    blank_ch = 255*np.ones_like(label_hue)
    labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

    labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2RGB)

    labeled_img[label_hue==0] = 0

    '''
    img2 = np.zeros(output.shape)
    img2[output == max_label] = 255
    img2 = img2 + output
    '''
    return labeled_img

#########################################################
#                       INPUTS
#########################################################

flag = 15
while flag != 0:
    block = int(input("Please enter block size (m X m): "))
    if flag == 1 or flag == 15:
        adj = int(input("Enter the adjacency for detection (4 or 8): "))
    if flag == 2 or flag == 15:
        thresh = list(map(int, input("Enter the range of threshold separated by space(Example: 150 200): ").split(" ")))
    if adj != 4 and adj != 8:
        flag = 1
        print("Inoperable value for adjacency. Please enter 4 or 8")
        continue
    elif len(thresh) != 2:
        print("Please input exactly 2 numbers in the given format.")
        flag = 2
        continue
    elif thresh[0] > thresh [1]:
        thresh[0], thresh[1] = thresh[1], thresh[0]
    else:
        flag = 0
    if thresh[0] < 0 or thresh[1] > 255:
        print("Values are beyond limits. Please enter values between 0 and 255")
        flag = 2

#########################################################
#                   READING IMAGE
#########################################################

img_orig = cv2.imread('../../Images/2.jpg')

cv2.imshow('Original', img_orig)

#im = cv2.UMat(Image.fromarray(img_orig).convert("L"))
#Image.fromarray(img_orig)

bw = cv2.cvtColor(img_orig, cv2.COLOR_RGB2GRAY)
#cv2.imshow("BW", bw)
#cv2.imwrite("./Outputs/Grayscale.jpg", bw)

x, img = cv2.threshold(bw, thresh[0], thresh[1], cv2.THRESH_BINARY)  #ensuring binary
img[img==x] = 255
cv2.imshow("Binary", img)
#cv2.imwrite("./Outputs/Binary Image {V=("+str(thresh[0])+", "+str(thresh[1])+"), adj="+str(adj)+"}.jpg", img)

img2 = drawComponents(img, adj, block)   # calling implementation function
#print(img2.shape)
cv2.imshow('Connected Components', img2)
#cv2.imwrite("./Outputs/Paths{V=("+str(thresh[0])+", "+str(thresh[1])+"), adj="+str(adj)+"}.jpg", img2)

#########################################################
#                   PRINTING OUTPUT
#########################################################

#img3 = bw * (img2.reshape(img2.shape[0],img2.shape[1]))

# Using the hues from img2 and the saturation and luminosity from the original image to get proper results.

cvt = cv2.cvtColor(img_orig, cv2.COLOR_RGB2HSV)
img4 = np.zeros(cvt.shape)
img2 = cv2.cvtColor(img2.astype(np.uint8), cv2.COLOR_RGB2HSV)

for i in range(img2.shape[0]):
    for j in range(img2.shape[1]):
        img4[i][j][0] = (img2[i][j][0]*9 + cvt[i][j][1]*1)//10       # HUE
        img4[i][j][1] = (img2[i][j][1]*2 + cvt[i][j][1]*8)//10      # SATURATION
        img4[i][j][2] = cvt[i][j][2]                                # LIGHT VALUE

        if img2[i][j][0] == 0:
            img4[i][j] = 0

img4 = cv2.cvtColor(img4.astype(np.uint8), cv2.COLOR_HSV2RGB)

#img3 = bw + (img2.reshape(img2.shape[0],img2.shape[1]))
#img4 =  [[[i, i, i] for i in j] for j in img2]
#img5 = img_orig * img4
cv2.imshow('Result', img4.astype(np.uint8))
#cv2.imwrite("./Outputs/Result{V=("+str(thresh[0])+", "+str(thresh[1])+"), adj="+str(adj)+"}.jpg", img4.astype(np.uint8))
print ("Job done!")

cv2.waitKey(0)
cv2.destroyAllWindows()


from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
import math

browser = webdriver.Chrome()

try:
    def ln(x):
        return math.log(x)
    def sin(x):
        return math.sin(x)

    browser.get("http://suninjuly.github.io/explicit_wait2.html")
    # Selenium will Wait until price be 100$
    button = browser.find_element(By.ID, "book")
    price_text = WebDriverWait(browser, 10).until(EC.text_to_be_present_in_element((By.ID, "price"), "$100"))
    button.click()

    x = int(browser.find_element(By.ID, "input_value").text)

    # extract clean formula from text
    formula_element = browser.find_element(By.CSS_SELECTOR, "label > :nth-child(1)")
    formula = formula_element.text.split()[2].replace(",", "")
    result_formula = eval(formula)

    form_element = browser.find_element(By.ID, "answer")
    form_element.send_keys(result_formula)

    submit_buttom = browser.find_element(By.ID, "solve")
    submit_buttom.click()
finally:
    time.sleep(10)
    browser.quit()

    # add an empty line for unix system


import multiprocessing
import os
import glob
import sys
import json

from tqdm import tqdm

from extractors.default import *

def main():
    if not os.path.exists('../finished'):
        os.makedirs('../finished')
    for parser in availableParsers:
        if not os.path.exists('../finished/%s' % parser):
            os.makedirs('../finished/%s' % parser)

    mypath = sys.argv[1]
    fs = []
    if os.path.exists('files.json'):
        fs = json.load(open('files.json', 'r'))
    else:
        for root, directories, filenames in os.walk(mypath):
            for filename in filenames:
                fs.append(os.path.join(root, filename))
        with open('files.json', 'w') as f:
            f.write(json.dumps(fs))

    # # Testing purposes
    # for f in fs:
    #     print(f)
    #     parseRecipe(f)

    # Process all
    p = multiprocessing.Pool(multiprocessing.cpu_count())
    print("Processing %d files..." % len(fs))
    for i in tqdm(range(0, len(fs), 2 * multiprocessing.cpu_count())):
        p.map(parseRecipe, fs[i:i + 2 * multiprocessing.cpu_count()])

if __name__ == "__main__":
    main()


from facenet_pytorch import MTCNN, InceptionResnetV1
import torch
from torchvision import datasets
from torch.utils.data import DataLoader
import datetime

# 初始化预训练的pytorch人脸检测模型MTCNN和预训练的pytorch人脸识别模型InceptionResnet
mtcnn = MTCNN(image_size=240, margin=0, keep_all=False, min_face_size=40)
resnet = InceptionResnetV1(pretrained='vggface2').eval()

# 从照片集中读取数据
dataset = datasets.ImageFolder('/Users/zhengrongkai/PycharmProjects/Face-Recognition-PyTorch-main/images_pytorch')
# 关联名字和文件
idx_to_class = {i:c for c,i in dataset.class_to_idx.items()}
print('开始时间 :',datetime.datetime.now())
print('Training..')

def collate_fn(x):
    return x[0]

loader = DataLoader(dataset, collate_fn=collate_fn)

# 关联人名和照片的列表
name_list = []
# 嵌入矩阵列表
embedding_list = []

# 用MTCNN检测是否为人脸并且用InceptionResnet生成嵌入矩阵
for img, idx in loader:
    face, prob = mtcnn(img, return_prob=True)
    if face is not None and prob > 0.92:
        emb = resnet(face.unsqueeze(0))
        embedding_list.append(emb.detach())
        name_list.append(idx_to_class[idx])

# 保存模型数据
data = [embedding_list, name_list]
torch.save(data, '/Users/zhengrongkai/PycharmProjects/Face-Recognition-PyTorch/model.pt')
print('训练完成')
print('完成时间 :',datetime.datetime.now())


from pywinauto import Desktop
import time, requests, os, threading
import pyautogui
from pywinauto import timings

BASEURL = 'http://127.0.0.1:8000/'
PING_TIMEOUT = 45
PING_FREQUENCY = 45

QUEUE_LIMIT = 10
QUEUE_FREQUENCY = 5

q_processor = None
def exit_gracefully():
    if q_processor:
        q_processor.stop()
    exit(0)

class QProcessor():
    def __init__(self):
        self.thread = threading.Thread(target=self.process_queue)
        self.thread.setDaemon(True)
        self.stopping = False

    def start(self):
        self.thread.start()

    def process_queue(self):
        while not self.stopping:
            items = self.get_queue()

            if items:
                for item in items:
                    self.process(item['id'], item['component_id'], item['access_url'])

                time.sleep(0.1)
            else:
                time.sleep(QUEUE_FREQUENCY)

    def get_queue(self):
        data = []
        try:
            r = requests.get("{}{}".format(BASEURL, "job-queue/"), timeout=10)
            if r.status_code == 200:
                d = r.json()
                data = d['data_items'] if d['count'] > 0 else []
        except Exception as e:
            print(e)
            pass

        return data

    def process(self, id, component_id, access_url):
        try:
            r = requests.post("{}{}".format(BASEURL, "job-processor/"), data={
                'id' : id,
                'component_id' : component_id,
                'access_url' : access_url,
            }, timeout=45)
            print(r.json())
        except:
            try:
                windows = Desktop(backend="uia").windows()
                for win in windows:
                    try:
                        if ('OrCAD Capture CIS - Lite' in win.__str__()):
                            win.close()
                        else:
                            continue
                    except:
                        continue
            except:
                pass

    def stop(self):
        print('STOPPING TASK HANDLER.....')
        self.stopping = True
        while self.thread.is_alive():
            print('waiting for thread to finish.....')
            time.sleep(1)

if __name__ == '__main__':

    try:

        q_processor = QProcessor()
        q_processor.start()

        ping_check = 0
        while True:
            time.sleep(10)
            print('.')
            ping_check += 1
            if ping_check >= PING_FREQUENCY:
                ping_check = 0

    except KeyboardInterrupt:
        print("exiting..using keyboard")
        exit_gracefully()
    except SystemExit as se:
        print("system existing..{}".format(se))
        exit_gracefully()
    except Exception as e:
        print("Error happen..{}".format(e))


import uuid
from random import randint

class Producto:
    def __init__(self,descripcion,codigoBarras,precio,proveedor):
        self.id = uuid.uuid4()
        self.descripcion = descripcion
        self.clave = randint(1,200)
        self.codigoBarras = codigoBarras
        self.precio = precio
        self.proveedor = proveedor
    def __str__(self):
        return '{0}, {1}, {2}, {3}, {4}, {5}'.format(self.id,self.descripcion,self.clave,self.codigoBarras,self.precio,self.proveedor)

class Carrito:
    def __init__(self):
        self.listadoProductos = []
        self.usuario = ""
    def cargarProducto(self,prod,cant):
        self.listadoProductos.append([prod,cant])
    def mostrarProductos(self):
        i = 1
        for Producto in self.listadoProductos:
            print(str(i) + " - " + str(Producto[0].descripcion) + "\n")
            i=i+1

class ListaProductos:
    def __init__(self):
        self.listadoProductos = []
    def cargarProducto(self,prod):
        self.listadoProductos.append(prod)
    def mostrarProductos(self):
        i = 0
        for Producto in self.listadoProductos:
            print(str(i) + " - " + str(Producto.descripcion) + "\n")
            i=i+1

# Manzana = Producto("Fruta",1231241231,120,"Moño Azul")
# Carrito1 = Carrito()
# Carrito1.cargarProducto(Manzana,2)
# print(Carrito1.listadoProductos[0][0].descripcion)
# print(Carrito1.listadoProductos[0][1])
# print(Carrito1.listadoProductos)

menu = '''### MENÚ ###
- 1 Agregar Producto
- 2 Agregar al Carrito
- 3 Salir'''

opcion = True
listadoProductosObjeto = ListaProductos()
carritoProductosObjeto = Carrito()

while opcion == True :
    print(menu)
    op = int (input("Ingrese una Opción\n"))
    if op == 1:
        descripcion = input("Descripcion\n")
        codigoBarras = int (input("Codigo de Barras\n"))
        precio = int (input("Precio\n"))
        proveedor = input("Proveedor\n")
        objetoTransitorio = Producto(descripcion, codigoBarras, precio, proveedor)
        listadoProductosObjeto.cargarProducto(objetoTransitorio)
        print("Se agrego el Producto",objetoTransitorio)
        #listadoProductosObjeto(Producto(descripcion,codigoBarras,precio,proveedor))
    elif op == 2:
        listadoProductosObjeto.mostrarProductos()
        indice = int (input("Ingrese el numero del producto\n"))
        cantidad = int (input("cantidad\n"))
        productoTransitorio = listadoProductosObjeto.listadoProductos[indice]
        carritoProductosObjeto.cargarProducto(productoTransitorio,cantidad)
        carritoProductosObjeto.mostrarProductos()
    elif op == 3:
        opcion=False


#
# Create on 4/17/2018
#
# Author: Sylvia
#

"""
202. Happy Number
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by
the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops
endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

1^2 + 9^1 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
"""

class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        res = set()
        while n != 1:
            sum = 0
            if n in res:
                return False
            res.add(n)
            for d in str(n):
                sum += int(d) * int(d)
            n = sum
        return True

import pytest

class Test:
    @pytest.mark.parametrize('num, expect', [(19, True), (38, False), (1, True)])
    def test_normal(self, num, expect):
        res = Solution()
        assert res.isHappy(num) is expect


#Alex Black
import cv2
import numpy as np
import io
from PIL import Image
from threading import Thread
import collections

frame = cv2.VideoCapture( 0 )
kern = np.ones( ( 1, 1 ), np.uint8 )

i = 0
while True:
    i+=1
    print( i )
    _,cimg = frame.read()

    #cimg = cv2.imread( 'test.png' )

    cimgh, cimgw, _ = cimg.shape

    cimg = cv2.blur( cimg, ( 1, 1 ) )   #Quick blur removes some of the worst artifacts

    bwcimg = cv2.cvtColor( cimg, cv2.COLOR_BGR2GRAY )
    '''This section isolates areas'''
    diff = cv2.cvtColor( cv2.bitwise_xor( cimg, cv2.blur( cimg, ( 5, 5 ) ) ), cv2.COLOR_BGR2GRAY )
    white1 = np.array( [ 10 ] )
    white2 = np.array( [ 256 ] )
    maxdiff = cv2.cvtColor( cv2.inRange( cv2.blur( diff, ( 5, 5 ) ), white1, white2 ), cv2.COLOR_GRAY2BGR )
    edge = cv2.bitwise_and( cv2.Canny( cv2.blur( cimg, ( 2, 2 ) ), 133, 100 ), cv2.cvtColor( cv2.bitwise_and( cimg, maxdiff ), cv2.COLOR_BGR2GRAY ) )
    w1 = np.array( [ 50 ] )
    w2 = np.array( [ 256 ] )
    edge = cv2.cvtColor( cv2.inRange( edge, w1, w2 ), cv2.COLOR_GRAY2BGR )
    dedge = cv2.dilate( edge, np.ones( ( 5, 5 ), np.uint8 ), iterations = 3 )
    rcimg = cv2.bitwise_and( dedge,  cimg )

    '''This section is for contour identification'''
    bwcimg = cv2.cvtColor( rcimg, cv2.COLOR_BGR2GRAY )
    ret, thresh = cv2.threshold( bwcimg, 0, 255, 0 )
    dthresh = cv2.dilate( thresh, np.ones( ( 5, 5 ), np.uint8 ), iterations = 5 )
    im2, contours, hierarchy = cv2.findContours( thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
    #cv2.drawContours( cimg, contours, -1, ( 0, 255, 0 ), 3 )

    fMask = np.zeros( ( cimgh, cimgw ), np.uint8 )
    cMask = np.zeros( ( cimgh, cimgw ), np.uint8 )
    for o in contours:
        cv2.fillPoly( cMask, pts = [ o ], color = 255 )
        pcontavg = cv2.meanStdDev( bwcimg, mask = cMask )
        pcontmodavg = cv2.meanStdDev( bwcimg, mask = cv2.bitwise_xor( cv2.dilate( cMask, np.ones( ( 5, 5 ), np.uint8 ), iterations = 3 ), cMask ) )
        #For this if statement, it appears that the magic value for the blue star (greyscaled) example is between 10.0125 and 10.01, and the splotches of white tend to fall within a seperate but similar range
        #Having an upper bound may not work later for different targets, notably white one
        #A value of ten should pick up many dull objects in theory
        if( abs( pcontavg[ 1 ][ 0 ] - pcontmodavg[ 1 ][ 0 ] ) > 10.0 ):
            fMask = cv2.bitwise_or( fMask, cMask )
        cMask = np.zeros( ( cimgh, cimgw ), np.uint8 )

    '''Just some other things, display mostly'''
    cv2.imshow( 'ori', cimg )
    cv2.imshow( 'cut1', cv2.bitwise_and( cv2.cvtColor( dthresh, cv2.COLOR_GRAY2BGR ), cimg ) )
    cv2.imshow( 'cut2', cv2.bitwise_and( cv2.cvtColor( fMask, cv2.COLOR_GRAY2BGR ), cimg ) )

    if cv2.waitKey(1) == 27:
        break
cv2.destroyAllWindows()
frame.release()


import requests
import bs4
from bs4.dammit import EncodingDetector
from urllib.request import Request, urlopen, urlretrieve

def get_flag(url):
    try:
        url = url.replace(" ", "%20")
        useragent = ["Mozilla/5.0 (compatible; Googlebot/2.1; +http://google.com/bot.html)",
                     "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0",
                     "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0",
                     "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0"]
        headers = {"User-Agent": useragent[1]}
        req = Request(url)
        a = urlopen(req).read()
        html_encoding = EncodingDetector.find_declared_encoding(a, is_html=True)
        search_soup = bs4.BeautifulSoup(a, from_encoding=html_encoding, features="lxml")
        resp = search_soup.find_all('a')
        for i in resp:
            filename = i.extract().get_text()
            url_new = url+filename
            url_new = url_new.replace(" ", "%20")
            if (filename[-1] == '/' and filename[0] != '.'):
                get_flag(url_new)
                print(url_new)

    except Exception as e:
        print(e)

result = get_flag("https://www.google.com/search?client=firefox-b-d&q=questrade")
print(result)


"""
Factory for HID transport connections.

Currently supports only Cython/HIDAPI
"""

import platform
from logging import getLogger
from ..pyedbglib_errors import PyedbglibNotSupportedError

def hid_transport(library="hidapi"):
    """
    Dispatch a transport layer for the OS in question

    The transport layer is typically used to connect to a tool and then it is passed in as a parameter when creating
    protocol objects.  An example where the transport layer is used to create an instance of the housekeepingprotocol
    for communication with the nEDBG debugger::

        from pyedbglib.hidtransport.hidtransportfactory import hid_transport
        transport = hid_transport()
        connect_status = False
        try:
            connect_status = transport.connect(serial_number='', product='nedbg')
        except IOError as error:
            print("Unable to connect to USB device ({})".format(error))

        if not connect_status:
            print("Unable to connect to USB device")

        housekeeper = housekeepingprotocol.Jtagice3HousekeepingProtocol(transport)

    :param library: Transport library to use, currently only 'hidapi' is supported which will use the libusb hidapi
    :type library: string
    :returns: Instance of transport layer object
    :rtype: class:cyhidapi:CyHidApiTransport
    """
    logger = getLogger(__name__)
    operating_system = platform.system().lower()
    logger.debug("HID transport using library '{:s}' on OS '{:s}'".format(library, operating_system))

    # HID API is the primary transport
    if library == 'hidapi':
        hid_api_supported_os = ['windows', 'darwin', 'linux', 'linux2']
        if operating_system in hid_api_supported_os:
            from .cyhidapi import CyHidApiTransport
            return CyHidApiTransport()

        msg = "System '{0:s}' not implemented for library '{1:s}'".format(operating_system, library)
        logger.error(msg)
        raise PyedbglibNotSupportedError(msg)

    # Other transports may include cmsis-dap DLL, atusbhid (dll or so) etc
    msg = "Transport library '{0}' not implemented.".format(library)
    logger.error(msg)
    raise PyedbglibNotSupportedError(msg)


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

# In[38]:

# Importar las librerias
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import sklearn

# In[2]:

get_ipython().run_line_magic('cd', "'/home/jovyan/python/dataset'")
get_ipython().run_line_magic('ls', '')

# In[9]:

dir = '/home/jovyan/python/dataset/{}'.format('data4.csv')
data = pd.read_csv(dir)

# In[10]:

len(data)

# ### Dividir utilizando la distribución normal

# In[11]:

a = np.random.randn(len(data))

# In[12]:

plt.hist(a)

# In[23]:

check = (a <0.75)

# In[24]:

check

# In[25]:

plt.hist(check.astype(int))

# In[26]:

training = data[check]
testing = data[~check]

# In[27]:

len(training)

# In[28]:

len(testing)

# ### Con la libreria sklearn

# In[31]:

train, test = train_test_split(data, test_size = 0.2)

# In[32]:

len(train)

# In[33]:

len(test)

# ### Usando una función de shuffle

# In[36]:

data.head()

# In[41]:

data = sklearn.utils.shuffle(data)

# In[44]:

cut_id = int(0.75* len(data))
train_data = data[:cut_id ]
test_data = data[cut_id +1:]

# In[45]:

len(train_data)

# In[46]:

len(test_data)

# In[ ]:


from lib2to3.pgen2 import token
import requests
import json

#Variáveis iniciais

def get_logradouro():

    inicio = 1
    qtregistros = 99
    mais_paginas = True
    token_entidade = "xxxxxx"
    conta_registros = 0
    lote = 0
    #Parametros da requisição

    url = "https://e-gov.betha.com.br/glb/service-layer/v2/api/logradouros/"
    querystring = {"iniciaEm": inicio,"nRegistros": qtregistros}
    payload = ""
    headers = {"Authorization": "Bearer "+ token_entidade}
    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

    logradouros_json = json.loads(response.text)

    mais_paginas = logradouros_json['maisPaginas']
    print("Possui mais registros?:" + str(logradouros_json['maisPaginas']))
    print("Quantidade de registros retornados no lote:" + str(len(logradouros_json['conteudo'])))

    while mais_paginas:
        lote += 1
        querystring = {"iniciaEm": inicio,"nRegistros": qtregistros}
        response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
        logradouros_json = json.loads(response.text)
        print("Lote = " + str(lote))
        print("Registros = " + str(conta_registros))
        print("Possui mais registros?:" + str(logradouros_json['maisPaginas']))
        print("Quantidade de registros retornados no lote:" + str(len(logradouros_json['conteudo'])))
        for logradouro in logradouros_json['conteudo']:
            logradouros = open('logradouros.json', 'a', encoding='utf-8')
            logradouros.write(str(logradouro).replace("'",'"'))
            logradouros.write('\n')
            inicio = logradouro['idGerado']['iLogradouros']
            logradouros.close
            conta_registros += 1

        mais_paginas = logradouros_json['maisPaginas']

get_logradouro()


import os
import sys
import argparse

from spinalcordtoolbox.utils import Metavar, SmartFormatter, init_sct, extract_fname, printv

def get_parser():
    # Initialize the parser
    parser = argparse.ArgumentParser(
        description='Transpose bvecs file (if necessary) to get nx3 structure.',
        formatter_class=SmartFormatter,
        add_help=None,
        prog=os.path.basename(__file__).strip(".py")
    )

    mandatory = parser.add_argument_group("\nMANDATORY ARGUMENTS")
    mandatory.add_argument(
        '-bvec',
        metavar=Metavar.file,
        required=True,
        help="Input bvecs file. Example: bvecs.txt"
    )
    optional = parser.add_argument_group("\nOPTIONAL ARGUMENTS")
    optional.add_argument(
        "-h",
        "--help",
        action="help",
        help="Show this help message and exit."
    )
    optional.add_argument(
        '-o',
        metavar=Metavar.file,
        default='',
        help="Output bvecs file. By default, input file is overwritten. Example: bvecs_t.txt"
    )
    optional.add_argument(
        '-v',
        choices=['0', '1', '2'],
        default='1',
        help="Verbose: 0 = nothing, 1 = basic, 2 = extended."
    )

    return parser

# MAIN
# ==========================================================================================
def main(args=None):

    parser = get_parser()
    if args:
        arguments = parser.parse_args(args)
    else:
        arguments = parser.parse_args(args=None if sys.argv[1:] else ['--help'])

    fname_in = arguments.bvec
    fname_out = arguments.o
    verbose = int(arguments.v)
    init_sct(log_level=verbose, update=True)  # Update log level

    # get bvecs in proper orientation
    from dipy.io import read_bvals_bvecs
    bvals, bvecs = read_bvals_bvecs(None, fname_in)

    # # Transpose bvecs
    # printv('Transpose bvecs...', verbose)
    # # from numpy import transpose
    # bvecs = bvecs.transpose()

    # Write new file
    if fname_out == '':
        path_in, file_in, ext_in = extract_fname(fname_in)
        fname_out = path_in + file_in + ext_in
    fid = open(fname_out, 'w')
    for iLine in range(bvecs.shape[0]):
        fid.write(' '.join(str(i) for i in bvecs[iLine, :]) + '\n')
    fid.close()

    # display message
    printv('Created file:\n--> ' + fname_out + '\n', verbose, 'info')

# Start program
# =======================================================================================================================
if __name__ == "__main__":
    init_sct()
    # call main function
    main()


from py2neo import Graph, Node, Relationship
from flask import jsonify
import json
import re
import chardet
from demjson import decode
import numpy as np
from neo4j import GraphDatabase, basic_auth, kerberos_auth, custom_auth, TRUST_ALL_CERTIFICATES

def build_nodes(node_record):
    data = {'id': str(node_record['id(n)'])}
    return {'data': data}

def build_edges(relation_record):
    data = {'source': str(relation_record['id(k)']),
            'target': str(relation_record['id(n)']),
            'relationship': "生产"}
    return {'data': data}

def old():
    graph = Graph()
    nodes = list(map(build_nodes, graph.run('MATCH (n:product) WHERE n.title=~".*索尼.*" RETURN id(n) LIMIT 50').data()))
    nodes.append({'data': {'id': '14'}})
    edges = list(map(build_edges, graph.run(
        'MATCH (n:product) WHERE n.title=~".*索尼.*" MATCH (k)-[r]-(n) RETURN id(n),id(k) LIMIT 50').data()))
    elements = {'nodes': nodes, 'edges': edges}
    js = json.dumps(elements)
    print(js)

def re_data():
    graph = Graph()
    data = graph.run('match (n:newNode)-[r]-(k:newNode) return n,r,k').data()
    # print(type(data))
    # print(data)
    # for i in data:r'^("[^"]+")([^"]+)("[^"]+")'
    #     for e in i:
    #         if 'title' in e:
    #             print(i)
    nodes = []
    edges = []
    # print(data)
    for i in data:
        for e in i:
            if 'title' in i[e]:
                id_pattern = re.compile(r'_(\d+):')
                id = id_pattern.findall(str(i[e]))
                label_pattern = re.compile(r':(\w+)')
                label = label_pattern.findall(str(i[e]))
                title_pattern = re.compile(r'title: \'(\S+)\'')
                title = title_pattern.findall(str(i[e]))
                t_title = eval(repr(title).replace('\\\\', '\\'))
                s_title = str(t_title).replace("['", "").replace("']", "")

                info_pattern = re.compile(r'\"(.+)\"')
                info = info_pattern.findall(str(i[e]))
                t_info = eval(repr(info).replace('\\\\', '\\'))
                s_info = str(t_info).replace("['", "").replace("']", "")

                data = {'id': str(id).replace("['", "").replace("']", ""),
                        'label': str(label).replace("['", "").replace("']", ""),
                        'title': s_title, 'info': s_info}
                nodes.append({'data': data})
            else:
                target_pattern = re.compile(r'_(\d+)\)-')
                target = target_pattern.findall(str(i[e]))
                source_pattern = re.compile(r'->\(_(\d+)')
                source = source_pattern.findall(str(i[e]))
                rela_pattern = re.compile(r'\'(.+)\'')
                rela = rela_pattern.findall(str(i[e]))
                r = eval(repr(rela).replace('\\\\', '\\'))
                s = str(r).replace("['", "").replace("']", "")
                data = {'source': str(source).replace("['", "").replace("']", ""),
                        'target': str(target).replace("['", "").replace("']", ""),
                        'relationship': s}
                edges.append({'data': data})
    elements = {'nodes': nodes, 'edges': edges}
    js = json.dumps(elements, ensure_ascii=False)
    print(js)

class Solution:
    def entityParser(self, text: str) -> str:
        data = text.replace(""", "\"").replace("'", "\'").replace("&", "&").replace(">", ">").replace(
            "<", "<").replace("⁄", "/")
        return data

def jaccard_coefficient(terms_model, reference):
    grams_reference = set(reference)
    grams_model = set(terms_model)
    temp = 0
    for i in grams_reference:
        if i in grams_model:
            temp = temp + 1
    dis = len(grams_model) + len(grams_reference) - temp
    jaccard_res = float(temp / dis)
    return jaccard_res

def dice_coefficient(a, b):
    """dice coefficient 2nt/na + nb."""
    a_bigrams = set(a)
    b_bigrams = set(b)
    overlap = len(a_bigrams & b_bigrams)
    return overlap * 2.0 / (len(a_bigrams) + len(b_bigrams))

def edit_distance(word1, word2):
    len1 = len(word1)
    len2 = len(word2)
    dp = np.zeros((len1 + 1, len2 + 1))
    for i in range(len1 + 1):
        dp[i][0] = i
    for j in range(len2 + 1):
        dp[0][j] = j
    for i in range(1, len1 + 1):
        for j in range(1, len2 + 1):
            delta = 0 if word1[i - 1] == word2[j - 1] else 1
    dp[i][j] = min(dp[i - 1][j - 1] + delta, min(dp[i - 1][j] + 1, dp[i][j - 1] + 1))
    return dp[len1][len2]

def print_title_of(tx, name):
    for record in tx.run("MATCH (a:class)-[]->(f) "
                         "WHERE a.name = {name} "
                         "RETURN f.name", name=name):
        print(record["f.name"])

if __name__ == '__main__':
    uri = "bolt://localhost:7687"
    driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
    session = driver.session()
    data = session.run("MATCH (m)-[r]->(n) RETURN m.title, r.relation, n.title LIMIT 100")
    # print(data)
    blists = []
    out_j = []
    out_d = []
    for d in data:
        bs = str(d[0])
        blists.append(bs)
    for i in range(len(blists)):
        for j in range(0, i):
            a = blists[i]
            b = blists[j]
            td_j = jaccard_coefficient(a, b)
            td_d = dice_coefficient(a, b)
            std = edit_distance(a, b) / max(len(a), len(b))
            fy = 1 - std
            avg_j = (td_j + fy) / 2
            avg_d = (td_d + fy) / 2
            if avg_j < 1:
                # print(blists[i], blists[j])
                # print('avg_sim: ', avg_j)
                out_j.append(blists[i] + " " + blists[j] + " " + str(avg_j))
            if avg_d < 1:
                out_d.append(blists[i] + " " + blists[j] + " " + str(avg_d))
        list_j = list(set(out_j))
        list_d = list(set(out_d))
        list_j.sort()
        list_d.sort()
        print(list_j)


import os
from typing import Dict, List, Optional

class CopySpec:
    """Copy specification of a single file or directory."""

    def __init__(self, source_path: str, target_path: Optional[str] = None):
        self.source_path = source_path
        self.target_path = target_path

    def get_target(self) -> str:
        """Get the target path of the copy spec.

        Returns:
            str: If a target_path is given explicitly, it will be returned.
                 Otherwise the source_path will be returned.
        """
        if self.target_path is not None:
            return self.target_path
        return self.source_path

def copy_templates(
    template_dir: str,
    target_dir: str,
    template_file_mapping: List[CopySpec],
    variables: Dict[str, str],
) -> None:
    """Copy templates from the template dir to the target dir.

    Args:
        template_dir (str): Path to the directory containing the template files.
        target_dir (str): Path to the target directory.
        template_file_mapping (Dict[str, str]): A mapping of source path to target path.
        variables (Dict[str, str]): Name to value mapping which will be replaced when parsing the template files.
    """
    for file_to_copy in template_file_mapping:
        with open(
            os.path.join(
                template_dir,
                file_to_copy.source_path,
            ),
            encoding="utf-8",
        ) as file_in:
            target_file_path = os.path.join(target_dir, file_to_copy.get_target())

            os.makedirs(os.path.split(target_file_path)[0], exist_ok=True)
            with open(target_file_path, encoding="utf-8", mode="w") as file_out:
                lines = file_in.readlines()
                for line in lines:
                    for key, value in variables.items():
                        line = line.replace("${{ " + key + " }}", value)
                    file_out.write(line)


#!/usr/bin/env python3

import uuid
from typing import List

import click
import backoff
from bravado.exception import HTTPConflict, HTTPError

from neptune.new.internal.backends.hosted_neptune_backend import HostedNeptuneBackend
from neptune.new.internal.credentials import Credentials

class Invitation:
    def __init__(self, _uuid: uuid.UUID, invitee: str):
        self.uuid = _uuid
        self.invitee = invitee

@backoff.on_exception(backoff.expo, HTTPError, max_tries=5)
def invite_member(backend, organization, invitee) -> List[Invitation]:
    payload = {
        "organizationIdentifier": organization,
        "invitationsEntries": [
            {
                "invitee": invitee,
                "invitationType": "emailRecipient",
                "roleGrant": "member",
                "addToAllProjects": False
            }
        ]
    }

    try:
        response = backend.backend_client.api.createOrganizationInvitations(
            newOrganizationInvitations=payload,
            **backend.DEFAULT_REQUEST_KWARGS)\
            .response()
    except HTTPConflict:
        click.echo(f"ERROR: Pending invitation for '{invitee}'")
        return []

    return list(map(lambda r: Invitation(_uuid=r.id, invitee=r.invitee), response.result.invitations or []))

@backoff.on_exception(backoff.expo, HTTPError, max_tries=5)
def remove_member(backend: HostedNeptuneBackend, organization: str, username: str):
    backend.backend_client.api.deleteOrganizationMember(
        organizationIdentifier=organization,
        userId=username,
        **backend.DEFAULT_REQUEST_KWARGS)\
        .response()

    click.echo(f"Removed '{username}' from organization '{organization}'")

@click.group()
def cli():
    pass

@cli.command()
@click.argument('organization')
@click.option('--invitee-email', 'email', help='Email to invite')
@click.option('--admin-api-token', envvar='NEPTUNE_API_TOKEN', help='API Token for organization Admin')
def invite(admin_api_token, email, organization):
    credentials = Credentials(api_token=admin_api_token)
    config_api_url = credentials.api_url_opt or credentials.token_origin_address
    backend = HostedNeptuneBackend(credentials=credentials)

    invitations = invite_member(backend=backend, organization=organization, invitee=email)

    for invitation in invitations:
        click.echo(f"{config_api_url}/-/invitations/organization/{invitation.uuid}")

@cli.command()
@click.argument('organization')
@click.option('--removed-username', 'username', help='User to removal')
@click.option('--admin-api-token', envvar='NEPTUNE_API_TOKEN', help='API Token for organization Admin')
def remove(admin_api_token, username, organization):
    credentials = Credentials(api_token=admin_api_token)
    backend = HostedNeptuneBackend(credentials=credentials)

    remove_member(backend=backend, organization=organization, username=username)

if __name__ == '__main__':
    cli()