import torch
import numpy as np
#import matplotlib.pyplot as plt
#import seaborn as sns
import os
import csv
import sys
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from time import time
import os
import math
from HardNet import HardNet
from PIL import Image
import pandas as pd

def greedy_iterative_snns_slow(dmv1, do_mutual=False):
    dmv = dmv1.detach().clone().cpu()
    MAXVAL = dmv1.max() +100
    h,w = dmv.size()
    min_size = min(h,w)
    out = torch.zeros(min_size,3)
    min_dist_r2, idxs_in_1 = torch.min(dmv, 0)
    min_dist_c2, idxs_in_2 = torch.min(dmv, 1)
    idxs_c_in1 = torch.arange(h)
    idxs_r_in2 = torch.arange(w)
    #vals1 = []
    if h < w:
        mutual_mask = idxs_in_1[idxs_in_2[:]] == idxs_c_in1#.cuda()
        for i in range(h):
            #dmv = dmv.double()
            #min_dist_r2, idxs_in_1 = torch.min(dmv, 0)
            min_dist_c2, idxs_in_2 = torch.min(dmv, 1)
            if do_mutual:
                min_dist_c2 = min_dist_c2.view(-1) - 1000*mutual_mask.float()
                #That is a hack for first having mutual neighbors, and then the rest
            val, min_dist_c2_idx = torch.min(min_dist_c2.view(-1),0)
            assert val == dmv.min()
            y = min_dist_c2_idx.item()
            row = dmv[y,:].clone()
            val1, x = torch.min(row.view(-1),0)
            col = dmv[:,x].clone()
            dmv[y,x] = MAXVAL
            col[y]=MAXVAL
            row[x]=MAXVAL
            out[i,0] = y#idx in img1
            out[i,1] = x#idx in img2
            out[i,2] = -(row.min()+ col.min())/ (2.0*val.float())#iSSN ratio
            dmv[:,x] = MAXVAL
            dmv[y,:] = MAXVAL
    else:
        mutual_mask = idxs_in_2[idxs_in_1[:]] == idxs_r_in2#.cuda()
        #dmv = dmv.double()
        for i in range(w):
            min_dist_r2, idxs_in_1 = torch.min(dmv, 0)
            #min_dist_c2, idxs_in_2 = torch.min(dmv, 1)
            if do_mutual:
                min_dist_r2 = min_dist_r2.view(-1)-1000*mutual_mask.float()
                #That is a hack for first having mutual neighbors, and then the rest
            #print (min_dist_r2.view(-1).topk(2,0, False)[0])
            val, min_dist_r2_idx = torch.min(min_dist_r2.view(-1).float(),0)
            #vals1.append(val.item())
            x = min_dist_r2_idx.item()
            col = dmv[:,x].clone()
            val1, y = torch.min(col.view(-1),0)
            row = dmv[y,:].clone()
            #col[col<val.float()] = MAXVAL
            #row[row<val.float()] = MAXVAL
            col[y]=MAXVAL
            row[x]=MAXVAL
            dmv[y,x] = MAXVAL
            out[i,0] = y#idx in img1
            out[i,1] = x#idx in img2
            out[i,2] = -(row.min()+ col.min())/(2.0*val)#iSSN ratio
            #print (row.min().item(), col.min().item())
            dmv[:,x] = MAXVAL
            dmv[y,:] = MAXVAL
    out[i,2] = -1
    out[:,0] += 1
    out[:,1] += 1
    vals, idxs = torch.sort(out[:,2])
    out2 = out[idxs].detach().cpu()
    return out2.float().cpu()

def read_circle_patches(fname, rot_ang = 0):
    #patches = np.loadtxt(fname, delimiter=',') #24 sec to read
    patches = pd.read_csv(fname,header=None, sep =',', dtype=np.uint8).values #6 sec to read. Still huge :(
    num, area = patches.shape
    PS = int(np.sqrt(area))
    assert PS == 137
    patches = np.reshape(patches, (num,1, PS, PS))
    return patches

def crop_round_patches(circle_patches, cropsize=97):
    num,ch,h,w = circle_patches.shape
    assert h == 137
    assert w == 137
    PS = h
    PS_crop = cropsize
    pad = (PS - PS_crop)//2
    crop_patches = circle_patches[:,:,pad:pad+PS_crop,pad:pad+PS_crop]
    return crop_patches

def rotate_circle_patches(cp, rot_angles):
    ropatches = np.ndarray(cp.shape, dtype=np.uint8)
    for i in range(len(cp)):
        ropatches[i,0,:,:] = np.array(Image.fromarray(cp[i,0,:,:]).rotate(-rot_angles[i], resample=Image.BILINEAR))
    return ropatches

def resize_patches(rp, PS=32):
    num,ch,h,w = rp.shape
    out_patches = np.ndarray((num,ch, PS,PS), dtype=np.uint8)
    for i in range(len(rp)):
        out_patches[i,0,:,:] = np.array(Image.fromarray(rp[i,0,:,:]).resize((PS,PS), resample=Image.LANCZOS))
    return out_patches

def describe_with_default_ori(fname, model):
    model = model.cuda()
    cp = read_circle_patches(fname)
    angles = np.loadtxt(fname.replace('big_patches', 'ori'))
    cp_rot = rotate_circle_patches(cp,angles)
    rp_rot = crop_round_patches(cp_rot)
    out_patches = resize_patches(rp_rot).astype(np.float32)
    n_patches = len(out_patches)
    bs = 128
    outs = []
    n_batches = int(n_patches / bs) + 1
    descriptors_for_net = np.zeros((n_patches, 128))
    for i in range(0, n_patches, bs):
        data_a = out_patches[i: i + bs, :, :, :]
        data_a = torch.from_numpy(data_a).cuda()
        with torch.no_grad():
            out_a = model(data_a)
        descriptors_for_net[i: i + bs,:] = out_a.data.cpu().numpy().reshape(-1, 128)
    descriptors_for_net = descriptors_for_net + 0.45;
    out = (descriptors_for_net * 210.).astype(np.int32)
    out = np.clip(out, 0, 255).astype(np.uint8)
    return out

def match_fname(pair, descdir, descname, matchdir):
    fn1 = os.path.join(descdir, pair[0]+'_'+descname + '.csv')
    fn2 = os.path.join(descdir, pair[1]+'_'+descname + '.csv')
    mfn = os.path.join(matchdir, pair[0]+'_'+pair[1]+'_'+descname + '.csv')
    return fn1, fn2, mfn

def distance_matrix_vector(anchor, positive):
    """Given batch of anchor descriptors and positive descriptors calculate distance matrix"""
    d1_sq = torch.sum(anchor * anchor, dim=1).unsqueeze(-1)
    d2_sq = torch.sum(positive * positive, dim=1).unsqueeze(-1)
    eps = 1e-5
    return torch.sqrt(torch.abs((d1_sq.repeat(1, positive.size(0)) + torch.t(d2_sq.repeat(1, anchor.size(0)))
                    - 2.0 * torch.bmm(anchor.unsqueeze(0), torch.t(positive).unsqueeze(0)).squeeze(0)))+eps)


import cv2
import numpy as np
import time
import math
from shooterdriver import ShooterDriver

driver = ShooterDriver()

def make_coordinates(image, line_parameters):
    # if (line_parameters[0] < 3.0e-10 and line_parameters[1] > 1.00000000e+01) or (line_parameters[0] > 3.0e-10 and line_parameters[1] < 1.00000000e+01):
    slope, intercept = line_parameters
    y1 = image.shape[0]
    y2 = int(y1*(7/10))
    x1 = int((y1 - intercept)/slope)
    x2 = int((y2 - intercept)/slope)
    return np.array([x1 ,y1 ,x2, y2])

def average_slope_intercept(image, lines):
    left_line=[]
    right_line=[]
    left_fit=[]
    right_fit=[]

    height, width, _ = image.shape

    left_boundary = width  # left lane line segment should be on left 2/3 of the screen
    right_boundary = 0 # right lane line segment should be on left 2/3 of the screen

    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            parameters = np.polyfit((x1,x2),(y1,y2), 1)
            slope = parameters[0]
            intercept = parameters[1]
            if slope < 0:
                if (x1 < left_boundary and x2 < left_boundary):
                    left_fit.append((slope, intercept))
            else:
                if (x1 > right_boundary and x2 > right_boundary):
                    right_fit.append((slope, intercept))
    left_fit_average = np.average(left_fit, axis=0)
    right_fit_average = np.average(right_fit, axis=0)
    if len(left_fit) > 0:
        left_line.append(make_coordinates(image, left_fit_average))
    if len(right_fit) > 0:
        right_line.append(make_coordinates(image, right_fit_average))

    return np.array([left_line, right_line])

def canny(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    lower  = np.array([160, 80, 80]) #Color range grade (160-240 is blue) , Saturation, Value
    upper = np.array([200, 255, 255])
    gray = cv2.inRange(hsv, lower, upper)
    # cv2.imwrite("gray.png", gray)
    # gray2 = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
    # cv2.imwrite("hsv.png", hsv)
    #cv2.imwrite('./resultGRAY.png',gray2)
    canny = cv2.Canny(gray, 190, 255)
    cv2.imwrite('./resultCanny.png',canny)
    return canny

def display_lines(image, lines):
    print("line ", lines)
    try:
        height, width, _ = image.shape
        line_image = np.zeros_like(image)
        if lines is not None:
            for line in lines:
                for x1, y1, x2, y2 in line:
                    cv2.line(line_image, (int(x1),int(y1)), (int(x2),int(y2)), (255,0,255), 5)
        return line_image
    except Exception as e:
        print(e)

def region_of_interest(image):
   height = image.shape[0]
   horizon = np.array([[(0, height), (320, height) , (320, height-50), (185, 120),(135, 120), (0, height-50)]], np.int32)
   mask = np.zeros_like(image)
   cv2.fillPoly(mask, horizon, 255)
   masked_image = cv2.bitwise_and(image,mask)
   cv2.imwrite('./resultROI.png',masked_image)
   return masked_image

# cv2.imwrite("orig.png", im)
# image = cv2.imread('orig.png')
# lane_image = np.copy(image)
# canny_image = canny(lane_image)
# cropped_image = region_of_interest(canny_image)
# lines = cv2.HoughLinesP(cropped_image, 4, np.pi/180, 200, np.array([]), minLineLength=45, maxLineGap=20)
# average_lines = average_slope_intercept(lane_image, lines)
# line_image = display_lines(lane_image, average_lines)
# combo_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1)
# # cv2.imwrite("hsv.png", hsv)
# # cv2.imwrite('resultGRAY.png',gray)
# cv2.imwrite('resultCANNY.png',canny_image)
# cv2.imwrite('resultMASK.png',combo_image)

#

cap = cv2.VideoCapture(-1)
cap.set(cv2.CAP_PROP_FPS, 15)
fps = int(cap.get(5))
print("fps:", fps)
cap.set(3,320)
cap.set(4,240)
while(cap.isOpened()):
    start_time = time.time()
    _, frame = cap.read()
    canny_image = canny(frame)
    cropped_image = region_of_interest(canny_image)
    lines = cv2.HoughLinesP(cropped_image, 4, np.pi/180, 70, np.array([]), minLineLength=15, maxLineGap=4)
    average_lines = average_slope_intercept(frame, lines)
    try:
        line_image = display_lines(frame, average_lines)
        combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
    except:
        print("can't draw lanes")
    print("--- %s seconds ---" % (time.time() - start_time))
    cv2.imwrite('./result.png',combo_image)


import discord
import os
import asyncio
import time
import json
import requests

from discord.ext.commands import Bot
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
Client = discord.Client()
client = commands.Bot(command_prefix="!")

async def background_loop():
    await client.wait_until_ready()
    while not client.is_closed:
        try:
            # Get all pairs from CryptoBridge exchange
            tok_response = requests.get(url='https://api.crypto-bridge.org/api/v1/ticker')
            tok_response.close()
            tok_data = tok_response.json()

            for line in tok_data:
                if line["id"] == os.getenv("BOTPAIR"):
                    print(line)
                    lst = line["last"]
                    vol = line["volume"]
                    ask = line["ask"]

            # Get fiat value versus BTC
            fiat_response = requests.get(url='https://blockchain.info/it/ticker')
            fiat_response.close()
            fiat_data = fiat_response.json()

            # Get the last price for selected fiat and calculate the value of the token in fiat.
            btcfia = fiat_data[os.getenv("BOTFIAT")]["last"]
            tokfia = round(float(lst) * float(btcfia), 2)

            # Calculate the token volume
            tokvol = round(float(vol) / float(ask), 3)

            # Grab the first half of the pair
            toksym = os.getenv("BOTPAIR").split('_', 1)[0]

            playing = []
            playing.append('฿ ' + str(lst) + ' BTC')
            playing.append('$ ' + str(tokfia) + ' ' + os.getenv("BOTFIAT"))
            playing.append('฿ ' + str(vol) + ' VOL 24h')
            playing.append(toksym + ' ' + str(tokvol) + ' VOL 24h')

            # This loop is there to space out the request to the API for an avarage of 5 minutes.
            for time in range(5):
                print(time)
                for play in playing:
                  print(play)
                  # The magical update of the playing bot status happens here!
                  await client.change_presence(game=discord.Game(name=play))
                  # This is the time the bot will sleep between playing statuses
                  await asyncio.sleep(int(os.getenv("BOTSLEEP")))
        except:
            pass

client.loop.create_task(background_loop())
client.run(os.getenv("YOURDISCORDTOKEN"))


import argparse
import os
import torch
import numpy as np
import utils
import helps_pre as pre
import optuna
from optuna.samplers import TPESampler
from optuna.pruners import MedianPruner
import yaml
from easydict import EasyDict as edict

parser = argparse.ArgumentParser()
parser.add_argument(
    '--edl', type=int, default=0,
    help='0: no edl; 1: edl without kl; 2: edl with kl (annealing); \
        3: edl with kl (trade-off)')
parser.add_argument('--tcn', action='store_true', default=False,
                    help='to use tcn if it activates, cnn otherwise')

args = parser.parse_args()

EDL_USED = args.edl
TCN_USED = args.tcn
DEVICE = pre.try_gpu()
GLOBAL_BEST_LOSS = np.inf

def run_training(fold, cfg):
    train_trial_list = list(set(range(1,cfg.DATA_CONFIG.trial_n+1))-set(cfg.CV.valid_trial_list[fold]))

    # load_data
    train_loader = pre.load_data(cfg.DATA_PATH, cfg.DATA_CONFIG.sb_n, cfg.DATA_CONFIG.day_list, cfg.DATA_CONFIG.time_list, train_trial_list, tcn_used=TCN_USED, batch_size=cfg.HP.batch_size, shuffle=cfg.DATA_LOADER.shuffle, drop_last=cfg.DATA_LOADER.drop_last, num_workers=cfg.DATA_LOADER.num_workers, pin_memory=cfg.DATA_LOADER.pin_memory)

    valid_loader = pre.load_data(cfg.DATA_PATH, cfg.DATA_CONFIG.sb_n, cfg.DATA_CONFIG.day_list, cfg.DATA_CONFIG.time_list, cfg.CV.valid_trial_list[fold], tcn_used=TCN_USED, batch_size=cfg.HP.batch_size, shuffle=cfg.DATA_LOADER.shuffle, drop_last=cfg.DATA_LOADER.drop_last, num_workers=cfg.DATA_LOADER.num_workers, pin_memory=cfg.DATA_LOADER.pin_memory)

    trainloaders = {
        "train": train_loader,
        "val": valid_loader,
    }

    n_class = len(cfg.CLASS_NAMES)
    # Load Model
    if TCN_USED:
        #model = utils.TCN(input_size=cfg.DATA_CONFIG.channel_n, output_size=n_class, num_channels=cfg.HP.tcn_channels, kernel_size=cfg.HP.kernel_size, dropout=cfg.HP.dropout_rate)
        model = utils.TCN(input_size=cfg.DATA_CONFIG.channel_n, output_size=n_class, num_channels=cfg.HP.layer_n*[cfg.DATA_CONFIG.channel_n], kernel_size=cfg.HP.kernel_size, dropout=cfg.HP.dropout_rate)
    else:
        model = utils.Model(number_of_class=n_class, dropout=cfg.HP.dropout_rate)
    model.to(DEVICE)
    optimizer = getattr(
        torch.optim,cfg.HP.optimizer)(model.parameters(), lr=cfg.HP.lr)

    eng = utils.EngineTrain(model, optimizer, device=DEVICE)

    loss_params = {'edl_used': EDL_USED, 'device': DEVICE}
    if EDL_USED != 0:
        loss_params['class_n'] = n_class
        for item in cfg.HP_SEARCH[f'EDL{EDL_USED}']:
            loss_params[item]=cfg.HP[item]

    best_loss = np.inf
    early_stopping_iter = cfg.TRAINING.early_stopping_iter

    for epoch in range(1, cfg.TRAINING.epochs + 1):
        # if 'annealing_step' in loss_params:
        if EDL_USED == 2:
            loss_params['epoch_num'] = epoch
        train_losses, acc = eng.train(trainloaders, loss_params)
        train_loss = train_losses['train']
        valid_loss = train_losses['val']
        train_acc = acc['train']
        valid_acc = acc['val']
        '''
        print(
            f"epoch:{epoch}, "
            f"train_loss: {train_loss}, "
            f"valid_loss: {valid_loss}. "
            f"train_acc: {train_acc}, "
            f"valid_acc: {valid_acc}. "
        )
        '''
        if valid_loss < best_loss:
            best_loss = valid_loss
            early_stopping_counter = 0
            '''
            torch.save({
                'epoch': epoch,
                'model_state_dict': model.state_dict(),
                'optimizer': params['optimizer'],
                'optimizer_state_dict': optimizer.state_dict(),
                'train_loss': train_loss,
                'valid_loss': valid_loss # best_loss
                }, params['saved_model'])
            '''
        else:
            early_stopping_counter += 1
            early_stopping_counter += 1
        if early_stopping_counter > early_stopping_iter:
            break

    global GLOBAL_BEST_LOSS
    if best_loss < GLOBAL_BEST_LOSS:
        GLOBAL_BEST_LOSS = best_loss
        torch.save({
            'model_state_dict': model.state_dict(),
            'train_loss': train_loss,
            'valid_loss': valid_loss, # best_loss
            'train_acc:': train_acc,
            'valid_acc': valid_acc
            }, cfg.model_path+f'/{cfg.TRAINING.model_name}_sb{cfg.DATA_CONFIG.sb_n}.pt') # modify it later
    return best_loss

def objective(trial, cfg):
    # cfg.HP={} # hyperparams
    # Update the params for tuning with cross validation
    #with open("hpo_search.yaml", 'r') as f:
    #    hyo_search = yaml.load(f, Loader=yaml.SafeLoader)
    #trial.set_user_attr("batch_size", 256)
    #if TCN_USED:
    #    trial.set_user_attr("kernel_size", 5)
    for key, item in cfg.HP_SEARCH[f'EDL{EDL_USED}'].items():
        cfg.HP[key] =  eval(item) # Example of an item: trial.suggest_int("kernel_size", 2, 6)
    if TCN_USED:
        cfg.HP['layer_n'] = eval(cfg.HP_SEARCH['TCN'].layer_n)
        cfg.HP['kernel_size'] = cfg.HP_SEARCH['TCN'].kernel_list[cfg.HP['layer_n']-3]
        #for key, item in cfg.HP_SEARCH['TCN'].items():
        #    if key == 'tcn_channels':
        #        cfg.HP[key] = item
        #    else:
        #        cfg.HP[key] =  eval(item)

        #tcn_channels = [int(cfg.HP['init_channel'])]
        #for i in range(cfg.HP['tcn_layer_n']-1):
        #    tcn_channels.append(tcn_channels[-1]*2)
        #cfg.HP['TCN_CHANNELS'] = [32, 64, 128, 256, 512]
    #print(cfg.HP)
    #cfg.HP['batch_size'] = 256
    #if TCN_USED:
    #    cfg.HP['kernel_size'] = 5

    if cfg.CV.valid_trial_select is None:
        all_losses = []
        for fold_n in range(len(cfg.CV.valid_trial_list)):
            temp_loss = run_training(fold_n, cfg)
            intermediate_value = temp_loss
            trial.report(intermediate_value, fold_n)
            if trial.should_prune():
                raise optuna.TrialPruned()

            all_losses.append(temp_loss)
        return np.mean(all_losses)
    else:
        temp_loss = run_training(cfg.CV.valid_trial_select, cfg)
        return temp_loss

def cv_hyperparam_study():

    # Load config file
    with open("hpo_search.yaml", 'r') as f:
        cfg = edict(yaml.load(f, Loader=yaml.SafeLoader))

    sb_n=cfg.DATA_CONFIG.sb_n
    # Check study path
    study_dir = f'etcn{EDL_USED}' if TCN_USED else f'ecnn{EDL_USED}'
    study_path = os.getcwd() + cfg.STUDY_PATH + study_dir
    if not os.path.exists(study_path):
        os.makedirs(study_path)

    # Check model saved path
    cfg.model_path = os.getcwd() + cfg.MODEL_PATH + study_dir
    if not os.path.exists(cfg.model_path):
        os.makedirs(cfg.model_path)

    # Create Optuna Study
    sampler = eval(cfg.HPO_STUDY.sampler)
    study = optuna.create_study(
        direction=cfg.HPO_STUDY.direction,  # maximaze or minimaze our objective
        sampler=sampler,  # parametrs sampling strategy
        pruner=eval(cfg.HPO_STUDY.pruner),
        study_name=study_dir+f'_sb{sb_n}',
        storage=f"sqlite:///study/{study_dir}/sb{sb_n}.db",
        load_if_exists=True
    )
    study.optimize(lambda trial: objective(trial, cfg), n_trials=cfg.HPO_STUDY.trial_n)

    print("Number of finished trials: ", len(study.trials))
    print("Best trial:")
    trial = study.best_trial
    print("  Value: ", trial.value)
    study_results = [{'best_loss': trial.value}, trial.params]
    #if TCN_USED:
    #    study_results[1]['tcn_channels'] = cfg.HP_SEARCH.TCN.tcn_channels

    # Write (Save) HP to a yaml file
    with open(f'{study_path}/sb_{cfg.DATA_CONFIG.sb_n}', 'w') as f:
        yaml.dump(study_results, f)

        for key, value in trial.params.items():
            print("    {}: {}".format(key, value))
    return

if __name__ == "__main__":

    cv_hyperparam_study()

    os.system('shutdown')


from flask import Flask
from flask_cors import CORS, cross_origin
from flask import request, jsonify
import nltk
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import operator
from nltk.tokenize import word_tokenize
import json
import os
import time

app = Flask(__name__)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

# load data

#======================data cranfield for VSM============================
tfidf_file = open("./VSM/tfidf_cranfield.json", "r")
tfidf_cran = json.load(tfidf_file)

tokens_file = open("./VSM/tokens_cranfield.json", "r")
tokens_cran = json.load(tokens_file)

idfDict_file = open("./VSM/tfidf_cranfield.json", "r")
idfDict_cran = json.load(idfDict_file)

#======================data corpus for VSM===============================
tfidf_file = open("./VSM/tfidf_corpus.json", "r")
tfidf_corpus = json.load(tfidf_file)

tokens_file = open("./VSM/tokens_corpus.json", "r")
tokens_corpus = json.load(tokens_file)

idfDict_file = open("./VSM/tfidf_corpus.json", "r")
idfDict_corpus = json.load(idfDict_file)

#========================data cranfield for BIM==========================
docToken_file = open("./BIM/docToken_cranfield.json", "r")
docToken_cranfield = json.load(docToken_file)

#========================data corpus for BIM=============================
docToken_file = open("./BIM/docToken_corpus.json", "r")
docToken_corpus = json.load(docToken_file)

path_docs_cranfield = '../../cranfield/preprocessed/docs'
path_docs_corpus = '../../nfcorpus/preprocessed/docs'

listFileName_cranfield = os.listdir(path_docs_cranfield)
listFileName_cranfield = [int(fileName.split('.')[0]) for fileName in listFileName_cranfield]
listFileName_corpus = os.listdir(path_docs_corpus)
listFileName_corpus = [int(fileName.split('.')[0]) for fileName in listFileName_corpus]

def tokenize_query(query):
    tokens = nltk.word_tokenize(query)
    tokens=[x for x in set(tokens)]
    tokens=sorted(tokens)
    return tokens
def rank_BIM(query, docToken, listFileName):
    log_odd = dict.fromkeys([x for x in listFileName],0)
    query_pre = tokenize_query(query)
    for term in query_pre:
        for x in listFileName:
            try:
                log_odd[x] += docToken[str(x)][term]
            except:
                None
    log_odd = dict( sorted(log_odd.items(), key=operator.itemgetter(1),reverse=True))
    log_odd = [x for x,y in log_odd.items() if y!=0]
    return log_odd[0:10], len(log_odd)

def rank_VSM(query, tokens, idfDict, tfidf, listFileName):
    qr=nltk.word_tokenize(query)
    qr=[x for x in set(qr)]
    qrV = dict.fromkeys(tokens_cran,0)
    for word in qr:
        if word in tokens:
            qrV[word] += 1
    for words in qrV:
        try:
            qrV[words]=qrV[words]*idfDict[word]
        except KeyError:
            None
    res={}
    temp=0
    vec1=np.array([[x for x in (qrV.values())]])
    for x in listFileName:
        vec2=np.array([list(tfidf[str(x)].values())])
        if cosine_similarity(vec1,vec2) > 0:
            temp=cosine_similarity(vec1,vec2)[0][0]
            res[x]=temp
    result={}
    result = sorted(res.items(), key=operator.itemgetter(1), reverse=True)
    result = [x for x in (result)]
    if result == []:
        return [], 0
    rank=[]
    for i in range(10):
        rank.append(result[i][0])

    return rank, len(result)

@app.route('/search', methods=['POST'])
@cross_origin(origin='*')
def process():
    query = request.form.get('query')
    data = request.form.get('data')
    tool = request.form.get('tool')
    rank_ = []
    results = []
    nums_result = 0
    start_time = time.time()
    if query == '':
        rank_.append('Hãy nhập từ khóa tìm kiếm')
    else:
        if(data == 'cranfield'):
            if (tool == 'VSM'):
                rank_, nums_result = rank_VSM(query, tokens_cran, idfDict_cran, tfidf_cran, listFileName_cranfield)
            else: rank_, nums_result = rank_BIM(query, docToken_cranfield, listFileName_cranfield)
            if (rank_ == []):
                rank_.append('Không tìm thấy kết quả nào cho #'+query)
            else:
                for file in rank_:
                    f_name = path_docs_cranfield + '/' + str(file) + '.txt'
                    f = open(f_name).read()
                    results.append(f)
        else:
            if(tool == 'VSM'):
                rank_, nums_result = rank_VSM(query, tokens_corpus, idfDict_corpus, tfidf_corpus, listFileName_corpus)
            else: rank_, nums_result = rank_BIM(query, docToken_corpus, listFileName_corpus)

            if (rank_ == []):
                rank_.append('Không tìm thấy kết quả nào cho #'+query)
            else:
                for file in rank_:
                    f_name = path_docs_corpus + '/' + str(file) + '.txt'
                    f = open(f_name).read()
                    results.append(f)

    end_time = time.time()
    print('time: ',end_time-start_time)
    res = [rank_, results]
    print(nums_result)
    return {
        'resuslt' : res,
        'num_res' : nums_result,
        'time' : end_time-start_time
    }

@app.route('/', methods=['GET'])
@cross_origin(origin='*')
def index():
  return "hello"

if __name__ == '__main__':
  app.run()


import io
import json
from glob import glob
from os import path

from pytest import mark

from sls import App
from sls.services.consthub import ConstServiceHub

test_dir = path.dirname(path.realpath(__file__))
# make the test_file paths relative, s.t. test paths are nice to read
test_files = list(map(lambda e: path.relpath(e, test_dir),
                  glob(path.join(test_dir, '**', '*.story'), recursive=True)))

script_dir = path.dirname(path.realpath(__file__))
fixture_dir = path.join(script_dir, '..', 'fixtures', 'hub')
fixture_file = path.join(fixture_dir, 'hub.fixed.json')

hub = ConstServiceHub.from_json(fixture_file)

# compile a story and compare its completion with the expected tree
def run_test_completion(uri, source, expected, patch):
    assert App(hub=hub).complete(uri=uri, text=source) == expected

# load a story from the file system and load its expected result file (.json)
def run_test(story_path, patch):
    story_string = None
    with io.open(story_path, 'r') as f:
        story_string = f.read()

    expected_path = path.splitext(story_path)[0]
    assert path.isfile(expected_path + '.json'), (
          f'Path: `{expected_path}.json` does not exist.')

    expected_completion = None
    with io.open(expected_path + '.json', 'r') as f:
        expected_completion = f.read()

    # deserialize the expected completion
    expected = json.loads(expected_completion)
    return run_test_completion(story_path, story_string, expected, patch)

@mark.parametrize('test_file', test_files)
def test_story(test_file, patch):
    test_file = path.join(test_dir, test_file)
    run_test(test_file, patch)


import argparse
import logging
from os.path import isfile, basename

parser = argparse.ArgumentParser(
    formatter_class = argparse.RawDescriptionHelpFormatter,
    description = "sumup 1 hist in root files",
    epilog = """Example:\npython sumup_histo.py histoname gstore_outdirs/v28/SingleMuon/Ntupler_v28_Data13TeV_SingleMuon2016*/1*/*/*root"""
    )

parser.add_argument('histo_name', type=str, help='full name of TH from root of the files, like ntupler/weight_counter')

parser.add_argument("--debug",  action='store_true', help="DEBUG level of logging")
parser.add_argument("--output", type=str, default="output.root", help="filename for output")

parser.add_argument('input_files', nargs='+', help="""the files to sum up, passed by shell, as:

/gstore/t3cms/store/user/otoldaie/v19/TT_TuneCUETP8M2T4_13TeV-powheg-pythia8/Ntupler_v19_MC2016_Summer16_TTJets_powheg/180226_022336/0000/MC2016_Summer16_TTJets_powheg_*.root""")

args = parser.parse_args()

if args.debug:
    logging.basicConfig(level=logging.DEBUG)
else:
    logging.basicConfig(level=logging.INFO)

logging.info("import ROOT")

import ROOT
from ROOT import TH1D, TFile, TTree, gROOT
from ROOT import kGreen, kYellow, kOrange, kViolet, kAzure, kWhite, kGray, kRed, kCyan, TColor
from ROOT import TLegend
#from ROOT import THStack

logging.info("done")

from plotting_root import rgb

gROOT.SetBatch()

input_files = args.input_files

out_histo = None

for filename in input_files:
    if not isfile(filename):
        logging.info("missing: " + filename)
        continue

    logging.debug(filename)

    tfile  = TFile(filename)
    thisto = tfile.Get(args.histo_name)

    if not out_histo:
        out_histo = thisto.Clone()
        out_histo.Clone()
        out_histo.SetDirectory(0)
    else:
        out_histo.Add(thisto)

    tfile.Close()

fout = TFile(args.output, "RECREATE")
fout.Write()

out_histo.Write()

fout.Write()
fout.Close()


import rpyc
import time
import janus
import platform
import argparse
import threading
from rpyc.utils.server import ThreadedServer
from rpyc.utils.helpers import classpartial

from helper import *

# Configure service host address and port here
SERVICE_HOST = 'localhost'
SERVICE_PORT = 2100

logger = logging.getLogger('main')

class TriggerService(rpyc.Service):
    """RPC Trigger Service"""

    def __init__(self, send_jq):
        self._send_jq = send_jq

    def exposed_put(self, **kwargs):
        """Exposed put method

        Args:
            kwargs args sent from sender program
        """
        if kwargs.get('stop', None):
            self._send_jq.sync_q.put(None)
        else:
            self._send_jq.sync_q.put('{} {}'.format(kwargs['state'], kwargs['machine']))

def loop_in_thread(loop, args, que):
    """Run event loop in a separate thread.

    Args:
        loop: event loop
        args (NameSpace): command line arguments
        que (Queue): Queue to hold asynchronous events
    """
    asyncio.set_event_loop(loop)
    loop.run_until_complete(send(loop, args, que))
    logger.info('Exiting loop in thread.')

async def send(loop, args, jq):
    """Trigger service to send events to the notification service

    Args:
        loop: asyncio event loop
        args (NameSpace): command line arguments
        jq (Queue): Queue to put the events

    Raises:
        ConnectionRefusedError: when client cannot connect to server
        CancelledError - when cou-routine task cancels
        IncompleteReadError, ConnectionResetError - When trigger disconnects to the
         notification service prematurely
    """
    # Connecting to Server
    for i in range(1, 6):
        try:
            logger.debug('Reaching server(attempt=%s)...', str(i))
            reader, writer = await asyncio.open_connection(host=args.host,
                                                           port=args.port,
                                                           loop=loop)
            if reader and writer:
                logger.debug('%s @%s', args.name,
                             writer.transport.get_extra_info('sockname'))
                break
        except ConnectionRefusedError:
            time.sleep(10)
            logger.debug('Attempt %s failed. Retrying..', str(i))
    else:
        logger.info('Could not reach server.')
        # Send error notification only once on connection error
        show('Error', 'Connection to Server failed.')
        loop.stop()
        return

    try:
        # Handshake between server and trigger
        logger.info('Sending hello message to server.')
        await send_msg(writer, 'Trigger hello')

        logger.info('Waiting for hello message from server')
        message = await read_msg(reader)

        if message and message.lower() == 'hello':
            logger.info('Received reply from server. Sending \'trigger\' as subscription')
            await send_msg(writer, 'trigger')

        logger.debug('Sending events...')
        while True:
            _item = await jq.async_q.get()

            if not _item:
                break

            state, machine = _item.split(' ')
            logger.info('Sending[%s][%s]', state, machine)
            await send_msg(writer, '{} {}'.format(state, machine))
    except asyncio.CancelledError:
        logger.debug('Stopping trigger')
        writer.write_eof()
    except (asyncio.streams.IncompleteReadError, ConnectionResetError):
        logger.debug('Disconnected to Server')
    finally:
        logger.info('Terminating trigger')
        writer.close()

if __name__ == '__main__':
    cli = argparse.ArgumentParser(description='''Trigger application which sends random state and machine name to
                                                 publisher''')
    cli.add_argument('--host', help='Host IP of the server', default=config.server_host)
    cli.add_argument('--port', type=int, help='Port in which server is listening to',
                     default=config.server_port)
    cli.add_argument('--name', help='Name of the trigger', default='mysql-trigger')
    cli.add_argument('-v', '--verbose', action="store_true", help='Enable Verbose mode')
    _args = cli.parse_args()

    # Client name should be lesser than 15 characters
    if len(_args.name) > 15:
        _args.name = _args.name[:16]

    # Setup logging
    setup_logging(log_name=os.path.abspath(os.path.join(
        os.path.dirname(__file__), '..', 'log', 'trigger.log')),
        default_level=logging.DEBUG if _args.verbose else logging.INFO)
    logger.info('%s-%s-%s', platform.system(), platform.version(), platform.machine())
    logger.info('Trigger pid: %s', os.getpid())
    logger.info(_args)

    sender_loop = asyncio.get_event_loop()
    queue = janus.Queue()

    # Runs event loop on a thread
    a_thread = threading.Thread(target=loop_in_thread, args=(sender_loop, _args, queue))
    a_thread.start()

    service = classpartial(TriggerService, send_jq=queue)
    ThreadedServer(service, SERVICE_HOST, port=SERVICE_PORT).start()


from django.db.models import Q

from rest_framework import exceptions as drf_exceptions, viewsets

from .models import *
from .serializers import *

class BookViewSet(viewsets.ModelViewSet):
    """ This is an API endpoint that allows books to be viewed. """

    lookup_field = 'gutenberg_id'

    queryset = Book.objects.exclude(download_count__isnull=True)
    queryset = queryset.exclude(title__isnull=True)

    serializer_class = BookSerializer

    def get_queryset(self):
        queryset = self.queryset

        sort = self.request.GET.get('sort')
        if sort == 'ascending':
            queryset = queryset.order_by('id')
        elif sort == 'descending':
            queryset = queryset.order_by('-id')
        else:
            queryset = queryset.order_by('-download_count')

        author_year_end = self.request.GET.get('author_year_end')
        try:
            author_year_end = int(author_year_end)
        except:
            author_year_end = None
        if author_year_end is not None:
            queryset = queryset.filter(
                Q(authors__birth_year__lte=author_year_end) |
                Q(authors__death_year__lte=author_year_end)
            )

        author_year_start = self.request.GET.get('author_year_start')
        try:
            author_year_start = int(author_year_start)
        except:
            author_year_start = None
        if author_year_start is not None:
            queryset = queryset.filter(
                Q(authors__birth_year__gte=author_year_start) |
                Q(authors__death_year__gte=author_year_start)
            )

        copyright_parameter = self.request.GET.get('copyright')
        if copyright_parameter is not None:
            copyright_strings = copyright_parameter.split(',')
            copyright_values = set()
            for copyright_string in copyright_strings:
                if copyright_string == 'true':
                    copyright_values.add(True)
                elif copyright_string == 'false':
                    copyright_values.add(False)
                elif copyright_string == 'null':
                    copyright_values.add(None)
            for value in [True, False, None]:
                if value not in copyright_values:
                    queryset = queryset.exclude(copyright=value)

        id_string = self.request.GET.get('ids')
        if id_string is not None:
            ids = id_string.split(',')

            try:
                ids = [int(id) for id in ids]
            except ValueError:
                pass
            else:
                queryset = queryset.filter(gutenberg_id__in=ids)

        language_string = self.request.GET.get('languages')
        if language_string is not None:
            language_codes = [code.lower() for code in language_string.split(',')]
            queryset = queryset.filter(languages__code__in=language_codes)

        mime_type = self.request.GET.get('mime_type')
        if mime_type is not None:
            queryset = queryset.filter(format__mime_type__startswith=mime_type)

        search_string = self.request.GET.get('search')
        if search_string is not None:
            search_terms = search_string.split(' ')
            for term in search_terms[:32]:
                queryset = queryset.filter(
                    Q(authors__name__icontains=term) | Q(title__icontains=term)
                )

        topic = self.request.GET.get('topic')
        if topic is not None:
            queryset = queryset.filter(
                Q(bookshelves__name__icontains=topic) | Q(subjects__name__icontains=topic)
            )

        return queryset.distinct()


import fluent.syntax.ast as FTL

from .errors import SkipTransform
from .util import get_message, get_transform

def merge_resource(ctx, reference, current, transforms, in_changeset):
    """Transform legacy translations into FTL.

    Use the `reference` FTL AST as a template.  For each en-US string in the
    reference, first check for an existing translation in the current FTL
    `localization` and use it if it's present; then if the string has
    a transform defined in the migration specification and if it's in the
    currently processed changeset, evaluate the transform.
    """

    def merge_body(body):
        return [
            entry
            for entry in map(merge_entry, body)
            if entry is not None
        ]

    def merge_entry(entry):
        # All standalone comments will be merged.
        if isinstance(entry, FTL.BaseComment):
            return entry

        # Ignore Junk
        if isinstance(entry, FTL.Junk):
            return None

        ident = entry.id.name

        # If the message is present in the existing localization, we add it to
        # the resulting resource.  This ensures consecutive merges don't remove
        # translations but rather create supersets of them.
        existing = get_message(current.body, ident)
        if existing is not None:
            return existing

        transform = get_transform(transforms, ident)

        # Make sure this message is supposed to be migrated as part of the
        # current changeset.
        if transform is not None and in_changeset(ident):
            if transform.comment is None:
                transform.comment = entry.comment
            try:
                return ctx.evaluate(transform)
            except SkipTransform:
                return None

    body = merge_body(reference.body)
    return FTL.Resource(body)


import numpy as np
import matplotlib.pyplot as plt

def loadDataSet(fileName):
    dataArr, labelArr = [], []
    with open(fileName) as fr:
        numFeat = len(fr.readline().split('\t')) - 1
        fr.seek(0,0) #reset the handle
        for line in fr.readlines():
            lineArr = []
            curLine = line.strip().split('\t')
            for i in range(numFeat):
                lineArr.append(float(curLine[i]))
            dataArr.append(lineArr)
            labelArr.append(float(curLine[-1]))
    return dataArr, labelArr

#wHat = (X^T*X)^-1*X^T*y
def standRegres(xArr, yArr):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    xTx = xMat.T * xMat
    if np.linalg.det(xTx) == 0.0:
        print("this matrix is singular, cannot do inverse")
        return
    ws = xTx.I * xMat.T * yMat
    return ws

def standRegresFigure(xArr, yArr, ws):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xMat[:,1].flatten().A[0], yMat.T[:,0].flatten().A[0]) #Masked arrays must be 1-Dim
    xCopy = xMat.copy()
    xCopy.sort(axis=0)
    yHat = xCopy * ws
    ax.plot(xCopy[:,1], yHat)
    yHatO = xMat * ws
    print("the Correlation Coefficient is: ", np.corrcoef(yHatO.T, yMat)) #
    plt.show()

#Locally Weighted Linear Regression
#wHat = (X^T*W*X)^-1*X^T*W*y
def locWeightedRegres(testPoint, xArr, yArr, k=1.0):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    m = np.shape(xMat)[0]
    weights = np.mat(np.eye((m)))
    for j in range(m):
        diffMat = testPoint - xMat[j,:]
        weights[j,j] = np.exp(diffMat * diffMat.T/(-2 * k**2))
    xTwx = xMat.T * weights * xMat
    if np.linalg.det(xTwx) == 0.0:
        print("this matrix is singular, cannot do inverse")
        return
    ws = xTwx.I * xMat.T * weights * yMat
    return testPoint * ws

def lwlrTest(testArr, xArr, yArr, k=1.0):
    m = np.shape(testArr)[0]
    yHat = np.zeros((m))
    for i in range(m):
        yHat[i] = locWeightedRegres(testArr[i], xArr, yArr, k)
    return yHat

def lwlrFigure(testArr, xArr, yArr, k=1.0):
    xMat = np.mat(xArr)
    yHat = lwlrTest(testArr, xArr, yArr, k)
    srtInd = xMat[:,1].argsort(axis=0)
    xSort = xMat[srtInd][:,0,:]
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(xSort[:,1], yHat[srtInd])
    ax.scatter(xMat[:,1].flatten().A[0], yArr, c='r')
    plt.show()

def rssError(yArr, yHatArr):
    return ((yArr - yHatArr)**2).sum()

#wHat = (X^T*X+λI)^-1*X^T*y
def ridgeRegres(xMat, yMat, lam=0.2):
    xTx = xMat.T * xMat
    denom = xTx + np.eye(np.shape(xMat)[1])*lam
    if np.linalg.det(denom) == 0.0:
        print("this matrix is singular, cannot do inverse")
        return
    ws = denom.I * xMat.T * yMat
    return ws

def regularize(xMat):
    xVar = np.var(xMat, axis=0) #variance of x
    xMean = np.mean(xMat, axis=0)
    return (xMat - xMean)/xVar

def ridgeTest(xArr, yArr):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    yMean = np.mean(yMat, axis=0) #mean value of y
    yMat = yMat - yMean #regularize
    xMat = regularize(xMat) #regularize
    numTestPts = 30
    wMat = np.mat(np.zeros((numTestPts, np.shape(xMat)[1])))
    for i in range(numTestPts):
        ws = ridgeRegres(xMat, yMat, np.exp(i-10))
        wMat[i,:] = ws.T
    return wMat

def stageWise(xArr, yArr, eps=0.01, numIt=100):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    yMean = np.mean(yMat, axis=0)
    yMat = yMat - yMean
    xMat = regularize(xMat)
    m,n = np.shape(xMat)
    returnMat = np.zeros((numIt,n))
    ws = np.zeros((n,1))
    wsTest = ws.copy()
    wsMax = ws.copy()
    for i in range(numIt):
        print(ws.T)
        lowestError = np.inf
        for j in range(n):
            for sign in [-1,1]:
                wsTest = ws.copy()
                wsTest[j] += eps * sign
                yTest = xMat * wsTest
                rssErr = rssError(yMat.A, yTest.A)
                if rssErr < lowestError:
                    lowestError = rssErr
                    wsMax = wsTest
        ws = wsMax.copy()
        returnMat[i,:] = ws.T
    return returnMat


import pytest
import asyncio

from db import Store

class TestStore():
    def setup(self):
        self.test_string = {'name': 'UserForm', 'phone': 'phone', 'e_mail': 'mail'}
        self.store = Store(host='172.23.0.1')

    def test_get_list_from_store(self):
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.store.get_documents_from_db())
        assert self.store.list_records[0]['name'] == self.test_string['name']
        assert self.store.list_records[0]['phone'] == self.test_string['phone']
        assert self.store.list_records[0]['e_mail'] == self.test_string['e_mail']


from django.contrib import admin
from chores.models import Chores, Category, History

class BlogAdmin(admin.ModelAdmin):
    exclude = ['posted']
    prepopulated_fields = {'slug': ('title',)}

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

class HistoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {}

admin.site.register(Chores, BlogAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(History, HistoryAdmin)


'''
Discord image history viewer
Attempts to guessimate unique ID keyspace in image URIs that are publically available
Author: @jcobu
'''
from sys import exit
import requests, random
foundList = []

class urlTemplate:

    def __init__(self, uuid, uuid2, fname):
        self.uuid = uuid
        self.uuid2 = uuid2
        self.fname = fname

    def combinedURL(self):
        urlPrefix = 'https://media.discordapp.net/attachments'
        urlComplete = (f'{urlPrefix}/{self.uuid}/{self.uuid2}/{self.fname}')
        print(urlComplete)
        return urlComplete

# Temporary array keeping track of valid urls
def ifFound(code, url):
    if code == 200:
        for a in foundList:
            if url != a:
                foundList.append(url)

# Prints the final foundList array vertically
def printStuff():
    for i in foundList:
        print('Valid links: ' + i)

# Main menu
def menu():
    while(True):
        print('(1) for manual')
        print('(2) for automatic sifting')
        selection = input('Selection: ')

        if selection == '1':
            print('Starting manual\n')
            manualBuilder()
        elif selection == '2':
            print('Starting automatic\n')
            automatedBuilder()
        else:
            print('Exiting\n')
            exit(0)

# Picks random 3, 6 digit numbers and appends them to a single str var
def combiner():
    # Open pre-generated 6 digit list
    lines = open('numbers.txt').read().splitlines()
    list = str(random.choice(lines))
    return list
    # Closes the file
    lines.close()

# Manual search
def manualBuilder():
    print('Example: https://media.discordapp.net/attachments/<useruuid>/<secondaryuuid>/<unknown.png>')
    userUUID = input('User UUID: ')
    userFile = input('File name (if known): ')
    if userFile == '':
        userFile = 'unknown.png'

    for a in range(10):
        alist = ''
        for x in range(3):
            alist += combiner()

        # Combines the provided info
        userInput = urlTemplate(userUUID, '909926715197902868', userFile)
        resp = requests.get(userInput.combinedURL())
        print(resp.status_code)
        ifFound(resp.status_code, userInput.combinedURL())

        printStuff()

# Automated search based on wordlists
def automatedBuilder():
    for z in range(10):
        alist = ''
        blist = ''
        for x in range(3):
            alist += combiner()
            blist += combiner()

        # Combines the provided info using classes
        automatedInput = urlTemplate(alist, blist, 'unknown.png')
        resp = requests.get(automatedInput.combinedURL())
        print(resp.status_code)
        ifFound(resp.status_code, automatedInput.combinedURL())

        printStuff()

menu()


from flask import Flask, make_response, jsonify
from flask.json import jsonify
from flask_restful import Resource, Api

import unioptapi

app = Flask(__name__)
api = Api(app)

class ApiRoot(Resource):
    """
    api controller of the path [/uniopt] which retrieves the api meta information
    """
    def get(self):
        root_data = {
            "name": "uniopt-api",
            "version": unioptapi.__version__
        }
        return make_response(jsonify(root_data), 200)

class ApiVer(Resource):
    """
    api controller of the path [/uniopt/api] which retrieves the api version
    """
    def get(self):
        return make_response(jsonify(unioptapi.__version__), 200)

api.add_resource(ApiRoot, '/uniopt')
api.add_resource(ApiVer, '/uniopt/api')

if __name__ == '__main__':
    app.run(debug=True)


import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
#위에 4줄 한이유는 Atom에서 한글을 출력하지 못하기 떄문에 문제 해결하기 위해 설정한다.

#self 이해하기
class SelfTest():
    def function1():
        print("function1 called!")

    def function2(self):
        print(id(self))
        print("function2 called!")

f = SelfTest()
#print(dir(f))

f.function1() #오류 발생, TypeError: function1() takes 0 positional arguments but 1 was given
f.function2() #성공, 즉 자동으로 뭔가 넘어감...
print(id(f)) #3059900794248  ,  3059900794248 주소값이 일치한다.
#설명: 인스턴스의 주소값이 self에 담겨서 넘어간다. 즉,

#그렇다면 function1을 self없이 접근 하려면?
print(SelfTest.function1())


#!/usr/bin/env python
#_*_coding:utf-8_*_

import argparse
import numpy as np
import pandas as pd
import sys
import os
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path + '/repDNA/')
from repDNA.nac import *
from repDNA.psenac import *
from repDNA.ac import *
from Bio import SeqIO

def feature_extraction_repdna(finput, label):

	names_seq = []
	for seq_record in SeqIO.parse(finput, "fasta"):
		name = seq_record.name
		names_seq.append(name)

	rev_kmer = RevcKmer(k=3, normalize=True, upto=True)
	data_kmer = rev_kmer.make_revckmer_vec(open(finput))
	data_kmer = pd.DataFrame(data_kmer)

	psednc = PseDNC()
	data_psednc = psednc.make_psednc_vec(open(finput))
	data_psednc = pd.DataFrame(data_psednc)

	pseknc = PseKNC()
	data_pseknc = pseknc.make_pseknc_vec(open(finput))
	data_pseknc = pd.DataFrame(data_pseknc)

	sc_psednc = SCPseDNC()
	data_sc_psednc = sc_psednc.make_scpsednc_vec(open(finput), all_property=True)
	data_sc_psednc = pd.DataFrame(data_sc_psednc)

	sc_psetnc = SCPseTNC(lamada=2, w=0.05)
	data_sc_psetnc = sc_psetnc.make_scpsetnc_vec(open(finput), all_property=True)
	data_sc_psetnc = pd.DataFrame(data_sc_psetnc)

	dac = DAC(2)
	data_dac = dac.make_dac_vec(open(finput), all_property=True)
	data_dac = pd.DataFrame(data_dac)

	tac = TAC(2)
	data_tac = tac.make_tac_vec(open(finput), all_property=True)
	data_tac = pd.DataFrame(data_tac)

	tcc = TCC(2)
	data_tcc = tcc.make_tcc_vec(open(finput), all_property=True)
	data_tcc = pd.DataFrame(data_tcc)

	tacc = TACC(2)
	data_tacc = tacc.make_tacc_vec(open(finput), all_property=True)
	data_tacc = pd.DataFrame(data_tacc)

	df = pd.concat([data_kmer,
					data_psednc,
					data_pseknc,
					data_sc_psednc,
					data_sc_psetnc,
					data_dac,
					data_tac,
					data_tcc,
					data_tacc],
				   axis=1, ignore_index=False)

	df.insert(0, "nameseq", names_seq)
	df.insert(len(df.columns), "label", label)

	return df

if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument("--file", dest='file')
	parser.add_argument("--output", dest='outFile',
						help="the generated descriptor file")
	parser.add_argument("--label", dest='labelFile')
	args = parser.parse_args()
	input_file = str(args.file)
	label = str(args.labelFile)
	output_file = str(args.outFile)

	df = feature_extraction_repdna(input_file, label)
	df.to_csv(output_file, index=False, mode='a')

# Documentation: http://bioinformatics.hitsz.edu.cn/repDNA/static/download/repDNA_manual.pdf


"""Module containing helpers to operate with file objects."""
import io
import typing

import magic

class MimeTypeDetectingFileObject(io.RawIOBase):
    """File like class which tries to detect the mimetime as soon as enough data is availabe."""

    def __init__(self, orig_obj: io.RawIOBase,
                 detection_callback: typing.Callable[[str], None], max_buffer_size: int=1024):
        """Init.

        :param orig_obj: the file object to wrap
        :param detection_callback: a function pointer taking a string as argument, called if either
               the max_buffer_size has been read or the file has ended before.
        :param max_buffer_size: maximum data collected to pass to magic
        """
        super().__init__()
        self._orig_obj = orig_obj
        self.read_count = 0

        self.max_buffer_size = max_buffer_size

        # this buffer stores the bytes
        self.buffer = b''

        self.detection_callback = detection_callback
        self.called_back = False

    def read(self, count=None):
        """Pass the read to the orig_obj and tries to find out the mime-type."""
        buffer = self._orig_obj.read(count)

        if len(self.buffer) < self.max_buffer_size and not self.called_back:
            # lets add the buffer
            self.buffer += buffer

        if (len(self.buffer) >= self.max_buffer_size or len(
                buffer) != count) and not self.called_back:
            self.detection_callback(magic.from_buffer(self.buffer, mime=True))
            self.called_back = True
            self.buffer = b''
        return buffer


from tkinter import *
from PIL import Image, ImageTk
import tkinter as tk

def Registrationform():
    global reg_screen
    reg_screen = Tk()
    reg_screen.title("فرم حضور غیاب گلبرگ دنیا")
    reg_screen.iconphoto(False, PhotoImage(file='images/logos.png'))
    reg_screen.geometry("600x380")

    fram1 = tk.Frame(reg_screen, width=150, height=340, relief=SUNKEN,borderwidth=1)
    fram1.place(x=0,y=40)

    # TOP RIBBON
    Label(reg_screen, width="300", text=" سلام، روز خوش! لطفا منوی مورد نظر را انتخاب نمایید ", bg="purple", fg="white").pack(anchor='e')
    canvas = Canvas(reg_screen, width=160, height=45)
    canvas.place(x=-15,y=45)
    new_image = ImageTk.PhotoImage((Image.open("images/logo farsi eng.png")).resize((160, 40), Image.ANTIALIAS))
    canvas.create_image(10, 10, anchor=NW, image=new_image)

    fram2 = tk.Frame(reg_screen, width=450, height=340, relief=SUNKEN, borderwidth=1)
    fram2.place(x=150, y=40)
    canvas2 = Canvas(fram2, width=450, height=340)
    canvas2.place(x=0,y=0)
    new_image2 = ImageTk.PhotoImage((Image.open("images/Back1.png")).resize((450, 340), Image.ANTIALIAS))
    canvas2.create_image(1, 1, anchor=NW, image=new_image2)

    ###MENUE  hozoor ghiab
    def clickon0():
        B1.config(fg='White', bg='Purple')
        B2.config(fg='black', bg='White')
        B3.config(fg='black', bg='White')
        fram2 = tk.Frame(reg_screen, width=0, height=0)
        for widgets in fram2.winfo_children():
            widgets.destroy()
        import subpages.MenueSelectDate
        subpages.MenueSelectDate.menueSelectDate(reg_screen)
      # کلیک روی مدیریت پروفایل ها
    def clickon1():
        B1.config(fg='black', bg='White')
        B2.config(fg='White', bg='Purple')
        B3.config(fg='black', bg='White')
        fram2 = tk.Frame(reg_screen, width=0, height=0)
        for widgets in fram2.winfo_children():
            widgets.destroy()
        import subpages.MenueModidateProfile
        subpages.MenueModidateProfile.MenueModiriatProfile(reg_screen)

    # کلیک روی بررسی وضعیت کارمندان
    def clickon2():
        B1.config(fg='black', bg='White')
        B2.config(fg='black', bg='White')
        B3.config(fg='White', bg='Purple')
        fram2 = tk.Frame(reg_screen, width=0, height=0)
        for widgets in fram2.winfo_children():
            widgets.destroy()
        import subpages.MenueBarresiVaziat
        subpages.MenueBarresiVaziat.MenueVaziateKarmand(reg_screen)

    # ERROR

    B1 = Button(reg_screen, text=' لیست حضور و غیاب', command=lambda: clickon0(), width="17", bg='white',
                fg='black')
    B1.place(x=10, y=130)
    B2 = Button(reg_screen, text='مدیریت پروفایل ها', command=lambda: clickon1(), bg='white', fg='black',
                width="17")
    B2.place(x=10, y=170)
    B3 = Button(reg_screen, text='بررسی وضعیت کارمندان ', command=lambda: clickon2(), bg='white', fg='black',
                width="17")
    B3.place(x=10, y=210)

    reg_screen.mainloop()

Registrationform()


import urllib
import json
import globalData
globalData.init()

def rotoLinkForOffset(offset):
    return 'http://www.rotoworld.com/services/mobile.asmx/GetNews?articleid=' + str(offset) + '&sport=NBA&token=m1rw-xor-434s-bbjt-1'

def generateTestData(fileName, num, articleid):
    arr = []
    lastOffset = articleid
    for i in range(num / 50):
        url = rotoLinkForOffset(lastOffset)
        response = urllib.urlopen(url)
        data = json.loads(response.read())
        arr.extend(map(lambda d: d['HEADLINE'] + ': ' + d['ANALYSIS'], data))
        lastOffset = data[-1]['NEWSID'] - 1
    with open(fileName, 'w') as f:
        for a in arr:
            f.write((',' + '"' + a.replace('"', '\'') + '"').encode("ascii", "ignore").decode('UTF-8') + '\n')


# imports - general
import time

# imports - linguistic
import re
import spacy
from scispacy.abbreviation import AbbreviationDetector

nlp = spacy.load("en_core_sci_sm")
# Add the abbreviation detector to spacy pipeline
nlp.add_pipe("abbreviation_detector")

'''
Description:
    The function removes trailing spaces from a string and replaces them with a single string

Args:
    string (str): free-text string

Returns:
    string (str): clean string
'''
def removeSpaceTrailsString(s):
    return " ".join(s.split())

'''
Description:
    The function extracts acronyms using Scispacy abbreviation detector

Args:
    string (str): free-text string

Returns:
    List (list): tuple with detected abbreviation and the long form of the abbreviation
'''
def fetchAcronyms(value):

    doc = nlp(value)

    altered_tok = [tok.text for tok in doc]

    abbreviations = []

    for abrv in doc._.abbreviations:
        altered_tok[abrv.start] = str(abrv._.long_form)

        # print(f"{abrv} \t ({abrv.start}, {abrv.end}) {abrv._.long_form} \t  {value}")
        # print( " ".join(altered_tok) )

        abbreviations.append( str(abrv) )
        abbreviations.append( str(abrv._.long_form) )
        # original = str(abrv._.long_form) + ' (' + str(abrv) + ')'
        # abbreviations.append( original  )

    if abbreviations:
        return abbreviations

'''
Description:
    The function fetches POS tags for the input string

Args:
    String: free-text string

Returns:
    Dictionary (dict): returns a dictionary containing free-text string with its tokenized string, token lemma, POS tags for the tokens, finer POS tags for the token
'''
def getPOStags(value):

    doc = nlp(value)

    pos_dict = dict()

    tokens = [ token.text for token in doc ]
    lemma = [ token.lemma_ for token in doc ]
    pos = [ token.pos_ for token in doc ]
    pos_fine = [ token.tag_ for token in doc ]
    depth = [ token.dep_ for token in doc ]
    shape = [ token.shape_ for token in doc ]
    is_alpha = [ token.is_alpha for token in doc ]
    is_stop = [ token.is_stop for token in doc ]

    pos_dict['text'] = value
    pos_dict['tokens'] = tokens
    pos_dict['lemma'] = lemma
    pos_dict['pos'] = pos
    pos_dict['pos_fine'] = pos_fine

    return pos_dict

'''
TODO
Description:

Args:
    Dictionary (dict): free-text string
        value (string):
        key (string):

Returns:
    Dictionary (dict):
'''
def appendPOSSED(expanded_dictionary, values, key):

    for value_i in values:
        possed_value = getPOStags(value_i)
        if key not in expanded_dictionary:
            expanded_dictionary[key] = [possed_value]
        else:
            expanded_dictionary[key].append( possed_value )

    return expanded_dictionary


from collections import Counter

MyList = [1, 2, 3, 4, 1, 2, 3, 1, 2, 1, 5]
ListCount = Counter(MyList)

print(ListCount)

for ThisItem in ListCount.items():
   print("L'élément : ", ThisItem[0],
         " apparait : ", ThisItem[1], " fois")

print("La valeur 1 apparait {0} fois."
      .format(ListCount.get(1)))


from PyQt5.QtWidgets import QDialog, QListWidgetItem, QApplication
from PyQt5.QtCore import pyqtSlot
import os
import sys

from utils.searchdialog.searchdialog import Ui_searchdialog
from utils.config import get_default_data_file_extension

Test_workdir = ".." + os.sep + ".." + os.sep + "data"

class searchdialog(QDialog, Ui_searchdialog):

    def __init__(self, workdir, parent=None):
        super(searchdialog, self).__init__(parent)
        self._initUI()
        self.workdir = workdir
        self.lineEdit.setPlaceholderText("input keywords to search...")

    def _initUI(self):
        self.setupUi(self)

    @pyqtSlot()
    def on_searchbutton_clicked(self):
        keyword = self.lineEdit.text()  # 获取搜索词
        self.listWidget.clear()  # 清空搜索结果列表
        for root, dirs, files in os.walk(self.workdir):
            #print(root,dirs,files)
            if self.dirnamecheckbox.isChecked():
                for dir in dirs:  # 遍历子目录
                    if keyword in dir:
                        fulldir = root + os.sep + dir
                        self.add_to_list(fulldir)

            for file in files:  # 遍历目录下的文件
                fullfilepath = root + os.sep + file
                if os.path.splitext(fullfilepath)[1] == get_default_data_file_extension():  # 匹配默认数据格式
                    if keyword in file:
                        self.add_to_list(fullfilepath)

                    if self.contentcheckbox.isChecked():
                        with open(fullfilepath, encoding='utf-8') as files:
                            content = files.read()
                        if keyword in content:
                            self.add_to_list(fullfilepath)

    # 添加到列表中
    def add_to_list(self, index):
        f = QListWidgetItem(index)  # 创建一个搜索结果项
        self.listWidget.addItem(f)  # 将搜索结果添加到搜索部件中

if __name__ == '__main__':
    app = QApplication(sys.argv)
    sd = searchdialog(Test_workdir)
    sd.show()
    sys.exit(app.exec_())


# Import modules
from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import numpy as np
from torchvision import models, transforms
import matplotlib.pyplot as plt

import itertools
import torchvision.transforms.functional as functional
import torch.nn.functional as F
from collections import Counter
import random
from PIL import Image

# Define classes
# classes = ('normal', 'bacteria', 'TB', 'viral_and_COVID')
classes = ('Normal', 'LCA')

def make_weights_for_balanced_classes_customloader(images_dic, nclasses):
    count = [0] * nclasses
    for image in images_dic:
        count[images_dic[image]] += 1
    weight_per_class = [0.] * nclasses
    N = float(sum(count))
    for i in range(nclasses):
        weight_per_class[i] = N/float(count[i])
    weight = [0] * len(images_dic)
    for idx, image in enumerate(images_dic):
        weight[idx] = weight_per_class[images_dic[image]]
    return weight

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

def augmentation(image, rand_p, mode):

    # if mode == 'train':
    #     # random vertical flip
    #     # if rand_p > 0.5:
    #     #     image = functional.hflip(np.asarray(image))
    #     # else:
    #     #     pass
    #     #
    #     # rot_angle = random.randint(-5, 5)
    #     #
    #     # image = functional.rotate(img=Image.fromarray(image), angle=rot_angle)
    #     pass
    # elif mode == 'val':
    #     pass
    # elif mode == 'test':
    #     pass
    # else:
    #     print('Error: not a valid phase option.')
    pass
    return image

def matplotlib_imshow(img, one_channel=False):
    if one_channel:
        img = img.mean(dim=0)
    img = img / 2 + 0.5     # unnormalize
    npimg = np.asarray(img.cpu())
    if one_channel:
        plt.imshow(npimg, cmap="Greys")
    else:
        plt.imshow(np.transpose(npimg, (1, 2, 0)))

def images_to_probs_single(net, images):
    '''
    Generates predictions and corresponding probabilities from a trained
    network and a list of images
    '''
    output = net(images)
    # convert output probabilities to predicted class
    _, preds_tensor = torch.max(output, 1)
    preds = np.asarray(preds_tensor.cpu())
    # preds = np.squeeze(preds_tensor.cpu().numpy())
    return preds, [F.softmax(el, dim=0)[i].item() for i, el in zip(preds, output)]

def plot_classes_preds_single(net, images, labels):
    '''
    Generates matplotlib Figure using a trained network, along with images
    and labels from a batch, that shows the network's top prediction along
    with its probability, alongside the actual label, coloring this
    information based on whether the prediction was correct or not.
    Uses the "images_to_probs" function.
    '''
    preds, probs = images_to_probs_single(net, images)
    # plot the images in the batch, along with predicted and true labels
    fig = plt.figure(figsize=(12, 48))
    for idx in np.arange(4):
        ax = fig.add_subplot(1, 4, idx+1, xticks=[], yticks=[])
        matplotlib_imshow(images[idx], one_channel=True)
        ax.set_title("{0}, {1:.1f}%\n(label: {2})".format(
            classes[preds[idx]],
            probs[idx] * 100.0,
            classes[labels[idx]]),
                    color=("green" if preds[idx]==labels[idx].item() else "red"))
    return fig

def most_common_top_1(candidates):
    assert isinstance(candidates, list), 'Must be a list type'
    if len(candidates) == 0: return None
    return Counter(candidates).most_common(n=1)[0][0]


import sys
import os
import tempfile
import shutil
import argparse

from pykickstart.parser import *
from pykickstart.version import makeVersion
import yum

class KickstartPackageResolver(object):
    def __init__(self, ks_path, blacklist=None):
        self.ks_path = ks_path
        if blacklist is None:
            self.blacklist = []
        else:
            self.blacklist = blacklist

        # create a kickstart parser from the latest version
        self.ksparser = KickstartParser(makeVersion())
        self.yb = yum.YumBase()

        self._repo_problems = False

    def parse(self):
        self.ksparser.readKickstart(self.ks_path)
        selected_package_names = list(self.ksparser.handler.packages.packageList)
        selected_group_names = list(self.ksparser.handler.packages.groupList)
        excluded_package_names = list(self.ksparser.handler.packages.excludedList)

        self.setup_yum()
        self.exclude_packages(excluded_package_names + self.blacklist)

        found, missing = self._find_packages(selected_package_names)
        if missing:
            raise Exception("failed to find %s" % (' '.join(missing),))

        for p in found:
            self.yb.install(p)
        for group in selected_group_names:
            tx_members = self.yb.selectGroup(group.name)

        result,msgs = self.yb.buildTransaction()
        self._repo_problems = result != 2
        if self._repo_problems:
            sys.stderr.write("\n".join(msgs) + "\n")

    def cleanup(self):
        shutil.rmtree(self.tmpdir)

    @property
    def selected_packages(self):
        return [m.po for m in self.yb.tsInfo]

    def setup_yum(self):
        self.tmpdir = tempfile.mkdtemp(suffix=".ksparser")
        yum_conf = os.path.join(self.tmpdir, "fakyum.conf")
        yum_cache = os.path.join(self.tmpdir, "yumcache")

        # populate temporary yum tree
        open(yum_conf, "a").close()
        os.mkdir(yum_cache)
        os.mkdir(os.path.join(self.tmpdir, "etc"))
        os.mkdir(os.path.join(self.tmpdir, "etc/yum"))
        os.mkdir(os.path.join(self.tmpdir, "etc/yum/repos.d"))
        os.mkdir(os.path.join(self.tmpdir, "etc/yum.repos.d"))
        os.mkdir(os.path.join(self.tmpdir, "var"))
        os.mkdir(os.path.join(self.tmpdir, "var/lib"))
        os.mkdir(os.path.join(self.tmpdir, "var/lib/rpm"))

        self.yb.preconf.fn=yum_conf
        self.yb.preconf.root=self.tmpdir
        self.yb._getConfig()
        self.yb.repos.disableRepo('*')
        self.yb.setCacheDir(force=True, tmpdir=yum_cache)

        # add and enable yum repos defined in the kickstart
        for repodata in self.ksparser.handler.repo.repoList:
            self.yb.add_enable_repo(repodata.name, [repodata.baseurl])

        self.yb.pkgSack.excludeArchs(['x86_64', 'noarch'])

    def _find_packages(self, pkg_names, newest_only=True):
        found = []
        missing = []
        if newest_only:
            find_func = self.yb.pkgSack.returnNewestByName
        else:
            find_func = self.yb.pkgSack.returnPackages

        # some package patterns did not match, so look one at a time
        for pkg_name in pkg_names:
            try:
                matches = find_func(patterns=[pkg_name])
                found.extend(matches)
            except:
                missing.append(pkg_name)
        return (found, missing)

    def exclude_packages(self, package_names):
        excluded_packages,_ = self._find_packages(package_names, newest_only=False)
        for p in excluded_packages:
            p.exclude()

    def what_requires(self, dep_name):
        for txm in self.yb.tsInfo:
            if txm.name == dep_name:
                required_by = [p[0].name for p in txm.relatedto]
                groups = ["@%s" % (g,) for g in txm.groups]
                result = required_by + groups or ["kickstart"]
                return result
        return None

    @property
    def problems(self):
        return self._repo_problems

    @property
    def packages_nvra(self):
        nvra = [p.nvra for p in self.selected_packages]
        nvra.sort()
        return nvra

    def write_packages_info(self, out=sys.stdout):
        out.write("\n".join(self.packages_nvra) + "\n")

    @property
    def packages_meta(self):
        meta = [(p.nvra, self.package_hash(p)) for p in self.selected_packages]
        meta.sort()
        return meta

    def package_hash(self, po):
        # TODO: canonicalize package metadata and return a hash of that
        # rather than a hash of the package file
        return po.pkgId

    def write_packages_meta(self, out=sys.stdout):
        meta = self.packages_meta
        out.write("\n".join([','.join(m) for m in meta]) + "\n")

    def strip_pseudo_pkgs(self, package_names):
        return [p for p in package_names if not p.startswith("gpg-pubkey-")]

    def get_extra_packages(self, baseline_packages):
        '''
        return a list of packages that are selected in the kickstart but
        are not present in baseline_packages
        '''
        extra = list(set(self.packages_nvra) - set(self.strip_pseudo_pkgs(baseline_packages)))
        extra.sort()
        return extra

    def write_extra_packages(self, baseline_packages, out=sys.stdout):
        extra = self.get_extra_packages(baseline_packages)
        out.write("\n".join(extra) + "\n")

    def write_extra_packages_and_why(self, baseline_packages, out=sys.stdout):
        extra = self.get_extra_packages(baseline_packages)
        extra_packages = self.get_selected_package_objects_by_nvra(extra)
        for p in extra_packages:
            requires = self.what_requires(p.name)
            if requires is None:
                msg = "%s is not selected" % (p.nvra,)
            elif not requires:
                msg = "%s required by nothing" % (p.nvra,)
            else:
                msg = "%s required by %s" % (p.nvra, ' '.join(requires))
            out.write(msg + "\n")

    def get_selected_package_objects_by_nvra(self, packages_nvra):
        ''' return the list of selected package objects that match the list of nvra strings '''
        matching_packages = []
        for package in self.selected_packages:
            for nvra in packages_nvra:
                if package.nvra == nvra:
                    matching_packages.append(package)
        return matching_packages

    def get_missing_packages(self, baseline_packages):
        '''
        return a list of packages that are present in baseline_packages
        but are not selected in the kickstart
        '''
        missing = list(set(self.strip_pseudo_pkgs(baseline_packages)) - set(self.packages_nvra))
        missing.sort()
        return missing

    def write_missing_packages(self, baseline_packages, out=sys.stdout):
        missing = self.get_missing_packages(baseline_packages)
        out.write("\n".join(missing) + "\n")

def package_list_file(path):
    if not os.path.exists(path):
        raise argparse.ArgumentTypeError("path %s does not exist" % (path,))
    value = []
    for line in file(path):
        line = line.strip()
        if not line or line[0] == '#':
            continue
        value.append(line)
    return value

def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("-b", "--blacklist",
                        help="path to a file containing packages to exclude",
                        type=package_list_file)
    parser.add_argument("-o", "--output",
                        help="path to a file to output selected package info",
                        type=argparse.FileType("w"))
    parser.add_argument("--expected",
                        help="path to a file containing expected package results",
                        type=package_list_file)
    parser.add_argument("--missing",
                        help="path to a file to output packages missing when compared to expected",
                        type=argparse.FileType("w"))
    parser.add_argument("--extra",
                        help="path to a file to output extra packages when compared to expected",
                        type=argparse.FileType("w"))
    parser.add_argument("--why-extra",
                        help="also output info for why extra packages are present when used in conjunction with --extra",
                        action="store_true")
    parser.add_argument("--with-meta",
                        help="also output metadata for packages",
                        action="store_true")
    parser.add_argument("kickstart", help="path to the kickstart file")
    args = parser.parse_args(argv)

    if not args.expected and (args.missing or args.extra):
        parser.error("--missing and --extra require --expected")

    pkg_resolver = KickstartPackageResolver(args.kickstart, args.blacklist)
    pkg_resolver.parse()
    if pkg_resolver.problems:
        sys.stderr.write("error: something is wrong with the kickstart or yum repos\n")
        sys.exit(1)

    if args.output:
        args.output.write("# selected packages\n")
        if args.with_meta:
            pkg_resolver.write_packages_meta(args.output)
        else:
            pkg_resolver.write_packages_info(args.output)

    if args.missing:
        args.missing.write("# missing packages\n")
        pkg_resolver.write_missing_packages(args.expected)

    if args.extra:
        args.extra.write("# extra packages\n")
        if args.why_extra:
            pkg_resolver.write_extra_packages_and_why(args.expected)
        else:
            pkg_resolver.write_extra_packages(args.expected)

    pkg_resolver.cleanup()

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))


import matplotlib.pyplot as plt
import numpy as np

def t_chart(patient, lwst, bgst, keylwst, keybgst, constant):
    x_min = lwst - constant
    x_max = bgst + constant

    x = np.linspace(x_min, x_max, 1000)
    y = np.ones(1000)

    range_values = np.where(x <= lwst, 0, np.where(x > bgst, 1, (x - lwst) / (bgst - lwst)))
    range_values = range_values.reshape(1, -1)

    fig, ax = plt.subplots(figsize=(6, 0.5))

    ax.imshow(range_values, cmap='RdYlGn_r', aspect='auto', extent=[x_min, x_max, 0, 1], alpha=0.7)

    ax.plot(x, y, color='black')

    ax.get_yaxis().set_visible(False)
    ax.get_xaxis().set_visible(False)

    for limit, label in zip([lwst, bgst], [keylwst, keybgst]):
        ax.axvline(x=limit, color='black', linestyle='-', linewidth=1.5)
        ax.annotate(str(limit), xy =(limit, 0),
             xytext =(limit, -0.50), ha='center', va='center', fontsize = 14,
             arrowprops = dict(facecolor ='black',headwidth=7, headlength=6))

    # Patient
    custom_value = max(x_min, min(patient, x_max))
    linewidth = 2 if x_min < patient < x_max else 5
    ax.axvline(x=custom_value, color='red', linestyle='-', linewidth=linewidth)

    if patient < x_max and patient > x_min:
        ax.annotate(str(patient), xy =(patient, 1),
            xytext =(patient, 1.45), ha='center', va='center', fontsize = 14,
            arrowprops = dict(facecolor ='red',headwidth=7, headlength=6))
    elif patient <= x_min:
        ax.annotate(str(patient), xy =(x_min + constant/25, 1), fontsize = 14,
            xytext =(x_min + constant/25, 1.45), ha='center', va='center',
            arrowprops = dict(facecolor ='red',headwidth=7, headlength=6))
    else:
        ax.annotate(str(patient), xy =(x_max - constant/25, 1), fontsize = 14,
                xytext =(x_max - constant/25, 1.45), ha='center', va='center',
                arrowprops = dict(facecolor ='red',headwidth=7, headlength=6))

    #plt.show()
    return plt

def trial():
    constant = 2 * (abs(27 - 20)) / 3
    t_chart(7, 20, 27, "<", ">", constant)

#trial()


import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, TextBox, CheckButtons
from scipy.spatial.distance import cdist
from sklearn import cluster
import numpy as np
import pandas as pd

from CallMat_as_Me_Utils import All_MatData
from Initial_Finding_Frequencies import Initial_Frequencies
from Fit_Finding_Frequencies import Fitted_Frequencies

class Find_Fre_GUI:
    def __init__(self):
        # Parents
        self.inital_fre = Initial_Frequencies()
        self.fitted_fre = Fitted_Frequencies()

        # Class attributes
        self.mat_path = None
        self.fre, self.pl = None, None

        # Class parameter
        self.cuton_val = 1
        self.dogmatic_On = False
        self.number_of_NV = 8
        self.kmin, self.kmax =  1, 11

        # Method output
        self.initial_frequencies = None
        self.distortions = np.array([])
        self.inertias = np.array([])
        self.fitting_error = np.array([])
        self.elbow_cutval = 0

    @staticmethod
    def Normalize(arr):
        return (arr - arr.min()) / (arr.max() - arr.min())

    def Get_InitFre(self):
        if self.dogmatic_On:
            self.initial_frequencies = self.inital_fre.Run_Dogmatic(self.fre, self.pl, self.cuton_val, self.number_of_NV)
        else:
            self.initial_frequencies = self.inital_fre.Run_Simple(self.fre, self.pl, self.cuton_val)

    def Plot_GUI(self):
        fig = plt.figure('Finding Frequencies via OD-ESR spectra')
        ax = plt.axes()

        # Plot part
        plt.plot(self.fre / 1e9, self.pl, 'r.-')
        cut_line, = plt.plot(self.fre / 1e9, 0 * self.fre / 1e9 + 1, '-', color='black')

        plt.subplots_adjust(left=0.138, bottom=0.28, top=0.902, right=0.842)
        plt.title('OD-ESR spectra')
        plt.xlabel('Frequency (GHz)')
        plt.ylabel('PL (a.u.)')

        # Widget part

        axamp = plt.axes([0.9, 0.27, 0.0225, 0.63])
        amp_slider = Slider(
            ax=axamp,
            label="Cut-On",
            valmin=self.pl.min(),
            valmax=self.pl.max(),
            valinit=1,
            orientation="vertical")

        reset_ax = plt.axes([0.8, 0.025, 0.1, 0.04])
        reset_button = Button(reset_ax, 'Reset', hovercolor='0.975')

        initial_ax = plt.axes([0.6, 0.025, 0.1, 0.04])
        initial_button = Button(initial_ax, 'Initial', hovercolor='0.975')

        fitted_ax = plt.axes([0.4, 0.025, 0.1, 0.04])
        fitted_button = Button(fitted_ax, 'Fitted', hovercolor='0.975')

        text_ax = plt.axes([0.2, 0.025, 0.1, 0.04])
        text_box = TextBox(text_ax, 'Number of NV  ')

        check_ax = plt.axes([0.05, 0.1, 0.2, 0.1])
        check_box = CheckButtons(check_ax, ['Dogmatic'])

        approx_ax = plt.axes([0.8, 0.1, 0.1, 0.04])
        approx_button = Button(approx_ax, 'Approx K', hovercolor='0.975')

        def update(val):
            cut_line.set_ydata(0 * self.fre + val)
            self.cuton_val = val
            fig.canvas.draw_idle()
        amp_slider.on_changed(update)

        def check_dogmatic(val):
            self.dogmatic_On = check_box.get_status()[0]
        check_box.on_clicked(check_dogmatic)

        def get_numberNV(val):
            self.number_of_NV = int(val)
        text_box.on_submit(get_numberNV)

        def inital_frequencies_plot(val):
            self.Get_InitFre()
            InitPreview = plt.figure('Preview Initial Frequencies')
            InitPreview.clear()
            for fre_now in self.initial_frequencies:
                plt.vlines(fre_now, self.pl.min(), self.pl.max(), label=f'{round(fre_now / 1e9, 2)} GHz')
            plt.plot(self.fre, self.pl, 'r.-')
            plt.subplots_adjust(left=0.13, bottom=0.22, top=0.88, right=0.792)
            plt.legend(bbox_to_anchor=(1.0, 0.5), loc="center left")
            plt.xlabel('Frequency (GHz)')
            plt.ylabel('PL (a.u.)')

            def save_init(val):
                df = pd.DataFrame(np.vstack((np.sort(self.initial_frequencies), np.append(self.ODESR_label, self.ODESR_label[::-1]))).T, columns=['Initial_frequncies (Hz)', 'NV_label'])
                df = df.astype({'Initial_frequncies (Hz)':np.float32, 'NV_label':int})
                try:
                    df.to_csv(f'/Frequencies_CSV_file/init_fre_{self.working_name[0]}.csv', index=False)
                    print('Save Succuessfully')
                except Exception:
                    print('Save Unsuccessfully')
            save_init_ax = plt.axes([0.8, 0.025, 0.1, 0.04])
            save_init_button = Button(save_init_ax, 'Save', hovercolor='0.975')
            save_init_button.on_clicked(save_init)

            plt.show()
        initial_button.on_clicked(inital_frequencies_plot)

        def fitted_frequncies_plot(val):
            self.Get_InitFre()
            self.fitted_frequencies, fitted_model = self.fitted_fre.Run_Fitting(self.fre, self.pl, self.initial_frequencies, get_model=True)
            FitPreview = plt.figure('Preview Fitted Frequencies')
            FitPreview.clear()
            for idx, fre_now in enumerate(self.fitted_frequencies):
                plt.vlines(fre_now, fitted_model[idx].min(), fitted_model[idx].max(), label=f'{round(fre_now / 1e9, 2)} GHz')
                plt.plot(self.fre, fitted_model[idx], '.-')
            plt.subplots_adjust(left=0.13, bottom=0.22, top=0.88, right=0.792)
            plt.legend(bbox_to_anchor=(1.0, 0.5), loc="center left")
            plt.xlabel('Frequency (GHz)')
            plt.ylabel('PL (a.u.)')

            def save_fit(val):
                df = pd.DataFrame(np.vstack((np.sort(self.fitted_frequencies), np.append(self.ODESR_label, self.ODESR_label[::-1]))).T, columns=['Fitted_frequncies (Hz)', 'NV_label'])
                df = df.astype({'Fitted_frequncies (Hz)':np.float32, 'NV_label':int})
                try:
                    df.to_csv(f'./Frequencies_CSV_file/fit_fre_{self.working_name[0]}.csv', index=False)
                    print('Save Succuessfully')
                except Exception:
                    print('Save Unsuccessfully')
            save_fit_ax = plt.axes([0.8, 0.025, 0.1, 0.04])
            save_fit_button = Button(save_fit_ax, 'Save', hovercolor='0.975')
            save_fit_button.on_clicked(save_fit)

            plt.show()
        fitted_button.on_clicked(fitted_frequncies_plot)

        def approximation_k(event):
            # Check the input
            if len(self.fitting_error) == 0 or self.elbow_cutval != self.cuton_val:
                self.elbow_cutval = self.cuton_val
                work_x = self.fre.copy()
                dim = len(work_x.shape)
                if dim == 1:
                    work_x = np.vstack((np.arange(work_x.shape[0]), work_x)).T
                else:
                    work_x = work_x.T
                self.distortions = np.array([])
                self.inertias = np.array([])
                self.fitting_error = np.array([])
                cutoff_x = work_x[np.where(self.pl <= self.cuton_val)]
                for k in range(self.kmin, self.kmax):
                    kmeanModel = cluster.KMeans(n_clusters=k)
                    kmeanModel.fit(cutoff_x)
                    center = kmeanModel.cluster_centers_
                    fitting_model = self.fitted_fre.Get_BestFit(self.fre, self.pl, center[:, 1])
                    self.fitting_error = np.append(self.fitting_error, np.mean((self.pl.copy() - fitting_model)**2))
                    self.distortions = np.append(self.distortions, sum(np.min(cdist(cutoff_x, center, 'euclidean'), axis=1)) / cutoff_x.shape[0])
                    self.inertias = np.append(self.inertias, kmeanModel.inertia_)
            norm_dis, norm_iner, norm_fit_err = self.Normalize(self.distortions), self.Normalize(self.inertias), self.Normalize(self.fitting_error)

            # Plot the elbow plot
            ElbowPreview = plt.figure('Preview Fitted Frequencies')
            ElbowPreview.clear()
            k_arr = np.arange(self.kmin, self.kmax)
            # plt.plot(k_arr, norm_iner, '-*', label = 'Inertias', lw=3, s=10)
            plt.plot(k_arr, norm_dis, '-d', label = 'Distortion', lw=3)
            plt.plot(k_arr, norm_fit_err, '.-', label = 'Fit Error', lw=3)
            plt.legend()
            plt.grid()
            plt.xlabel('Number of K')
            plt.ylabel('Normalized error (a.u.)')

            plt.show()
        approx_button.on_clicked(approximation_k)

        def reset(event):
            amp_slider.reset()
        reset_button.on_clicked(reset)

        plt.show()

    @staticmethod
    def Check_KeyboardInput(key_input):
        try:
            key_input = int(key_input)
        except ValueError:
            key_input = str(key_input)
        return key_input

    def Calculate(self, mat_path):
        while True:
            self.mat_path = mat_path
            Mat_Files = All_MatData(self.mat_path).CallMatData()
            key_lst = list(Mat_Files.keys())
            for idx, key in enumerate(key_lst):
                print(f'[{idx+1}] {key}')
            print('[r] Restart')
            print('[q] Quit')

            keyboard_input = self.Check_KeyboardInput(input('Please Enter: ').lower())
            if keyboard_input == 'q':
                print('Quit')
                break
            elif keyboard_input == 'r':
                continue
            elif keyboard_input in np.arange(len(key_lst) + 1):
                working_mat = Mat_Files[key_lst[keyboard_input-1]]
                self.working_name = key_lst[keyboard_input-1].split('.mat')
                print(f'Number: {keyboard_input} Name: {key_lst[keyboard_input-1]}')

                self.fre, self.pl = working_mat.xvals, working_mat.pl
                self.ODESR_label = working_mat.ODESR_label
                self.dogmatic_On = False
                self.Plot_GUI()

            else:
                print('[-] Can not find your input number or command, please enter again')

if __name__ == '__main__':

    mat_path = './Mat_file/'
    Find_Fre_GUI().Calculate(mat_path=mat_path)


import datetime
import random
import time
import unittest
import uuid

from pamqp import body, commands, constants, header

from aiorabbit import message

class TestCase(unittest.TestCase):

    def setUp(self) -> None:
        self.exchange = str(uuid.uuid4())
        self.routing_key = str(uuid.uuid4())
        self.app_id = str(uuid.uuid4())
        self.content_encoding = str(uuid.uuid4())
        self.content_type = str(uuid.uuid4())
        self.correlation_id = str(uuid.uuid4())
        self.delivery_mode = random.randint(1, 2)
        self.expiration = str(int(time.time()) + random.randint(60, 300))
        self.headers = {str(uuid.uuid4()): str(uuid.uuid4())}
        self.message_id = str(uuid.uuid4())
        self.message_type = str(uuid.uuid4())
        self.priority = random.randint(0, 255)
        self.reply_to = str(uuid.uuid4())
        self.timestamp = datetime.datetime.now()
        self.user_id = str(uuid.uuid4())
        self.body = b'-'.join(
            [str(uuid.uuid4()).encode('latin-1')
             for _offset in range(0, random.randint(1, 100))])

        self.message = message.Message(self.get_method())
        self.message.header = header.ContentHeader(
            0, len(self.body),
            commands.Basic.Properties(
                app_id=self.app_id,
                content_encoding=self.content_encoding,
                content_type=self.content_type,
                correlation_id=self.correlation_id,
                delivery_mode=self.delivery_mode,
                expiration=self.expiration,
                headers=self.headers,
                message_id=self.message_id,
                message_type=self.message_type,
                priority=self.priority,
                reply_to=self.reply_to,
                timestamp=self.timestamp,
                user_id=self.user_id))

        value = bytes(self.body)
        while value:
            self.message.body_frames.append(
                body.ContentBody(value[:constants.FRAME_MAX_SIZE]))
            value = value[constants.FRAME_MAX_SIZE:]

    def get_method(self):
        raise NotImplementedError

    def compare_message(self):
        for attribute in [
                'exchange', 'routing_key', 'app_id', 'content_encoding',
                'content_type', 'correlation_id', 'delivery_mode',
                'expiration', 'headers', 'message_id', 'message_type',
                'priority', 'reply_to', 'timestamp', 'user_id', 'body']:
            expectation = getattr(self, attribute)
            value = getattr(self.message, attribute)
            if isinstance(expectation, dict):
                self.assertDictEqual(value, expectation)
            else:
                self.assertEqual(value, expectation)
        self.assertEqual(bytes(self.message), self.body)
        self.assertEqual(len(self.message), len(self.body))
        self.assertTrue(self.message.is_complete)

class BasicDeliverTestCase(TestCase):

    def setUp(self) -> None:
        self.consumer_tag = uuid.uuid4().hex
        self.delivery_tag = random.randint(0, 100000000)
        self.redelivered = bool(random.randint(0, 1))
        super().setUp()

    def get_method(self):
        return commands.Basic.Deliver(
            self.consumer_tag, self.delivery_tag, self.redelivered,
            self.exchange, self.routing_key)

    def test_properties(self):
        self.assertEqual(self.message.consumer_tag, self.consumer_tag)
        self.assertEqual(self.message.delivery_tag, self.delivery_tag)
        self.assertEqual(self.message.redelivered, self.redelivered)
        self.assertIsNone(self.message.message_count)
        self.assertIsNone(self.message.reply_code)
        self.assertIsNone(self.message.reply_text)
        self.compare_message()

class BasicGetOkTestCase(TestCase):

    def setUp(self) -> None:
        self.delivery_tag = random.randint(0, 100000000)
        self.message_count = random.randint(0, 1000)
        self.redelivered = bool(random.randint(0, 1))
        super().setUp()

    def get_method(self):
        return commands.Basic.GetOk(
            self.delivery_tag, self.redelivered, self.exchange,
            self.routing_key, self.message_count)

    def test_properties(self):
        self.assertEqual(self.message.delivery_tag, self.delivery_tag)
        self.assertEqual(self.message.message_count, self.message_count)
        self.assertEqual(self.message.redelivered, self.redelivered)
        self.assertIsNone(self.message.reply_code)
        self.assertIsNone(self.message.reply_text)
        self.compare_message()

class BasicReturnTestCase(TestCase):

    def setUp(self) -> None:
        self.reply_code = random.randint(200, 500)
        self.reply_text = str(uuid.uuid4())
        super().setUp()

    def get_method(self):
        return commands.Basic.Return(
            self.reply_code, self.reply_text, self.exchange, self.routing_key)

    def test_properties(self):
        self.assertEqual(self.message.reply_code, self.reply_code)
        self.assertEqual(self.message.reply_text, self.reply_text)
        self.assertIsNone(self.message.consumer_tag)
        self.assertIsNone(self.message.delivery_tag)
        self.assertIsNone(self.message.message_count)
        self.assertIsNone(self.message.redelivered)
        self.compare_message()


from collections import deque

g_mango = {
    "i": ["alice", "bob", "claire"],
    "alice": ["peggy"],
    "bob": ["anuj", "peggy"],
    "claire": ["thom", "jonny"],
    "anuj": [],
    "peggy": [],
    "thom": [],
    "jonny": []
}
sells_mango = {"peggy": True}

def sell_mango(person):
    if sells_mango.get(person):
        return sells_mango[person]

def search(name):
    search_queue = deque()
    search_queue += g_mango[name]
    verified = []
    while search_queue:
        person = search_queue.popleft()
        if person not in verified:
            if sell_mango(person):
                print(person + " sells mango!")
                return True
            else:
                search_queue += g_mango[person]
                verified.append(person)
    return False

search("i")


import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','first_project.settings')

import django
django.setup()

import random
from firstapp.models import AccessRecord,Webpage,Topic
from faker import Faker

fakegen = Faker()

topics = ['Search', 'social', 'Sports','Politics']

def add_topic():
    t= Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t

def populate(N=5):
    for i in range(N):

        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()

        wepg = Webpage.objects.get_or_create(Topic=random.choice(topics),name=fake_name,url=fake_url)[0]
        acred = AccessRecord.objects.get_or_create(name=fake_name,date=fake_date)[0]

if __name__=='__main__':
    print("start")
    populate(N=10)
    print("done")


from collections import Counter

ll = [x for x in open('day5.txt').read().strip().split('\n')]

pts = []
for line in ll:
	x1=int(line.split()[0].split(",")[0])
	y1=int(line.split()[0].split(",")[1])

	x2=int(line.split()[2].split(",")[0])
	y2=int(line.split()[2].split(",")[1])
	if x1==x2 or y1==y2:
		for x in range(min(x1,x2),max(x1,x2)+1):
			for y in range(min(y1,y2),max(y1,y2)+1):
				pts.append((x,y))

print(len([x for (x,y) in Counter(pts).items() if y>1]))