import numpy as np
import matplotlib.pyplot as plt

def transform_map(map_image, rot_mat,
                  in_projection='platecarre',
                  out_projection='platecarre',
                  display=True,
                  debug=False):

    invalid_projection_msg = (
        "Please choose a projection from "
        "'platecarre' or 'gallpeters'."
    )

    ny, nx, _ = map_image.shape
    mapdims = (nx, ny)

    if debug:
        print(f'ny, nx are {ny} and {nx}')
    xs, ys = np.meshgrid(np.arange(0, nx), np.arange(0, ny))
    xs = xs.flatten().astype(float)
    ys = ys.flatten().astype(float)
    if debug:
        print(f'shape of xs and ys are {xs.shape} and {ys.shape}')
        print(f'xs min and max are {min(xs)} and {max(xs)}')
        print(f'ys min and max are {min(ys)} and {max(ys)}')

    if in_projection == 'platecarre':
        longs, lats = longitudeAndLatitudeFromPCPixel(xs, ys, mapdims)
    elif in_projection == 'gallpeters':
        longs, lats = longitudeAndLatitudeFromGPPixel(xs, ys, mapdims)
    else:
        raise ValueError(invalid_projection_msg)

    if debug:
        print(f'shape of longs and lats are {longs.shape} and {lats.shape}')
        print(f'longitudes histogram')
        plt.hist(longs)
        print(f'latitudes histogram')
        plt.hist(lats)

    xxs, yys, zzs = spatialCoordinatesFromLongitudeAndLatitude(longs, lats)
    if debug:
        print('pre rotation histograms')
        plt.hist(xxs)
        plt.hist(yys)
        plt.hist(zzs)
    new_xxyyzz = np.dot(rot_mat, np.array([xxs, yys, zzs]))

    new_xxs = np.squeeze(np.array(new_xxyyzz[0, :]))
    new_yys = np.squeeze(np.array(new_xxyyzz[1, :]))
    new_zzs = np.squeeze(np.array(new_xxyyzz[2, :]))
    if debug:
        print('post rotation hists')
        plt.hist(new_xxs)
        plt.hist(new_yys)
        plt.hist(new_zzs)

    new_longs, new_lats = longitudeAndLatitudeFromSpatialCoordinates(
        new_xxs, new_yys, new_zzs)
    if debug:
        print('new longs')
        plt.hist(new_longs)
        print('new lats')
        plt.hist(new_lats)

    if out_projection == 'platecarre':
        new_xs, new_ys = pixel_for_plate_carre(new_longs, new_lats, mapdims)
    elif out_projection == 'gallpeters':
        new_xs, new_ys = gall_peters_pixels(new_longs, new_lats, mapdims)
    else:
        raise ValueError(invalid_projection_msg)

    if debug:
        print('new x pixels and new y pixels')
        plt.hist(new_xs)
        plt.hist(new_ys)

        print(f'shape of new xs and new ys is {new_xs.shape} {new_ys.shape}')
        print(f'ny, nx are {ny}, and {nx}')

    new_xs.shape = (ny, nx)
    new_ys.shape = (ny, nx)

    new_map = np.zeros(dtype=map_image.dtype, shape=map_image.shape)

    new_map[:, :, 0] = map_image[new_ys, new_xs, 0]
    new_map[:, :, 1] = map_image[new_ys, new_xs, 1]
    new_map[:, :, 2] = map_image[new_ys, new_xs, 2]

    if display:
        plt.imshow(new_map)
    return(new_map)

def longitudeAndLatitudeFromGPPixel(x, y, mapdims):
    """
    GP Projection:

    x = R lambda / sqrt(2)
    y = R sqrt(2) sin(phi)
        lambda is longitude (left right)
        phi is latitude (up down)
        R is radius of globe, we'll use 1.0
    lambda = (x/R) * sqrt(2)
    phi = arcsin((y/R) / sqrt(2))
    """
    nx, ny = mapdims
    longitude = x * (2.0 * np.pi / float(nx)) - np.pi
    latitude = np.arcsin(2.0 * ((y / ny) - (0.5)))
    return(longitude, latitude)

def longitudeAndLatitudeFromPCPixel(x, y, mapdims):
    nx, ny = mapdims
    longitude = 2 * np.pi * ((x / float(nx)) - 0.5)
    latitude = - (np.pi * ((y / float(ny)) - 0.5))
    return (longitude, latitude)

def spatialCoordinatesFromLongitudeAndLatitude(longitude, latitude):
    x = np.cos(longitude) * np.cos(latitude)
    y = np.sin(longitude) * np.cos(latitude)
    z = np.sin(latitude)
    return (x, y, z)

def flip_longitudes(longitude):
    #  which need to be flipped?
    inds = np.greater(longitude, np.pi)
    longitude = longitude - 2 * np.pi * inds
    return(longitude)

def longitudeAndLatitudeFromSpatialCoordinates(xxs, yys, zzs):
    latitude = np.arcsin(zzs)
    longitude = np.arctan2(yys, xxs)
    longitude = flip_longitudes(longitude)
    return((longitude, latitude))

def gall_peters_pixels(longitude, latitude, mapdims):
    nx, ny = mapdims

    u = nx * (0.5 + (longitude / (2 * np.pi)))
    v = (ny / 2) * (np.sin(latitude) + 1.0)

    u = u.astype(int)
    v = v.astype(int)

    #  Happens w/ rounding.
    u[u == nx] = nx - 1
    v[v == ny] = 0

    return((u, v))

def pixel_for_plate_carre(longitude, latitude, mapdims):
    nx, ny = mapdims

    u = nx * (0.5 + (longitude / (2 * np.pi)))
    v = - (ny * (0.5 + (latitude / np.pi)))

    u = u.astype(int)
    v = v.astype(int)

    u[u == nx] = nx - 1
    v[v == ny] = ny - 1

    return(u, v)

def rotationMatrix(axis_vector, angle):
    axis_vector = axis_vector / np.sqrt(sum(axis_vector**2))
    (x, y, z) = axis_vector

    tensor_product_matrix = np.matrix([
        [x**2, x * y, x * z],
        [x * y, y**2, y * z],
        [x * z, y * z, z**2]
    ])
    cross_product_matrix = np.matrix([
        [0.0, -z, y],
        [z, 0.0, -x],
        [-y, x, 0.0]
    ])
    R = np.eye(3) * np.cos(angle) + \
        cross_product_matrix * np.sin(angle) + \
        tensor_product_matrix * (1 - np.cos(angle))

    return R

def rotationFromXYZ(angle_x, angle_y, angle_z):
    r_x = rotationMatrix(np.array([1.0, 0.0, 0.0]), angle_x)
    r_y = rotationMatrix(np.array([0.0, 1.0, 0.0]), angle_y)
    r_z = rotationMatrix(np.array([0.0, 0.0, 1.0]), angle_z)

    return (np.dot(np.dot(r_x, r_y), r_z))

def rotationFromTwoLocations(
        longlatA,
        longlatB):
    """
    Given two tuples (longitude and latitude),
    finds a rotation that places the first center left,
    and the second center right.
    """
    vec1 = spatialCoordinatesFromLongitudeAndLatitude(*longlatA)
    vec2 = spatialCoordinatesFromLongitudeAndLatitude(*longlatB)

    vec1 = np.array(vec1)
    vec2 = np.array(vec2)
    vec1 = vec1 / np.linalg.norm(vec1)
    vec2 = vec2 / np.linalg.norm(vec2)

    middle = vec1 + vec2
    middlevec = middle / np.linalg.norm(middle)

    crossprod = np.cross(vec1, vec2)
    crossvec = crossprod / np.linalg.norm(crossprod)

    #  Determines handedness / puts them in the center
    # thirdvec = np.cross(crossvec, middlevec)
    # thirdvec = thirdvec / np.linalg.norm(thirdvec)

    #  diffvec
    diffvec = vec2 - vec1
    diffvec = diffvec / np.linalg.norm(diffvec)

    rotation = np.matrix([middlevec, diffvec, crossvec])

    return(rotation.T)

def eulerAxisAngleFromRotationMatrix(R):
    """
    Returns a tuple, (theta, v)
    """
    theta = np.arccos(0.5 * (R[0,0] + R[1,1,] + R[2,2] - 1))

    v1 = R[2, 1] - R[1, 2]
    v2 = R[0, 2] - R[2, 0]
    v3 = R[1, 0] - R[0, 1]

    vec = np.array([v1, v2, v3]) / (2.0 * np.sin(theta))

    return(theta, vec)

def quaternionFromMatrix(R):
    """
    Returns a list [qi, qj, qk, qr]
    """
    qr = 0.5 * np.sqrt(1 + np.trace(R))

    v1 = R[2, 1] - R[1, 2]
    v2 = R[0, 2] - R[2, 0]
    v3 = R[1, 0] - R[0, 1]

    qi, qj, qk = [v / 4.0 * qr for v in [v1, v2, v3]]

    return [qi, qj, qk, qr]


#!/usr/bin/python3
'''
Programmer: Miles Boswell

Take user input from cli (image file, tool to apply on image)
Display original image and transformed image side by side in window
Optional: overwrite image file with transformation
'''
import sys
from matplotlib import pyplot as plt
from PIL import Image
from tools.utils import error, transform, visualize_text

def user_input():
	''' @return: dictionary with Image object as 'img',
				 file of Image as 'file', and optional tool
				 to apply '''
	if len(sys.argv) > 1:
		sys.argv.pop(0)
		data = {}
		image_file = ''
		if '-t' in sys.argv:
			tool_index = sys.argv.index('-t') + 1
			tool = sys.argv.pop(tool_index)
			data['tool'] = tool
			sys.argv.pop(tool_index - 1)
		elif '-ts' in sys.argv:
			tool_index = sys.argv.index('-ts') + 1
			tool = sys.argv.pop(tool_index)
			data['tool'] = tool
			sys.argv.pop(tool_index - 1)
			data['save'] = sys.argv[0]

		if '-s' in sys.argv:
			loc_index = sys.argv.index('-s') + 1
			loc = sys.argv.pop(loc_index)
			data['save'] = loc

		image_file = sys.argv[0]
		if image_file.endswith('.txt'):
			img = visualize_text(image_file)
			img.show()
			sys.exit()

		try:
			img = Image.open(image_file)
		except:
			error('Could not find file: {}'.format(image_file))
		else:
			data['img'], data['file'] = img, image_file
		return data
	else:
		error('You must specify an image file.')

if __name__ == '__main__':
	data = user_input()
	# if a tool is specified, apply it to the Image and display
	img, tool = data.get('img'), data.get('tool', None)
	fileloc = data.get('file')
	save = data.get('save', False)
	if tool:
		trans = transform(tool)
		newimg = trans(img)
		newimg.show()
		if save:
			newimg.save(save)
	else:
		img.show()


import os
import random
import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.nn.functional as F
from torch.optim import Adam
from torchvision.utils import make_grid

from models.ae.autoencoder import VAE
from datasets.dataset import cifar

# import matplotlib.pyplot as plt

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

# %% --------------------------------------- Fix Seeds -----------------------------------------------------------------
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)

torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.benchmark = True

# Hyper-parameters
batch_size = 128
num_epoch = 30
std_channel = 64
latent_dim = 128
learning_rate = 1e-3
beta1 = 0.5
beta2 = 0.9
dataset = cifar
image_size = 64
image_channel = 3

SAVE_PATH = f"../weights/var_ae/{dataset.__name__}/"
if not os.path.exists(SAVE_PATH):
    os.makedirs(SAVE_PATH)

train_loader = dataset(image_size=image_size, train=True, batch_size=batch_size)
test_loader  = dataset(image_size=image_size, train=False, batch_size=10000)

test_images = test_loader.__iter__().next()[0]
test_labels = test_loader.__iter__().next()[1]
target_idx = np.array([[np.where(test_labels==idx)[0][iter]
                        for idx in np.unique(test_labels)]
                       for iter in range(10)]).ravel()
test_images = test_loader.__iter__().next()[0][target_idx].to(device)
test_labels = test_loader.__iter__().next()[1][target_idx].to(device)

vae = VAE(image_size=image_size,
                  image_channel=image_channel,
                  std_channel=std_channel,
                  latent_dim=latent_dim).to(device)

# BCELoss = nn.MSELoss(reduction='sum')
# BCELoss = nn.MSELoss(reduction='sum')
# KLDLoss = lambda mu, log_var: -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
# optimizer = Adam([{'params':encoder.parameters(),
#                    'params': decoder.parameters()}],
#                              lr=learning_rate,
#                              weight_decay=1e-5,
#                              betas=(beta1, beta2))
optimizer = Adam(vae.parameters(), lr=learning_rate,
                             weight_decay=1e-3,
                             betas=(beta1, beta2))
# optimizer = Adam(vae.parameters())

def loss_function(recon_x, x, mu, log_var):
    BCE = F.mse_loss(recon_x, x, reduction='sum')
    # BCE = F.binary_cross_entropy(recon_x, x, reduction='sum')
    KLD = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
    # KLD = torch.mean(-0.5 * torch.sum(1 + log_var - mu ** 2 - log_var.exp(), dim = 1), dim = 0)

    return BCE + KLD

for epoch in range(num_epoch):
    train_loss = 0
    for i, (image, label) in enumerate(train_loader):
        image = image.to(device)
        label = label.to(device)

        decode, mu, log_var = vae(image)

        # bceloss = BCELoss(decode, image)
        # kldloss = KLDLoss(mu, log_var)
        # loss = bceloss + kldloss

        loss = loss_function(decode, image, mu, log_var)

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

        train_loss += loss.item()

    print(f"Epoch: {epoch+1}/{num_epoch}, Loss: {train_loss/len(train_loader)}")
    # torch.save(encoder.state_dict(), SAVE_PATH + f"encoder_{epoch+1}.pth")
    # torch.save(decoder.state_dict(), SAVE_PATH + f"decoder_{epoch+1}.pth")
    with torch.no_grad():
        vae.eval()
        z = torch.randn(64, 128).cuda()
        decode = vae.decoder(z)
        # encode, mu, log_var = encoder(test_images)
        # decode = decoder(encode)
        grid = make_grid(decode.detach().cpu(), nrow=10, normalize=True)
        plt.imshow(grid.permute(1,2,0))
        plt.show()

# #################### Save numpy ###################
# test_encode_results = np.empty((0,128))
# test_decode_results = np.empty((0, image_channel, image_size, image_size))
# for idx, (image, labels) in enumerate(test_loader):
#     image = image.to(device)
#     labels = labels.to(device)
#     with torch.no_grad():
#         encoder.eval()
#         decoder.eval()
#
#         encode = encoder(image)
#         decode = decoder(encode)
#
#     test_encode_results = np.append(test_encode_results, encode.detach().cpu().numpy(), axis=0)
#     test_decode_results = np.append(test_decode_results, decode.detach().cpu().numpy(), axis=0)
#
#     print(f"Epoch: {idx+1}/{len(test_loader)}")

# with torch.no_grad():
#     decoder.eval()
#     fixed_vector_output = decoder(encoder(fixed_test_dataset))
#     grid = make_grid(fixed_vector_output.detach().cpu(), nrow=10, normalize=True)
#     plt.imshow(grid.permute(1,2,0))
#     plt.show()
#


import pandas as pd
import numpy as np
#!pip install pandas_read_xml
import pandas_read_xml as pdx
from tqdm import tqdm
import glob
import os
from xml.etree.ElementTree import parse
import pathlib

def clean_data(input_string):
    punctuation = '!"#$%&\'()*+,./:;<=>?@[\\]^_`{|}~'
    return input_string.translate(str.maketrans('', '', punctuation))

class XMLparser:

    def __init__(self, file):

        self.file = file
        tree = parse(self.file) #XML 파일을 tree 형태로 파싱
        self.root = tree.getroot() #연결된 노드의 뿌리를 모두 불러옴

    def xml2npy(self):

        ecg = np.zeros([5000,12]) #ecg Size 초기화

        if(self.root.tag == 'INFINITT_CIS'): #INFINITT_CIS인 경우

                WaveInfo = self.root.findall("WaveInfo")
                WaveData = [x.findall("WaveData") for x in WaveInfo]
                Data = [x.findtext("Data") for x in WaveData[0]]

                for i in range(len(Data)):

                    ecg[:,i] = np.array(Data[i].split(','),dtype='float32')

        elif(self.root.tag == 'BTypeECG'): #IBTypeECG인 경우

            StudyInfo = self.root.findall("StudyInfo")
            RecordData = [x.findall("RecordData") for x in StudyInfo]
            Waveform = [x.findall("Waveform") for x in RecordData[0]]

            for i in range(len(Waveform)):

                Data = [x.findtext("Data") for x in Waveform[i]]

                cleaned_Data = clean_data(Data[0])
                wave = cleaned_Data.rstrip().split(' ')

                if(len(wave)== 4999):
                    wave.extend(wave[-1:]) #4999일 경우 가장 마지막 데이터 추가
                elif(len(wave) < 100):
                    wave = np.zeros(5000)
                elif(len(wave) < 5000):
                    wave.extend(list(np.zeros(5000-len(wave))))
                try:
                    ecg[:,i] = np.array(wave,dtype='float32')
                except ValueError:
                    wave = np.resize(wave,[5000])
                    ecg[:,i] = np.array(wave,dtype='float32')
                    print('error!')

        else:
            print('Type ERROR')

        return ecg

    def xml2info(self):

        path = pathlib.Path(self.file)
        unique_id = path.name.split('.')[0]

        info_dict = {}

        if(self.root.tag == 'INFINITT_CIS'): #INFINITT_CIS인 경우

            PatientInfo = self.root.findall("PatientInfo") #환자 정보 불러옴
            PatientID = [x.findtext("ID") for x in PatientInfo] #환자 ID 불러옴
            Age = [x.findtext("Age") for x in PatientInfo] #환자 ID 불러옴
            Gender = [x.findtext("Gender") for x in PatientInfo] #환자 ID 불러옴

            StudyInfo = self.root.findall("StudyInfo") # Examination 태그 데이터 정보 불러옴
            Status = [x.findtext("Status") for x in StudyInfo]
            StudyDate = [x.findtext("Date") for x in StudyInfo]
            StudyTime = [x.findtext("Time") for x in StudyInfo]

            Examination = self.root.findall("Examination") # Examination 태그 데이터 정보 불러옴
            Diagnosis = [x.findall("Diagnosis") for x in Examination] #진단 정보 불러옴
            Statements = [x.findtext("Statement") for x in Diagnosis[0]] #진단 정보의 Statements 태그 (SMC 라벨링)

            Status.extend(Statements)

            #info_dict['Severity'] = Severity[0]
            info_dict['Statements'] = Status
            info_dict['Age'] = Age[0]
            info_dict['gender'] = Gender[0]
            info_dict['StudyDate'] = StudyDate[0]
            info_dict['StudyTime'] = StudyTime[0]
            info_dict['Type'] = 'INFINITT_CIS'
            info_dict['Patient_id'] = PatientID[0]
            info_dict['unique_id'] = unique_id

        elif(self.root.tag == 'BTypeECG'): #IBTypeECG인 경우

            PatientInfo = self.root.findall("PatientInfo") #환자 정보 불러옴
            PatientID = [x.findtext("PatientID") for x in PatientInfo] #환자 ID 불러옴
            Age = [x.findtext("PatientAge") for x in PatientInfo] #환자 ID 불러옴
            Gender = [x.findtext("PatientSex") for x in PatientInfo] #환자 ID 불러옴

            StudyInfo = self.root.findall("StudyInfo") # Examination 태그 데이터 정보 불러옴
            StudyDate = [x.findtext("StudyDate") for x in StudyInfo]
            StudyTime = [x.findtext("StudyTime") for x in StudyInfo]

            Interpretation = [x.findall("Interpretation") for x in StudyInfo]
            Statements = [x.findall("Statement") for x in Interpretation[0]]

            Mdsignatureline = [x.findtext("Mdsignatureline") for x in Interpretation[0]]
            Leftstatement = [x.findtext("Leftstatement") for x in Statements[0]]
            Mdsignatureline.extend(Leftstatement)

            #info_dict['Severity'] = Severity[0]
            info_dict['Statements'] = Mdsignatureline
            info_dict['Age'] = Age[0]
            info_dict['gender'] = Gender[0]
            info_dict['StudyDate'] = StudyDate[0]
            info_dict['StudyTime'] = StudyTime[0]
            info_dict['Type'] = 'BTypeECG'
            info_dict['Patient_id'] = PatientID[0]
            info_dict['unique_id'] = unique_id

        else:
            print(file)
        return info_dict


import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
import matplotlib.pyplot as plt

import hydra
from omegaconf import DictConfig, OmegaConf

from models import ResNet50, ResNet101, ResNet152

data_dir = '/content/gsn-hydra/dataset'

transforms = {
    'Training': transforms.Compose
    ([
        transforms.RandomResizedCrop(224),
        #transforms.ToPILImage(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'Validation': transforms.Compose
    ([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

val_losses = []
train_losses = []
val_accuracy = []
train_accuracy = []

def train(model, criterion, optimizer, scheduler, epochs, loader, dataset_sizes):
    since = time.time()

    best_model_wts = copy.deepcopy(model.state_dict())
    val_acc = 0.0

    for epoch in range(epochs):
        print('Epoch {}/{}'.format(epoch, epochs - 1))

        for x in range(2):
            if x == 0:
                model.train()
                stage = 'Training'
            elif x == 1:
                model.eval()
                stage = 'Validation'
            else:
               break

            partial_loss = 0.0
            partial_corrects = 0
            val_loss = 0.0

            for data, targets in loader[stage]:
                device_data = data.to(device)
                device_targets = targets.to(device)
                optimizer.zero_grad()

                with torch.set_grad_enabled(x == 0): # Training
                    outputs = model(device_data)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, device_targets)

                    if x == 0: # Training
                        loss.backward()
                        optimizer.step()

                partial_loss += loss.item() * device_data.size(0)
                partial_corrects += torch.sum(preds == device_targets.data)
            if x == 0: # Training
                scheduler.step()

            loss_ = partial_loss / dataset_sizes[stage]
            acc = partial_corrects.double() / dataset_sizes[stage]

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(stage, loss_, acc))
            if x == 1:
              val_losses.append(loss_)
              val_accuracy.append(acc)
            else:
              train_losses.append(loss_)
              train_accuracy.append(acc)

            if x == 1 and acc > val_acc: # Validation
                val_acc = acc
                best_model_wts = copy.deepcopy(model.state_dict())

            if x == 1: # Validation, early stopping
              if val_loss < loss_ and val_loss != 0:
                  model.load_state_dict(best_model_wts)
                  return model
              else:
                val_loss = loss_

        print()

    model.load_state_dict(best_model_wts)
    return model

@hydra.main(config_path="./conf", config_name="config")
def parameters(cfg: DictConfig) -> None:
    #print(OmegaConf.to_yaml(cfg))
    workers = 2
    optimizer = cfg['optimizer']['name']
    learning_r = cfg['learning_rate']['lr']
    momentum_ = cfg['momentum']['value']
    dropout = cfg['dropout']['value']
    batch = cfg['batch_size']['size']
    epochs = cfg['epochs']['number']
    model =cfg['model']['name']

    image_split = {i: datasets.ImageFolder(os.path.join(data_dir, i), transforms[i])for i in ['Training', 'Validation']}

    loader = {i: torch.utils.data.DataLoader(image_split[i], batch_size=batch, shuffle=True, num_workers=workers) for i in ['Training', 'Validation']}

    dataset_sizes = {i: len(image_split[i]) for i in ['Training', 'Validation']}

    class_names = image_split['Training'].classes

    nr_epochs = epochs

    if model == "resnet50":
        res = ResNet50()
    elif model == "resnet101":
        res = ResNet101
    elif model == "resnet152":
        res = ResNet152

    res.to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))

    criterion = nn.CrossEntropyLoss()

    optimizer_ft = optim.SGD(res.parameters(), lr=learning_r, momentum=momentum_)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)

    model_ft = train(res, criterion, optimizer_ft, exp_lr_scheduler, nr_epochs, loader, dataset_sizes)

    PATH = "model.pt"
    torch.save({
                'model_state_dict': model_ft.state_dict(),
                'optimizer_state_dict': optimizer_ft.state_dict(),
                }, PATH)

if __name__ == "__main__":

    parameters()


import argparse
import importlib
import os
from pydoc import locate

from di import container

def get_full_path(command_string: str):
    class_name = command_string.capitalize() + "Command"
    module_path = f"commands.{class_name}.{class_name}"
    return locate(module_path)

class Arguments:
    pass

def parse_unknown_args(unknown_args):
    args_obj = Arguments()
    prefix_chars = argparse.ArgumentParser().prefix_chars

    while unknown_args:
        arg = unknown_args.pop(0)
        if arg[0] in prefix_chars:
            arg = arg.lstrip(prefix_chars)
            if '=' in arg:
                key, value = arg.split('=', 1)
                setattr(args_obj, key, value)
            elif unknown_args and unknown_args[0][0] not in prefix_chars:
                value = unknown_args.pop(0)
                setattr(args_obj, arg, value)
            else:
                setattr(args_obj, arg, True)
        else:
            print(f'Warning: {arg} is not recognized as a valid argument.')

    return args_obj

def load_all_modules_from_dir(dirname):
    for module_name in os.listdir(dirname):
        if module_name == '__init__.py' or module_name[-3:] != '.py':
            continue

        module_path = os.path.join(dirname, module_name).replace(os.sep, '.').rstrip('.py')
        importlib.import_module(module_path)

def initialize_app():
    load_all_modules_from_dir("environments")
    load_all_modules_from_dir("trainers")
    load_all_modules_from_dir("commands")

def main():
    parser = argparse.ArgumentParser(description="ML Sandbox")
    parser.add_argument("command", help="Command to run")

    args, _ = parser.parse_known_args()

    class_name = get_full_path(args.command)
    command_class = container.get(class_name)

    if not command_class:
        print(f"Unknown command: {args.command}")
        return

    # Allow the loaded command class to add its specific arguments
    command_class.arg_spec(parser)
    args, unknown = parser.parse_known_args()  # Parse again with full arguments
    vargs = vars(args)
    if len(unknown):
        unknown = parse_unknown_args(unknown)
        vargs.update(vars(unknown))
    vargs.pop("command")
    # Execute the command
    result = command_class.execute(**vargs)
    print(result)

if __name__ == '__main__':
    initialize_app()
    main()


from flask import render_template, request, Blueprint, redirect, url_for
from app.main1 import *

bp = Blueprint("delete_word", __name__, template_folder="../templates")

@bp.route("/deleteword", methods=["GET", "POST"])
def DeleteWord():

    if request.method == 'POST':
        value = request.form.get("value")
        if value:
            word_list = Dictionary("app/deneme.json")
            word_list.delete_word(value=value)
            return redirect(url_for("show_list.ShowList"))
    return render_template('delete.html')


import pydicom
import matplotlib.pyplot as plt
import glob
import os
import json
import zipfile
import shutil

inputDir = "D:\\OPTIMAM"
outputDir = "D:\\Images"

for fileName in os.listdir(inputDir):
    print("Current file:", fileName)#folder name = Patient code for images and metadata as well

    #Uncompressing zipped files that contain DICOM images
    zip_ref = zipfile.ZipFile(inputDir +'\\' + fileName, 'r')
    zip_ref.extractall(inputDir)
    zip_ref.close()

    clientID = str(fileName).replace('.zip', '')
    metadataFile = "D:\\OPTIMAM" + "\\data\\" + clientID + '\\nbss_' + clientID + '.json'

    print("MetaData File:", metadataFile)
    # Image counts
    imgSavedCount = 0
    imgSkippedCount = 0  # The count of images marked as 'For Processing', those images don't seem suitable for ML

    statsFile = open("stats.txt", "a")  # This file records statsitics on the images processed
    logFile = open("log.txt", "a")  # This file records exceptions (e.g. unexpected label, or missing DICOM tag)
    for file in glob.iglob(inputDir +"\\"+ clientID + '\\**\\*.dcm', recursive=True):
        #print(imgID)
        imgID = os.path.basename(file).split(".dcm")[0]
        dataset = pydicom.dcmread(file) #Reading the DICOM file

        try:
            if dataset.PresentationIntentType == "FOR PRESENTATION":#We only looking for unprocessed images
                #patientAge = dataset.PatientAge
                imgLaterality = dataset.ImageLaterality

                #Get the image label from json file
                #For example, the json file of optm1 should be found in: OPTIMAM\data\optm1\nbss_optm1.json
                with open(metadataFile) as f:
                    metadata = json.load(f)
                imgLabel = metadata['Classification']
                print("Label:", imgLabel)

                if imgLabel == "M":
                    plt.imsave(arr=dataset.pixel_array, fname=outputDir+"\\Malignant\\" + str(imgID)+"_"+str(imgLaterality)+".jpg", cmap=plt.cm.gray)
                    imgSavedCount = imgSavedCount + 1
                elif imgLabel == "B":
                    plt.imsave(arr=dataset.pixel_array, fname=outputDir+"\\Benign\\" + str(imgID)+"_"+str(imgLaterality)+".jpg", cmap=plt.cm.gray)
                    imgSavedCount = imgSavedCount + 1
                elif imgLabel == "CI":
                    plt.imsave(arr=dataset.pixel_array, fname=outputDir+"\\CI\\" + str(imgID)+"_"+str(imgLaterality)+".jpg", cmap=plt.cm.gray)
                    imgSavedCount = imgSavedCount + 1
                elif imgLabel == "N":
                    plt.imsave(arr=dataset.pixel_array, fname=outputDir+"\\Negative\\" + str(imgID)+"_"+str(imgLaterality)+".jpg", cmap=plt.cm.gray)
                    imgSavedCount = imgSavedCount + 1
                else:
                    print("Unexpected Label!")
                    logFile.write(clientID + ":Label=" + str(imgLabel) + "\n")
            else:
                imgSkippedCount = imgSkippedCount + 1
        except:
            print("Exception: Missing DICOM Tag in ",imgID)
            logFile.write(clientID + ":" + str(imgID) + "\n")

    print(imgSavedCount, " images saved.")
    print(imgSkippedCount, " images skipped.")
    os.remove(inputDir + '\\' + fileName)
    shutil.rmtree(inputDir + '\\' + clientID)
    statsFile.write(clientID + "," + str(imgSavedCount) + "," + str(imgSkippedCount) + "\n")
    statsFile.close()
    logFile.close()


#!/usr/bin/python3
# Zhang, Kaixu: kareyzhang@gmail.com
import argparse
import sys
import json
import time
import math

class Weights(dict): # 管理平均感知器的权重
    def __init__(self,penalty='no'):
        self._values=dict()
        self._last_step=dict()
        self._step=0
        self._ld=0.0001
        self._p=0.999
        self._log_p=math.log(self._p)
        self._words = dict()
        self._rwords = dict()
        self._mwords = dict()

        self._acc=dict()
        #self._new_value=self._l1_regu
        pena={'no':self._no_regu,'l1':self._l1_regu,'l2':self._l2_regu}
        self._new_value=pena[penalty]

    def _no_regu(self,key):
        dstep=self._step-self._last_step[key]
        value=self._values[key]

        # no regularization
        new_value=value
        self._acc[key]+=dstep*value

        self._values[key]=new_value
        self._last_step[key]=self._step
        return new_value

    def _l1_regu(self,key):
        dstep=self._step-self._last_step[key]
        value=self._values[key]

        # l1-norm regularization
        dvalue=dstep*self._ld
        new_value=max(0,abs(value)-dvalue)*(1 if value >0 else -1)
        if new_value==0 :
            self._acc[key]+=(value)*(value/self._ld)/2
        else :
            self._acc[key]+=(value+new_value)*dstep/2

        self._values[key]=new_value
        self._last_step[key]=self._step
        return new_value

    def _l2_regu(self,key):
        dstep=self._step-self._last_step[key]
        value=self._values[key]

        # l2-norm regularization
        new_value=value*math.exp(dstep*self._log_p)
        self._acc[key]+=value*(1-math.exp(dstep*self._log_p))/(1-self._p)

        self._values[key]=new_value
        self._last_step[key]=self._step
        return new_value

    def update_all(self):
        for key in self._values:
            self._new_value(key)
    def update_weights(self,key,delta): # 更新权重
        if key not in self._values :
            self._values[key]=0
            self._acc[key]=0
            self._last_step[key]=self._step
        else :
            self._new_value(key)

        self._values[key]+=delta

    def average(self): # 平均
        self._backup=dict(self._values)
        for k,v in self._acc.items():
            self._values[k]=self._acc[k]/self._step
    def unaverage(self):
        self._values=dict(self._backup)
        self._backup.clear()
    def save(self,filename):
        json.dump({k:v for k,v in self._values.items() if v!=0.0},
                open(filename,'w', encoding='utf-8'),
                ensure_ascii=False,indent=1)
    def load(self,filename):
        self._values.update(json.load(open(filename, encoding='utf-8')))
        self._last_step=None

    def get_value(self,key,default):
        if key not in self._values : return default
        if self._last_step==None : return self._values[key]
        return self._new_value(key)

    # 融入词典
    def merge_dict(self, filename):
        with open(filename, 'r', encoding='utf-8') as f:
            for word in f.readlines():
                word = word.strip()
                if len(word) <= 1:
                    continue

                # 正向
                make_word = ''
                for w in word:
                    make_word += w
                    if make_word not in self._words:
                        self._words[make_word] = 0
                self._words[word] = 1

                # 反向
                make_word = ''
                for w in reversed(word):
                    make_word = w + make_word
                    if make_word not in self._rwords:
                        self._rwords[make_word] = 0
                self._rwords[word] = 1

                # middle
                while len(word) >= 3:
                    self._mwords[word[:3]] = 0
                    word = word[1:]

    def max_match(self, sub):
        matched = 0
        for i in range(len(sub)):
            w = sub[:i+1]
            if w in self._words:
                if self._words[w] == 1:
                    matched = len(w)
            else:
                break
        return matched

    def reverse_max_match(self, sub):
        matched = 0
        make_word = ''
        for w in reversed(sub):
            make_word = w + make_word
            if make_word in self._rwords:
                if self._rwords[make_word] == 1:
                    matched = len(make_word)
            else:
                break
        return matched

class CWS :
    def __init__(self,penalty='no', words_list=None):
        self.weights=Weights(penalty=penalty)
        # 载入词典
        if words_list:
            self.weights.merge_dict(words_list)
            print('dict loaded')
            #print(self.weights._words)
            #matched = self.weights.max_match('前景是光明的')
            #print(matched)

    def gen_features(self,x): # 枚举得到每个字的特征向量
        for i in range(len(x)):
            #left3=x[i-3] if i-3 >=0 else '#'
            left2=x[i-2] if i-2 >=0 else '#'
            left1=x[i-1] if i-1 >=0 else '#'
            mid=x[i]
            right1=x[i+1] if i+1<len(x) else '#'
            right2=x[i+2] if i+2<len(x) else '#'
            #right3=x[i+3] if i+3<len(x) else '#'
            #triple = left1 + mid + right1
            features=['1'+mid,'2'+left1,'3'+right1,
                    '4'+left2+left1,'5'+left1+mid,'6'+mid+right1,'7'+right1+right2]

            #if mid+right1 in self.weights._words:
                #features.append('8B_' + mid + right1)
                #print(mid+right1)

            #if left1+mid in self.weights._rwords:
                #features.append('8E_' + left1 + mid)
                #print(left1+mid)

            #if left1+mid+right1 in self.weights._mwords:
                #features.append('8M_' + left1 + mid + right1)
                #print(left1+mid+right1)

            matched = self.weights.max_match(x[i:])
            if matched > 1:
                features.append('8B_' + mid + str(matched))
                #print(matched, x[i: i+matched])

            r_matched = self.weights.reverse_max_match(x[:i+1])
            if r_matched > 1:
                features.append('8E_' + mid + str(r_matched))
                #print(r_matched, x[i-r_matched+1: i+1])

            yield features

    def update(self,x,y,delta): # 更新权重
        for i,features in zip(range(len(x)),self.gen_features(x)):
            for feature in features :
                self.weights.update_weights(str(y[i])+feature,delta)
        for i in range(len(x)-1):
            self.weights.update_weights(str(y[i])+':'+str(y[i+1]),delta)

    def decode(self,x): # 类似隐马模型的动态规划解码算法
        # 类似隐马模型中的转移概率
        transitions = [ [self.weights.get_value(str(i)+':'+str(j),0) for j in range(4)]
                for i in range(4) ]

        # 类似隐马模型中的发射概率
        emissions = [ [sum(self.weights.get_value(str(tag)+feature,0) for feature in features)
            for tag in range(4) ] for features in self.gen_features(x)]

        # 类似隐马模型中的前向概率
        alphas = [[[e,None] for e in emissions[0]]]
        for i in range(len(x)-1) :
            alphas.append([max([alphas[i][j][0]+transitions[j][k]+emissions[i+1][k],j]
                                        for j in range(4))
                                        for k in range(4)])
        # 根据alphas中的“指针”得到最优序列
        alpha=max([alphas[-1][j],j] for j in range(4))
        i=len(x)
        tags=[]
        while i :
            tags.append(alpha[1])
            i-=1
            alpha=alphas[i][alpha[1]]
        return list(reversed(tags))

    def verbose(self, x):
        d = {0: 'B:', 1: 'M:', 2: 'E:', 3: 'S:'}
        emissions = [ [sum(self.weights.get_value(str(tag)+feature,0) for feature in features)
            for tag in range(4) ] for features in self.gen_features(x)]

        for tag in range(4):
            print(d[tag], end=' ')
            for i in range(len(x)):
                value = emissions[i][tag]
                print('{:8.2f}'.format(value), end=' ')
            print()

        print('-')
        for tag in range(4):
            print(d[tag], end=' ')
            for w in x:
                value = self.weights.get_value(str(tag) + '8B_' + w + '2', 0)
                print('{:8.2f}'.format(value), end=' ')
            print()

def load_example(words): # 词数组，得到x，y
    y=[]
    for word in words :
        if len(word)==1 : y.append(3)
        else : y.extend([0]+[1]*(len(word)-2)+[2])
    return ''.join(words),y

def dump_example(x,y) : # 根据x，y得到词数组
    cache=''
    words=[]
    for i in range(len(x)) :
        cache+=x[i]
        if y[i]==2 or y[i]==3 :
            words.append(cache)
            cache=''
    if cache : words.append(cache)
    return words

class Evaluator : # 评价
    def __init__(self):
        self.std,self.rst,self.cor=0,0,0
        self.start_time=time.time()
    def _gen_set(self,words):
        offset=0
        word_set=set()
        for word in words:
            word_set.add((offset,word))
            offset+=len(word)
        return word_set
    def __call__(self,std,rst): # 根据答案std和结果rst进行统计
        std,rst=self._gen_set(std),self._gen_set(rst)
        self.std+=len(std)
        self.rst+=len(rst)
        self.cor+=len(std&rst)
    def report(self):
        precision=self.cor/self.rst if self.rst else 0
        recall=self.cor/self.std if self.std else 0
        f1=2*precision*recall/(precision+recall) if precision+recall!=0 else 0
        print("历时: %.2f秒 答案词数: %i 结果词数: %i 正确词数: %i  准确率：%.4f 召回率：%.4f F值: %.4f"
                %(time.time()-self.start_time,self.std,self.rst,self.cor, precision, recall, f1))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--iteration',type=int,default=5, help='')
    parser.add_argument('--train',type=str, help='')
    parser.add_argument('--test',type=str, help='')
    parser.add_argument('--dev',type=str, help='')
    parser.add_argument('--predict',type=str, help='')
    parser.add_argument('--penalty',type=str, default='no')
    parser.add_argument('--result',type=str, help='')
    parser.add_argument('--model',type=str, help='')
    parser.add_argument('--dict',type=str, help='')
    parser.add_argument('--verbose',type=str, help='')
    args = parser.parse_args()

    # 训练
    if args.train:
        cws=CWS(penalty=args.penalty, words_list=args.dict)
        for i in range(args.iteration):
            print('\n**第 %i 次迭代:'%(i+1))
            #sys.stdout.flush()
            evaluator=Evaluator()
            lines = 0
            for sent in open(args.train, encoding='utf-8'):
                sent = sent.strip()
                if sent == '':
                    continue
                x,y=load_example(sent.split())
                z=cws.decode(x)
                evaluator(dump_example(x,y),dump_example(x,z))
                cws.weights._step+=1
                if z!=y :
                    cws.update(x,y,1)
                    cws.update(x,z,-1)

                # wk_debug
                lines += 1
                if lines % 2000 == 0:
                    print('trained sentences {}'.format(lines))

            evaluator.report()
            cws.weights.update_all()
            #cws.weights.average()

            if args.dev :
                evaluator=Evaluator()
                for sent in open(args.dev, encoding='utf-8') :
                    sent = sent.strip()
                    if sent == '':
                        continue
                    x,y=load_example(sent.split())
                    z=cws.decode(x)
                    evaluator(dump_example(x,y),dump_example(x,z))
                evaluator.report()
            #cws.weights.unaverage()

        cws.weights.average() # 最后再进行平均
        cws.weights.save(args.model)

    # 使用有正确答案的语料测试
    if args.test :
        cws=CWS(words_list=args.dict)
        cws.weights.load(args.model)
        evaluator=Evaluator()
        for sent in open(args.test, encoding='utf-8') :
            sent = sent.strip()
            if sent == '':
                continue
            x,y=load_example(sent.split())
            z=cws.decode(x)
            evaluator(dump_example(x,y),dump_example(x,z))
        evaluator.report()
    # 对未分词的句子输出分词结果
    if args.model and (not args.train and not args.test) :
        cws=CWS(words_list=args.dict)
        cws.weights.load(args.model)
        instream=open(args.predict, encoding='utf-8') if args.predict else sys.stdin
        outstream=open(args.result,'w', encoding='utf-8') if args.result else sys.stdout
        for sent in instream:
            sent = sent.strip()
            if sent == '':
                continue
            x,y=load_example(sent.split())
            z=cws.decode(x)
            print(' '.join(dump_example(x,z)),file=outstream)
            if args.verbose:
                cws.verbose(sent)


"""
Handles Mozilla Open Badge requests.
"""

from django.http import HttpResponse
from django.http import HttpResponseGone
from django.shortcuts import get_object_or_404
import json
from badge.models import Badge
from badge.models import Award
from media.processors import get_image
from p2pu_user.models import get_user
from .responses import create_assertion_from_template
from .responses import create_badge_from_template
from .responses import create_organisation_from_template
from .helpers import reverse_url
from p2pu_user.models import get_user

def get_assertion(request, uid):
    """
    Handle badge assertion requests.
    """

    award = get_object_or_404(Award, pk=uid)

    if award.ob_state=='REVOKED':
        return HttpResponseGone('{"revoked": true}', mimetype="application/json")

    badge = award.badge
    recipient_email = get_user(award.user_uri)['email']
    image = get_image(badge.image_uri)

    assertion = create_assertion_from_template(
        uid=uid,
        recipient_email=recipient_email,
        image=image['url'],
        evidence=award.evidence_url,
        issued_on=award.date_awarded,
        badge_id=badge.pk,
    )
    json_assertion = json.dumps(assertion)

    return HttpResponse(json_assertion, mimetype="application/json")

def get_badge(request, badge_id):
    """
    Handles badge requests.
    """

    badge = get_object_or_404(Badge, pk=badge_id)
    image = get_image(badge.image_uri)
    criteria = reverse_url('badge_view', args=[badge_id])

    badge = create_badge_from_template(
        name = badge.title,
        description = badge.description,
        image = image['url'],
        criteria = criteria,
    )
    json_badge = json.dumps(badge)

    return HttpResponse(json_badge, mimetype="application/json")

def get_organisation(request):
    """
    Handle organisation requests.
    """

    organisation = create_organisation_from_template()
    json_organisation = json.dumps(organisation)

    return HttpResponse(json_organisation, mimetype="application/json")


import requests
from dj_rest_auth.registration.views import SocialLoginView
from allauth.socialaccount.providers.kakao import views as kakao_view
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from allauth.socialaccount.models import SocialAccount
from json import JSONDecodeError
from django.http import JsonResponse
from rest_framework import status
from .models import User
from backend.secret import BASE_URL, REDIRECT_URL, KAKAO_REST_API_KEY
from django.views.decorators.csrf import csrf_exempt

# KAKAO
BASE_URL=BASE_URL
KAKAO_CALLBACK_URI=REDIRECT_URL

@csrf_exempt
def kakao_callback(request):
    client_id = KAKAO_REST_API_KEY
    code = request.GET.get('code')

    token_request = requests.get(f"https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id={client_id}&redirect_uri={KAKAO_CALLBACK_URI}&code={code}")
    token_response_json = token_request.json()
    error = token_response_json.get('error', None)
    if error is not None:
        raise JSONDecodeError('kakao token error',error,0)

    access_token = token_response_json.get('access_token')

    profile_request = requests.post("https://kapi.kakao.com/v2/user/me", headers={"Authorization": f"Bearer {access_token}"})
    profile_json = profile_request.json()

    kakao_account = profile_json.get('kakao_account')
    email = kakao_account.get('email', None)

    if email is None:
        return JsonResponse({'err_msg': 'failed to get email'}, status=status.HTTP_400_BAD_REQUEST)

    # Signin
    try:
        user = User.objects.get(email=email)
        social_user = SocialAccount.objects.get(user=user)
        if social_user == None:
            return JsonResponse({'message': 'kakao email is not registered'}, status=status.HTTP_400_BAD_REQUEST)

        # 기존 가입된 유저
        data = {'access_token': access_token,'code': code}
        accept = requests.post(BASE_URL + '/api/user/kakao/login/finish/', data=data)
        accept_status = accept.status_code
        if accept_status != 200:
            return JsonResponse({'message': 'kakao login failed'}, status=accept_status)
        accept_json = accept.json()
        return JsonResponse(accept_json)
    except User.DoesNotExist:
        # 신규 가입
        data = {'access_token': access_token,'code': code}
        accept = requests.post(BASE_URL + '/api/user/kakao/login/finish/', data=data)
        accept_status = accept.status_code
        if accept_status != 200:
            return JsonResponse({'message': 'kakao signup failed'}, status=accept_status)
        accept_json = accept.json()

        return JsonResponse(accept_json)

class KakaoLogin(SocialLoginView):
    adapter_class = kakao_view.KakaoOAuth2Adapter
    callback_url = KAKAO_CALLBACK_URI
    client_class = OAuth2Client


import csv
import torch
from PIL import Image
from torch.utils.data import Dataset

class CustomDataset(Dataset):
    def __init__(self, root_dir, annotations, transform):
        self.root_dir = root_dir
        self.annotations = annotations
        self.transform = transform

        self.default_images = []
        self.images = []
        self.labels = []
        self.__prepare_images()

    def __prepare_image_size(self):
        for img in self.default_images:
            resized_image = img.resize((1280, 720))
            self.images.append(resized_image)

    def __prepare_images(self):
        with open(self.annotations, 'r') as file:
            csv_annotations = csv.reader(file)
            for item in list(csv_annotations)[1:]:
                file_path = item[0]
                label = item[-1]
                image = Image.open(file_path).convert('RGB')
                self.default_images.append(image)
                self.labels.append(int(label))
        self.__prepare_image_size()

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

    def __getitem__(self, index):
        image = self.images[index]
        label = self.labels[index]
        if self.transform:
            image = self.transform(image)

        return image, torch.tensor(label)


from functools import wraps
from starlette.requests import HTTPConnection
from starlette.authentication import (
    AuthenticationBackend,
    AuthenticationError,
    SimpleUser,
    AuthCredentials,
    UnauthenticatedUser,
)

from ladsetal import settings

class User(SimpleUser):
    is_authed = True

    def __init__(self, username: str, discord_id: int):
        super().__init__(username)
        self.discord_id = discord_id

class LUnauthenticatedUser(UnauthenticatedUser):
    is_authed = False

def wrap_result_auth(f):
    @wraps(f)
    async def inner(*args, **kwargs):
        r = await f(*args, **kwargs)
        if r is None:
            return AuthCredentials(), LUnauthenticatedUser()
        return r

    return inner

class TokenAuthBackend(AuthenticationBackend):
    @wrap_result_auth
    async def authenticate(self, request: HTTPConnection):
        if "discord_id" not in request.session:
            request.session.pop("user", None)
            return

        user = request.session.get("user")
        if not user:
            uid = request.session["discord_id"]

            if uid not in settings.LADS:
                return
            user = {
                "discord_id": uid,
                "username": request.session["discord_username"],
            }
            request.session["user"] = user

        username, discord_id = (
            user["username"],
            user["discord_id"],
        )

        creds = ["authenticated"]

        return AuthCredentials(creds), User(username, discord_id)

def can_edit(request):
    return request.user.is_authenticated


from django.contrib.auth import authenticate
from django.http import JsonResponse, HttpResponse, HttpResponseNotAllowed

from firebase.auth import InvalidIdToken, NoTokenProvided, FirebaseUserDoesNotExist
from . import queries, utils
from .models import Location, LocationFilter
from .utils import paginator

def locations(request):
    # Authenticating user
    try:
        user = authenticate(request)
        if not user:
            raise NoTokenProvided()
    except (InvalidIdToken, NoTokenProvided, FirebaseUserDoesNotExist):
        return HttpResponse(status=401,
                            reason="Unauthorized: Operation needs authentication")

    if request.method == 'POST':

        return handle_create_location(request, user)

    elif request.method == 'GET':

        return handle_get_locations(request, user)

    else:

        return HttpResponseNotAllowed(['POST', 'GET'])

def crud_location(request, uuid):
    # Authenticating user
    try:
        user = authenticate(request)
        if not user:
            raise NoTokenProvided()
    except (InvalidIdToken, NoTokenProvided, FirebaseUserDoesNotExist):
        return HttpResponse(status=401,
                            reason="Unauthorized: Operation needs authentication")

    if request.method == 'GET':

        return handle_get_location(request, uuid, user)

    elif request.method == 'PATCH':

        return handle_patch_location(request, uuid, user)

    elif request.method == 'DELETE':

        return handle_delete_location(request, uuid, user)

    else:

        return HttpResponseNotAllowed(['GET', 'PATCH', 'DELETE'])

def stats_location(request, uuid):
    # Authenticating user
    try:
        user = authenticate(request)
        if not user:
            raise NoTokenProvided()
    except (InvalidIdToken, NoTokenProvided):
        return HttpResponse(status=401,
                            reason="Unauthorized: Operation needs authentication")

    if request.method == 'GET':

        return handle_get_stats_location(request, uuid, user)

    else:

        return HttpResponseNotAllowed(['GET'])

# Auxiliary functions for the Views

def handle_create_location(request, user):
    # Checking permissions
    if user.has_perm('locations.add_location'):

        # Unserializing
        try:
            unserialized_location = utils.decode_location_from_json(request.body, False)

            if not unserialized_location.get("name") or not unserialized_location.get("description"):
                return HttpResponse(status=400, reason="Bad Request: Name and Description must be provided")

        except utils.InvalidJSONData:
            return HttpResponse(status=400, reason="Bad Request: Malformed JSON object provided")

        # Executing query
        try:

            created_location = queries.create_location(unserialized_location, user.id)

        except utils.NotAValidImage:
            return HttpResponse(status=400, reason="Bad Request: Invalid image provided")

        # Serializing
        serialized_location = utils.encode_location_to_json([created_location])[0]
        return JsonResponse(serialized_location, safe=False, status=201)

    else:

        return HttpResponse(status=403,
                            reason="Forbidden: Current user does not have the permission"
                                   " required to add a location")

def handle_get_locations(request, user):
    # Checking permissions (possibly needs the permission to see if the badge is related to this user)
    if user.has_perm('locations.view_location'):

        f = LocationFilter(request.GET, queryset=Location.objects.all()).qs

        response = paginator(request, f)

        return JsonResponse(utils.encode_location_to_json(response), safe=False)

    else:
        return HttpResponse(status=403,
                            reason="Forbidden: Current user does not have the permission"
                                   " required to view locations")

def handle_get_location(request, uuid, user):
    # Checking permissions
    if user.has_perm('locations.view_location'):

        # Executing the query
        try:
            selected_location = queries.get_location_by_uuid(uuid)
        except Location.DoesNotExist:
            return HttpResponse(status=400, reason="Bad request: No Location with that UUID")

        # Serializing
        serialized_location = utils.encode_location_to_json([selected_location])[0]

        return JsonResponse(serialized_location, safe=False)

    else:

        return HttpResponse(status=403,
                            reason="Forbidden: Current user does not have the permission"
                                   " required to view this location")

def handle_patch_location(request, uuid, user):
    try:
        location_to_patch = queries.get_location_by_uuid(uuid)
    except Location.DoesNotExist:
        return HttpResponse(status=404, reason="Not Found: No Location by that UUID")

    # Checking if it's admin or the manager that created the location
    if not user.has_perm('locations.change_location') and location_to_patch.manager.user != user:
        return HttpResponse(status=403,
                            reason="Forbidden: Current user does not have the permission"
                                   " required to delete this location")

    # Unserializing
    try:
        unserialized_patch_location = utils.decode_location_from_json(request.body,
                                                                      user.has_perm('locations.change_location'))
    except utils.InvalidJSONData:
        return HttpResponse(status=400, reason="Bad Request: Malformed JSON object provided")

    # Executing query
    try:
        updated_location = queries.patch_location_by_uuid(uuid, unserialized_patch_location)

    except utils.NotAValidImage as e:
        return HttpResponse(status=400, reason=f"Bad Request: Invalid image provided {e}")

    # Serializing
    serialized_location = utils.encode_location_to_json([updated_location])[0]
    return JsonResponse(serialized_location, safe=False)

def handle_delete_location(request, uuid, user):
    # Checking permissions
    try:
        location = queries.get_location_by_uuid(uuid)

        # Checking if it's admin or the manager that created the location
        if not user.has_perm('locations.delete_location') and location.manager.user != user:
            return HttpResponse(status=403,
                                reason="Forbidden: Current user does not have the permission"
                                       " required to delete this location")

    except Location.DoesNotExist:
        return HttpResponse(status=404, reason="Not Found: No Location by that UUID")

    # Executing query
    success = queries.delete_location_by_uuid(uuid)
    if success:
        return HttpResponse(status=200)
    else:
        return HttpResponse(status=400, reason="Bad request: Failed to delete")

def handle_get_stats_location(request, uuid, user):
    try:
        location = queries.get_location_by_uuid(uuid)
    except Location.DoesNotExist:
        return HttpResponse(status=404, reason="Not Found: No Location by that UUID")

    # Checking if it's admin or the manager that created the location
    if not user.has_perm('locations.view_stats') and location.manager.user != user:
        return HttpResponse(status=403,
                            reason="Forbidden: Current user does not have the permission"
                                   " required to view statistics about this location")

    # Executing the query
    statistics = queries.get_location_stats(uuid)

    # Serializing
    # serialized_statistics = utils.encode_statistics_to_json(statistics)

    return JsonResponse(statistics, safe=False)


import logging
from common.common_fun import Common,TimeoutException
from common.desired_caps import appium_desired
from selenium.webdriver.common.by import By

class Search(Common):
    bn_search = (By.ID, 'com.clickcoo.yishuobaobao:id/ibn_serach')  #搜索按钮
    et_serachkey = (By.ID, 'com.clickcoo.yishuobaobao:id/et_serachkey')  #搜索输入框
    btn_searchstart = (By.ID, 'com.clickcoo.yishuobaobao:id/btn_searchstart')  #搜索
    heartstation_play = (By.ID, 'com.clickcoo.yishuobaobao:id/cb_heartstation_play')  #播放
    btn_searchusers = (By.ID, 'com.clickcoo.yishuobaobao:id/btn_searchusers')  #用户
    userlayout = (By.ID, 'com.clickcoo.yishuobaobao:id/layout_userlayout')  #用户列表
    btn_searchalbum = (By.ID, 'com.clickcoo.yishuobaobao:id/btn_searchalbum')  #专辑
    albumlayout = (By.ID, 'com.clickcoo.yishuobaobao:id/layout_albumlayout')  #专辑列表

    def search_action(self,keyword):
        logging.info('===搜索====')
        self.find_element(self.bn_search).click()
        self.wait(2,self.et_serachkey).send_keys(keyword)
        self.wait(1,self.btn_searchstart).click()
        return True

    def check_voice(self):
         logging.info('====check search_voice======')
         try:
             self.waits(8,self.heartstation_play)[0].click()
         except TimeoutException:
             logging.error('搜索声音失败！')
             self.getScreenShot('搜索声音失败！')
             return False
         else:
             logging.info('搜索声音成功')
             return True

    def check_user(self):
        self.wait(1,self.btn_searchusers).click()
        logging.info('====check search_user======')
        try:
            self.waits(3,self.userlayout)[0]
        except  TimeoutException:
            logging.error('搜索用户失败！')
            self.getScreenShot('搜索用户失败！')
            return False
        else:
            logging.info('搜索用户成功')
            return True

    def check_album(self):
        self.find_element(self.btn_searchalbum).click()
        logging.info('====check search_album======')
        try:
            self.waits(3,self.albumlayout)[0]
        except  TimeoutException:
            logging.error('搜索专辑失败！')
            self.getScreenShot('搜索专辑失败！')
            return False
        else:
            logging.info('搜索专辑成功')
            return True

if __name__ == '__main__':
    driver=appium_desired()
    s=Search(driver)
    s.search_action('a')
    s.check_voice()
    s.check_user()
    s.check_album()


#!/usr/bin/env python3

import argparse
from math import sqrt
import os.path
import sys

sys.path.append('../lib')
import project

# this is a one-off script for debugging.  The intention is to strip
# down a larger data set to something small for testing/debugging.  It
# computes the camera distance from the reference point and can delete
# anything further than some threshold.  Essentially leaving just a
# smaller number of images clustered around the center of the data
# set.  This isn't generally useful for anything other than debugging
# and it's not very general for that purpose either.

parser = argparse.ArgumentParser(description='Load the project\'s images.')
parser.add_argument('project', help='project directory')
parser.add_argument('--delete-further-than', type=float, help='delete images furhter than this distance from center')
args = parser.parse_args()
# print args

proj = project.ProjectMgr(args.project)
proj.load_image_info()

def robust_delete(path):
    try:
        os.remove(path)
    except:
        print('cannot remove:', path)

dist_list = []
for image in proj.image_list:
    print(image.name, image.camera_pose)
    if not image.camera_pose == None:
        ned = image.camera_pose['ned']
        dist = sqrt(ned[0]*ned[0] + ned[1]*ned[1])
    else:
        dist = 99999999
    dist_list.append( [ dist, image.name ] )

by_dist = sorted( dist_list, key=lambda fields: fields[0])
if not args.delete_further_than:
    # just print the list and exit
    for line in by_dist:
        print(line)
else:
    count_save = 0
    count_delete = 0
    for line in by_dist:
        if line[0] < args.delete_further_than:
            count_save += 1
        else:
            count_delete += 1
    print('save:', count_save, 'delete:', count_delete)
    result=input('Permanently delete ' + str(count_delete) + ' images from project: ' + args.project + '? (y/n):')
    if result == 'y' or result == 'Y':
        print('ok, will delete them')
    else:
        print('quitting without deleting any images')
        quit()

    for line in by_dist:
        if line[0] >= args.delete_further_than:
            dist = line[0]
            name = line[1]
            base, ext = os.path.splitext(name)
            print(base, ext)
            path = os.path.join(args.project, 'Images')
            robust_delete(os.path.join(path, base + '.desc.npy'))
            robust_delete(os.path.join(path, base + '.feat'))
            robust_delete(os.path.join(path, base + '.info'))
            robust_delete(os.path.join(path, base + '.match'))
            robust_delete(os.path.join(path, name))


from rest_framework import serializers
from .models import thread, message

from users.serializers import prefetch_user_serializer

class create_thread_serializer(serializers.ModelSerializer):
    class Meta:
        model = thread
        fields = '__all__'

class user_threads_serializer(serializers.ModelSerializer):
    user1 = prefetch_user_serializer()
    user2 = prefetch_user_serializer()
    class Meta:
        model = thread
        fields = ['pk', 'user1', 'user2','last_viewed', 'has_unread']

class thread_messages(serializers.ModelSerializer):
    messages = serializers.SerializerMethodField(read_only = True)
    class Meta:
        model = thread
        fields = ['pk', 'last_viewed', 'has_unread', 'user1', 'user2', 'messages']

class message_serializer(serializers.ModelSerializer):
    class Meta:
        model = message
        fields = '__all__'


import os
import sys
from collections import defaultdict, deque

with open(os.path.join(os.path.dirname(__file__), 'input.txt')) as f:
    program = [int(x) for x in f.read().split(",")]

class VM:
    def __init__(self, program):
        self.pointer = 0
        self.program = defaultdict(int, enumerate(program))
        self.input = []
        self.output = []
        self.done = False
        self.base = 0

        self.op_params = {
            1: 3, 2: 3, 3: 1, 4: 1, 5: 2, 6: 2, 7: 3, 8: 3, 9: 1
        }

    def add_input(self, input):
        self.input.append(input)
        self.run()

    def run(self):
        while True:
            opcode = self.program[self.pointer]
            code = opcode % 100
            if code == 99:
                self.done = True
                return
            if code == 3 and len(self.input) == 0:
                return

            num_params = self.op_params[code]
            modes = [(opcode // 10**i) %
                     10 for i in range(2, 2+num_params)]
            args = [self.program[self.pointer+x]
                    for x in range(1, 1+num_params)]
            reads = [(self.program[a], a, self.program[a+self.base])[m]
                     for a, m in zip(args, modes)]
            writes = [(a, None, a+self.base)[m]
                      for a, m in zip(args, modes)]

            self.pointer += num_params + 1
            if code == 1:
                self.program[writes[2]] = reads[0] + reads[1]
            elif code == 2:
                self.program[writes[2]] = reads[0] * reads[1]
            elif code == 3:
                self.program[writes[0]] = self.input.pop(0)
            elif code == 4:
                self.output.append(reads[0])
            elif code == 5:
                if reads[0] != 0:
                    self.pointer = reads[1]
            elif code == 6:
                if reads[0] == 0:
                    self.pointer = reads[1]
            elif code == 7:
                self.program[writes[2]] = int(reads[0] < reads[1])
            elif code == 8:
                self.program[writes[2]] = int(reads[0] == reads[1])
            elif code == 9:
                self.base += reads[0]

vms = [VM(program) for _ in range(50)]
qs = defaultdict(deque)
qnat = deque([], maxlen=1)

for i, vm in enumerate(vms):
    vm.add_input(i)
    vm.add_input(-1)

part1_done = False
prevY = None
while True:
    idle = True
    for ivm, vm in enumerate(vms):
        if vm.done is True:
            assert False
            continue
        # queue prev messages
        out = vm.output[:]
        vm.output = []
        for offset in range(len(out)//3):
            msg = out[offset*3:(offset+1)*3]
            to, x, y = msg
            if to == 255:
                if part1_done is False:
                    print('part1: ', y)
                    part1_done = True
                qnat.append((x, y))
            else:
                qs[to].append((x, y))
                idle = False
        # feed quequed messages
        if qs[ivm]:
            idle = False
            x, y = qs[ivm].popleft()
            vm.add_input(x)
            vm.add_input(y)
        else:
            vm.add_input(-1)

    if idle:
        assert len(qnat) == 1
        if all(len(q) == 0 for q in qs.values()):
            x, y = qnat.popleft()
            if y == prevY:
                print('part2: ', y)
                sys.exit(0)
            prevY = y
            vms[0].add_input(x)
            vms[0].add_input(y)


from flask import Flask, request, url_for, redirect, render_template
import pickle
import json
import config
from Project_app import utils
import numpy as np

app = Flask(__name__, template_folder='./templates', static_folder='./static')

with open(config.MODEL_FILE_PATH,"rb") as f:
            model=pickle.load(f)
@app.route('/')

def hello_world():
    return render_template('login.html')

@app.route('/predict', methods=['POST','GET'])
def predict():
    print([x for x in request.form.values()])
    features = [x for x in request.form.values()]
    age=features[0]
    sex=features[1]
    bmi=features[2]
    children=features[3]
    smoker=features[4]
    region=features[5]
    print(features)
    md=utils.MedicalInsurance(age,sex,bmi,children,smoker,region)
    # final = np.array(features).reshape((1,6))
    # print(final)
    # pred = model.predict(final)[0]
    # print(pred)
    pred=md.get_predicted_charges()

    return render_template('op.html', pred='Expected amount is {}'.format(pred))

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=False)

app.run()


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from fastdtw import fastdtw
from scipy.spatial import distance
from scipy.optimize import curve_fit
import WearableSignals
import IRCameraSignals
import copy
from filterpy.kalman import KalmanFilter
import statistics

class CurveFitting:

#Joints array
    arrJ1 = []
    arrJ2 = []
    arrJ3 = []
    arrJ4 = []
    arrJ5 = []
    arrD =[]

    arrT = []#Timestamp of Kinect
    arrT2 = []#Timestamp of Accelerometer
    arrT3 = []#Timestamp of Gyroscope
    arrT4 = []#time of stroke start from 0 to end of kinect
    arrT5 = []#time of stroke start from 0 to end of Accelerometer
    arrT6 = []#time of stroke start from 0 to end of Gyroscope
    arrT7 = []

    SensorStroke =[]
    KinectStroke = []

    acc =[]

    gyro = []

    def FilteringData(self,DataArray):
        #First construct the object with the required dimensionality.
        f = KalmanFilter(dim_x=len(DataArray), dim_z=3)
        f.x = np.array(DataArray)# Real data
        f.F = np.array([[1., 1.],[0., 1.]])#Define the state transition matrix
        f.H = np.array([[1., 0.]])#Define the measurement function
        f.P = np.array([[1000., 0.],[0., 1000.]])#Define the covariance matrix
        f.R = 5 #Assign the measurement noise.
        from filterpy.common import Q_discrete_white_noise
        f.Q = Q_discrete_white_noise(dim=len(DataArray), dt=0.1, var=0.13) #Assign the process noise
        for i in range(100): #IterationNum
            f.predict()
            f.update()

        return f.x

    def SignalsIntake(self,S_Stroke,K_Stroke):
        self.SensorStroke = S_Stroke
        self.KinectStroke = K_Stroke
        self.player_id=player_id

        for line in range(len(self.KinectStroke)):
                self.arrJ1.append(float(line[0]))
                self.arrJ1.append(float(line[1]))
                self.arrJ1.append(float(line[2]))
                self.arrJ2.append(float(line[3]))
                self.arrJ2.append(float(line[4]))
                self.arrJ2.append(float(line[5]))
                self.arrJ3.append(float(line[6]))
                self.arrJ3.append(float(line[7]))
                self.arrJ3.append(float(line[8]))
                self.arrJ4.append(float(line[9]))
                self.arrJ4.append(float(line[10]))
                self.arrJ4.append(float(line[11]))
                self.arrJ5.append(float(line[12]))
                self.arrJ5.append(float(line[13]))
                self.arrJ5.append(float(line[14]))
                self.arrT.append(float(line[15]))

        for y in range(len(self.SensorStroke)):
            for j in range(5):
                if j[0] == "acc":
                    self.acc.append(float(y[1]))
                    self.acc.append(float(y[2]))
                    self.acc.append(float(y[3]))
                    self.arrT2.append(float(y[4]))

                elif j[0] =="gyro":
                    self.gyro.append(float(y[1]))
                    self.gyro.append(float(y[2]))
                    self.gyro.append(float(y[3]))
                    self.arrT3.append(float(y[4]))

        arrT4.insert(0, 0)
        arrT5.insert(0, 0)
        arrT6.insert(0, 0)

        # Time from 0 to end of stroke
        for t in range(arrT.__len__() - 1):
            timeS = (arrT[t + 1] - arrT[0]) / 1000
            arrT4.insert(t + 1, timeS)

        for t2 in range(arrT2.__len__() - 1):
            timeS2 = (arrT2[t2 + 1] - arrT2[0]) / 1000
            arrT5.insert(t2 + 1, timeS2)

        for t3 in range(arrT3.__len__() - 1):
            timeS3 = (arrT3[t2 + 1] - arrT3[0]) / 1000
            arrT6.insert(t2 + 1, timeS3)

# The magnitude of each joint x,y,z
    arrM1 = []
    arrM2 = []
    arrM3 = []
    arrM4 = []
    arrM5 = []
    arrM6 = []
    arrM7 = []

    # put every joint/acc/gyro data magnitude in array
    def GetMagnitude(self):

        for a in range(0,len(self.arrJ1),3):#Mag for kinect

            b = np.array([self.arrJ1[a],self.arrJ1[a+1],self.arrJ1[a+2]], dtype=float)
            self.arrM1.append(np.linalg.norm(b))
            b3 = np.array([self.arrJ2[a],self.arrJ2[a+1],self.arrJ2[a+2]], dtype=float)
            self.arrM2.append(np.linalg.norm(b3))
            b5 = np.array([self.arrJ3[a],self.arrJ3[a+1],self.arrJ3[a+2]], dtype=float)
            self.arrM3.append(np.linalg.norm(b5))
            b6 = np.array([self.arrJ4[a],self.arrJ4[a+1],self.arrJ4[a+2]], dtype=float)
            self.arrM4.append(np.linalg.norm(b6))
            b7 = np.array([self.arrJ5[a],self.arrJ5[a+1],self.arrJ5[a+2]], dtype=float)
            self.arrM5.append(np.linalg.norm(b7))

        for a2 in range(0,len(self.acc),3):#Mag for acc
            b1 = np.array([self.acc[a2],self.acc[a2+1],self.acc[a2+2]], dtype=float)
            self.arrM6.append(np.linalg.norm(b1))

        for a3 in range(0,len(self.gyro),3):#Mag for gyro
            b2 = np.array([self.gyro[a2],self.gyro[a2+1],self.gyro[a2+2]], dtype=float)
            self.arrM6.append(np.linalg.norm(b2))

    #Fitting function
    def func(self,x, a, b):
        return a*np.exp(b*x)

    def func2(self,x, a, b, c):
        return a * np.exp(-b * x) + c

    def func3(self,x, a, b):
        return a*x+b

    #Initial guess for the pramameters
    initialGuess = [1.0,1.0]

    #Curve fitting for acc/gyro/kinect joint

    def StartCurveFit(self,xData1,yData1,xData2,yData2,xData3,yData3):

        xData = np.array(xData1)
        yData = np.array(yData1)
        #Perform the curve fit
        popt1, pcov1 = curve_fit(func2, xData, yData)

        xData = np.array(xData2)
        yData = np.array(yData2)
        # Perform the curve fit
        popt2, pcov2 = curve_fit(func2, xData, yData)

        xData = np.array(xData3)
        yData = np.array(yData3)
        # Perform the curve fit
        popt3, pcov3 = curve_fit(func2, xData, yData)

        Merg =[]
        for i in range(0,len(popt1),int(len(popt1)/len(popt3))):
            Merg.append(statistics.mean([popt1[i],popt2[i%len(popt2)],popt3[i%len(popt3)]]))

        return  Merg


from django.urls import path
from . import views

app_name="meal"
urlpatterns = [
    path('index/', views.index, name="index"),
    path('bmi/', views.bmi, name="bmi"),
    path('database/', views.food_Database, name="database"),
    path('detail/<mpk>', views.detail, name="detail"),
]


#-*- coding:utf-8 -*-
import hashlib
import json
import random
import time
import urllib.parse as urlparse
import xml.etree.ElementTree as ET

from tornado.gen import coroutine
from tornado.log import app_log
from tornado.web import MissingArgumentError, RequestHandler

from .. import config
from ..db.model import User
from ..service.async_wx import CustomMessage, Material, WXUser
from ..service.pic_vcode import VerifyCode
from ..tool import get_oauth_url, G
from ..wx_msg import Custom, TPL, XML

def need_openid(func):
    @coroutine
    def wrapper(self, *args, **kwargs):
        openid = self.get_cookie('openid')
        if not openid:
            try:
                code = self.get_argument('code')
                openid = yield WXUser.oauth_get_openid(code)
                if not openid:
                    raise Exception('获取openid失败')
            except MissingArgumentError:
                app_log.debug('REQUEST %s', self.request.__dict__)
                current_url = self.request.uri.split(config.URL_PREFIX)[1]
                self.redirect(get_oauth_url(current_url))
                return
        self.set_cookie('openid', openid)
        self.openid = openid
        app_log.debug('FUNC: %s', func)
        yield func(self, *args, **kwargs)
    return wrapper

def need_reg(func):
    @coroutine
    def wrapper(self, *args, **kwargs):
        user = G.db.query(User).filter(User.openid==self.openid).one_or_none()
        if not user:
            if self.request.method in ("GET", "HEAD"):
                url = config.REG_URL
                if "?" not in url:
                    if urlparse.urlsplit(url).scheme:
                        next_url = self.request.full_url()
                    else:
                        next_url = self.request.uri
                    url += "?" + urlparse.urlencode(dict(next=next_url))
                self.redirect(url)
                return
            raise HTTPError(403)
        self.user = user
        yield func(self, *args, **kwargs)
    return wrapper

class BaseHandler(RequestHandler):
    def prepare(self, *args, **kargs):
        self.xsrf_token

    def check_xsrf_cookie(self):
        if not hasattr(self, 'exempt_csrf'):
            super().check_xsrf_cookie()

    @coroutine
    def get_openid(self):
        openid = self.get_cookie('openid')
        if not openid:
            try:
                code = self.get_argument('code')
                openid = yield WXUser.oauth_get_openid(code)
                if not openid:
                    raise Exception('获取openid失败')
                self.set_cookie('openid', openid)
            except MissingArgumentError:
                app_log.debug('REQUEST %s', self.request.__dict__)
                current_url = self.request.uri.split(config.URL_PREFIX)[1]
                self.redirect(get_oauth_url(current_url))
                return
        app_log.info('FINAL OPENID: %s', openid)
        return openid

class RootHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)
        self.exempt_csrf = True

    def get(self):
        signature = self.get_argument('signature')
        timestamp = self.get_argument('timestamp')
        nonce = self.get_argument('nonce')
        echostr = self.get_argument('echostr')
        l = sorted([timestamp, nonce, config.TOKEN])
        s = hashlib.sha1()
        for t in l:
            s.update(t.encode('utf-8'))
        dig = s.hexdigest()
        if signature == dig:
            return self.write(echostr)
        else:
            app_log.debug(u'check signature fail: %s vs %s' %(signature, dig))
            return self.write('')

    @coroutine
    def post(self):
        root = ET.fromstring(self.request.body)
        xml_type = root.find('MsgType').text
        if xml_type == 'text':
            yield self.text_handler(root)
        elif xml_type == 'event':
            return self.event_handler(root)
        else:
            return self.write('')

    @coroutine
    def text_handler(self, root):
        openid = root.find('FromUserName').text
        serverid = root.find('ToUserName').text
        send_text = root.find('Content').text
        if send_text == '图片':
            mediaid = yield Material.get()
            app_log.info('mediaid %s', mediaid)
            ret = {'to_user': openid, 'from_user': serverid, 'media_id': mediaid}
            app_log.debug(ret)
            return self.render('xml/img.xml', **ret)
        else:
            reply_text = '''1、输入“图片”返回默认图片。\n2、这是一个百度链接。'''
            ret = {'to_user': openid, 'from_user': serverid, 'content': reply_text}
            return self.render('xml/text.xml', **ret)

    def event_handler(self, root):
        app_log.info('EVENT')
        openid = root.find('FromUserName').text
        serverid = root.find('ToUserName').text
        event = root.find('Event').text
        if event == 'subscribe':
            reply_text = '欢迎关注上海鹏弈程信息科技有限公司'
            ret = {'to_user': openid, 'from_user': serverid, 'content': reply_text}
            return self.render('xml/text.xml', **ret)
        elif event == 'CLICK':
            key = root.find('EventKey').text
            if key == 'reply_news':
                ret = XML.example_news(openid, serverid)
                #app_log.info(self.render_string('xml/news.xml', **ret))
                return self.render('xml/news.xml', **ret)
            elif key == 'reply_tpl':
                data = {
                    'date': '2015年6月16日 10:20',
                    'type': '收入/支出500元',
                    'balance': '300元',
                    'summary': '手机银行',
                }
                TPL.send_trade_detail(openid, data)
                return self.write('')
            elif key == 'custom_msg':
                articles = Custom.example_articles()
                CustomMessage.news(openid, articles)
        else:
            return self.write('')

class StaticTPLHandler(BaseHandler):
    #@coroutine
    def get(self, path):
        #openid = yield self.get_openid()
        #app_log.debug('OPENID %s', openid)
        self.render(path)

    def post(self):
        app_log.info('enter this')

class SendVcodeHandler(BaseHandler):
    def post(self):
        cellphone_number = self.get_argument('cellphone')
        client_now = self.get_argument('now')
        app_log.info('params: %s', self.request.body)
        vcode = str(random.randint(100000, 999999))
        app_log.info('VCODE %s', vcode)
        self.set_secure_cookie('vcode', vcode, expires=int(client_now) / 1000 + 120)
        self.write({'success': True})

class RefreshPicVcodeHandler(BaseHandler):
    @coroutine
    def get(self):
        openid = yield self.get_openid()
        generator = VerifyCode()
        img, code = generator.createCodeImage()
        img.save(config.MEDIA_PATH + '/pic_vcode/' + openid + '.jpg','JPEG')
        self.write({'success': True, 'url': config.MEDIA_URL + '/pic_vcode/' + openid + '.jpg'})

class UploadImgHandler(BaseHandler):
    @coroutine
    def post(self):
        openid = yield self.get_openid()
        imgs = self.request.files.get('photo', None)
        if not imgs:
            return self.write({'success': False})
        img = imgs[0]
        app_log.debug(img.keys())
        dirname = self.get_argument('dirname')
        path = config.MEDIA_PATH + '/' + dirname + '/'
        with open(path + openid + '.jpg', 'wb') as f:
            f.write(img['body'])
        self.write({'success': True})

#from tornado.httpclient import AsyncHTTPClient
from tornado.gen import coroutine
class TestHandler(BaseHandler):
    @coroutine
    def get(self, action):
        if action == 'tpl_msg':
            openid = 'oSDTiwq1vFtLARyBeBGhRpNeXczA'
            data = {
                'date': '2015年6月16日 10:20',
                'type': '收入/支出500元',
                'balance': '300元',
                'summary': '手机银行',
            }
            TPL.send_trade_detail(openid, data)


from recognition import E2E
import cv2
from pathlib import Path
import argparse
import time
from datetime import datetime
import json
import requests

class license:
    def __init__(self, license_number):
        self.license_number = license_number

def send_license_number_plate_checkin(license_number):
    LINK = 'http://127.0.0.1:8000/validate-license/'
    params = json.dumps(license(license_number).__dict__)
    connection = requests.post(url = LINK, data= params)
    print(connection.json())

def send_license_number_plate_checkout(license_number):
    LINK = 'http://127.0.0.1:8000/validate-license/check/out'
    params = json.dumps(license(license_number).__dict__)
    connection = requests.post(url = LINK, data= params)
    print(connection.json())

i = 1
model = E2E()
while(i < 29):
    path = './images/'+str(i)+'.jpg'
    i=i+1
    try:
        print('====================\n')
        img = cv2.imread(path)
        plate = model.predict(img)
        plate = plate.replace("-","")
        print(plate)
        time_send = datetime.now()
        if plate == None:
            print(str(time_send) + " --> NO CAR")
        else:
            send_license_number_plate_checkin(plate)
            print(str(time_send) + " -->  " + str(plate))
    except :
        continue

    time.sleep(3)


"""Node recency post-processor."""
from datetime import datetime
from typing import List, Optional, Set

import numpy as np
import pandas as pd

from llama_index.bridge.pydantic import Field
from llama_index.postprocessor.types import BaseNodePostprocessor
from llama_index.schema import MetadataMode, NodeWithScore, QueryBundle
from llama_index.service_context import ServiceContext

# NOTE: currently not being used
# DEFAULT_INFER_RECENCY_TMPL = (
#     "A question is provided.\n"
#     "The goal is to determine whether the question requires finding the most recent "
#     "context.\n"
#     "Please respond with YES or NO.\n"
#     "Question: What is the current status of the patient?\n"
#     "Answer: YES\n"
#     "Question: What happened in the Battle of Yorktown?\n"
#     "Answer: NO\n"
#     "Question: What are the most recent changes to the project?\n"
#     "Answer: YES\n"
#     "Question: How did Harry defeat Voldemort in the Battle of Hogwarts?\n"
#     "Answer: NO\n"
#     "Question: {query_str}\n"
#     "Answer: "
# )

# def parse_recency_pred(pred: str) -> bool:
#     """Parse recency prediction."""
#     if "YES" in pred:
#         return True
#     elif "NO" in pred:
#         return False
#     else:
#         raise ValueError(f"Invalid recency prediction: {pred}.")

class FixedRecencyPostprocessor(BaseNodePostprocessor):
    """Recency post-processor.

    This post-processor does the following steps:

    - Decides if we need to use the post-processor given the query
      (is it temporal-related?)
    - If yes, sorts nodes by date.
    - Take the first k nodes (by default 1), and use that to synthesize an answer.

    """

    service_context: ServiceContext
    top_k: int = 1
    # infer_recency_tmpl: str = Field(default=DEFAULT_INFER_RECENCY_TMPL)
    date_key: str = "date"

    @classmethod
    def class_name(cls) -> str:
        return "FixedRecencyPostprocessor"

    def _postprocess_nodes(
        self,
        nodes: List[NodeWithScore],
        query_bundle: Optional[QueryBundle] = None,
    ) -> List[NodeWithScore]:
        """Postprocess nodes."""
        if query_bundle is None:
            raise ValueError("Missing query bundle in extra info.")

        # sort nodes by date
        node_dates = pd.to_datetime(
            [node.node.metadata[self.date_key] for node in nodes]
        )
        sorted_node_idxs = np.flip(node_dates.argsort())
        sorted_nodes = [nodes[idx] for idx in sorted_node_idxs]

        return sorted_nodes[: self.top_k]

DEFAULT_QUERY_EMBEDDING_TMPL = (
    "The current document is provided.\n"
    "----------------\n"
    "{context_str}\n"
    "----------------\n"
    "Given the document, we wish to find documents that contain \n"
    "similar context. Note that these documents are older "
    "than the current document, meaning that certain details may be changed. \n"
    "However, the high-level context should be similar.\n"
)

class EmbeddingRecencyPostprocessor(BaseNodePostprocessor):
    """Recency post-processor.

    This post-processor does the following steps:

    - Decides if we need to use the post-processor given the query
      (is it temporal-related?)
    - If yes, sorts nodes by date.
    - For each node, look at subsequent nodes and filter out nodes
      that have high embedding similarity with the current node.
      Because this means the subsequent node may have overlapping content
      with the current node but is also out of date
    """

    service_context: ServiceContext
    # infer_recency_tmpl: str = Field(default=DEFAULT_INFER_RECENCY_TMPL)
    date_key: str = "date"
    similarity_cutoff: float = Field(default=0.7)
    query_embedding_tmpl: str = Field(default=DEFAULT_QUERY_EMBEDDING_TMPL)

    @classmethod
    def class_name(cls) -> str:
        return "EmbeddingRecencyPostprocessor"

    def _postprocess_nodes(
        self,
        nodes: List[NodeWithScore],
        query_bundle: Optional[QueryBundle] = None,
    ) -> List[NodeWithScore]:
        """Postprocess nodes."""
        if query_bundle is None:
            raise ValueError("Missing query bundle in extra info.")

        # sort nodes by date
        node_dates = pd.to_datetime(
            [node.node.metadata[self.date_key] for node in nodes]
        )
        sorted_node_idxs = np.flip(node_dates.argsort())
        sorted_nodes: List[NodeWithScore] = [nodes[idx] for idx in sorted_node_idxs]

        # get embeddings for each node
        embed_model = self.service_context.embed_model
        texts = [node.get_content(metadata_mode=MetadataMode.EMBED) for node in nodes]
        text_embeddings = embed_model.get_text_embedding_batch(texts=texts)

        node_ids_to_skip: Set[str] = set()
        for idx, node in enumerate(sorted_nodes):
            if node.node.node_id in node_ids_to_skip:
                continue
            # get query embedding for the "query" node
            # NOTE: not the same as the text embedding because
            # we want to optimize for retrieval results

            query_text = self.query_embedding_tmpl.format(
                context_str=node.node.get_content(metadata_mode=MetadataMode.EMBED),
            )
            query_embedding = embed_model.get_query_embedding(query_text)

            for idx2 in range(idx + 1, len(sorted_nodes)):
                if sorted_nodes[idx2].node.node_id in node_ids_to_skip:
                    continue
                node2 = sorted_nodes[idx2]
                if (
                    np.dot(query_embedding, text_embeddings[idx2])
                    > self.similarity_cutoff
                ):
                    node_ids_to_skip.add(node2.node.node_id)

        return [
            node for node in sorted_nodes if node.node.node_id not in node_ids_to_skip
        ]

class TimeWeightedPostprocessor(BaseNodePostprocessor):
    """Time-weighted post-processor.

    Reranks a set of nodes based on their recency.

    """

    time_decay: float = Field(default=0.99)
    last_accessed_key: str = "__last_accessed__"
    time_access_refresh: bool = True
    # optionally set now (makes it easier to test)
    now: Optional[float] = None
    top_k: int = 1

    @classmethod
    def class_name(cls) -> str:
        return "TimeWeightedPostprocessor"

    def _postprocess_nodes(
        self,
        nodes: List[NodeWithScore],
        query_bundle: Optional[QueryBundle] = None,
    ) -> List[NodeWithScore]:
        """Postprocess nodes."""
        now = self.now or datetime.now().timestamp()
        # TODO: refactor with get_top_k_embeddings

        similarities = []
        for node_with_score in nodes:
            # embedding similarity score
            score = node_with_score.score or 1.0
            node = node_with_score.node
            # time score
            if node.metadata is None:
                raise ValueError("metadata is None")

            last_accessed = node.metadata.get(self.last_accessed_key, None)
            if last_accessed is None:
                last_accessed = now

            hours_passed = (now - last_accessed) / 3600
            time_similarity = (1 - self.time_decay) ** hours_passed

            similarity = score + time_similarity

            similarities.append(similarity)

        sorted_tups = sorted(zip(similarities, nodes), key=lambda x: x[0], reverse=True)

        top_k = min(self.top_k, len(sorted_tups))
        result_tups = sorted_tups[:top_k]
        result_nodes = [
            NodeWithScore(node=n.node, score=score) for score, n in result_tups
        ]

        # set __last_accessed__ to now
        if self.time_access_refresh:
            for node_with_score in result_nodes:
                node_with_score.node.metadata[self.last_accessed_key] = now

        return result_nodes


from numpy.lib.function_base import append
from rejson import Client, Path
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
import math
import numpy as np
import time
import Text

def getSimilarities(client, keys, reqs, forInsert=False):

    memberRange = [1, 1090000]
    establishedRange = [1149, 2020]
    fieldLabels = [ 'Members', 'Year established', 'Headquarters Country', 'Scope', 'Funding methods', 'Languages', 'Continents', 'Countries', 'Activities']
    allFields = ['members','established', 'hq', 'scope', 'funding', 'languages', 'continents', 'countries', 'activities']
    catFields = ['hq', 'scope', 'funding', 'languages', 'continents', 'countries', 'activities']
    weights = client.jsonget('weights', '.')
    stats = client.jsonget('stats', '.')

    # Calculate the similarity of the ngo value with the range,
    # giving more weizzght to higher or lower values depending on the "higher" flag
    def numericSimilarity(reqRange, ngoValue, range, higher=True):
        if forInsert:
            reqValue = reqRange

        else:
            if ngoValue < range[0] or ngoValue > range[1]:
                return 0

            # If the value is in the range, similarity is 1
            if ngoValue >= reqRange[0] and ngoValue <= reqRange[1]:
                return 1

            # Else, similarity is the distance from the nearest limit of the range
            reqValue = 0
            if ngoValue < reqRange[0]:
                reqValue = reqRange[0]
            else:
                reqValue = reqRange[1]

        # Normalize values so they are between 0 and 1
        normReqValue = (reqValue-range[0]) / (range[1]-range[0])
        normNgoValue = (ngoValue-range[0]) / (range[1]-range[0])

        # Golden ratio -> 1.618, used in circle function of ratio sqrt(3) to determine its center
        # higher -> (x+(phi-1))²+(y-phi)²=3 -> y²-2phiy+(x+phi-1)²+phi²-3=0, np.roots([1,-2phiy,x²+pi-3]),
        # lower -> (x-phi)²+(y-phi)²=3 -> y²-2phiy+(x-phi)²+phi²-3=0, lower solution value
        phi = ( 1 + math.sqrt(5) ) / 2
        if higher:
            reqValue = np.roots([1,-2*phi, (normReqValue+phi-1)**2+phi**2-3])
            ngoValue = np.roots([1,-2*phi, (normNgoValue+phi-1)**2+phi**2-3])

        else:
            reqValue = np.roots([1,-2*phi, (normReqValue-phi)**2+phi**2-3])
            ngoValue = np.roots([1,-2*phi, (normNgoValue-phi)**2+phi**2-3])

        return 1 - abs(min(abs(reqValue))-min(abs(ngoValue)))

    # Generate matrix where for every ngo, there is an array with a position for each possible value
    # of the categorical data, where there will be a 1 in the values that the ngo has, or a 0 otherwise
    # One hot encoding
    def categoricalSimilarity(data, reqs, field):

        # Get all different values from both lists
        # colNames = data + list(set(reqs)-set(data)) not used anymore as

        # dataDummiesDF = [1 if elem in data else 0 for elem in colNames]
        # reqsDummiesDF = [1 if elem in reqs else 0 for elem in colNames]

        dataDummiesDF = [weights[field][elem] if elem in data and elem in weights[field] else 0 for elem in reqs]
        reqsDummiesDF = [weights[field][elem] if elem in reqs and elem in weights[field] else 0 for elem in reqs]

        # Calculate cosine similarity
        return cosine_similarity([reqsDummiesDF], [dataDummiesDF]).flatten()[0]

    def getMaxSimil(allSimil):
        maxFields = []

        for i in np.argpartition(allSimil, -2)[-2:]:
            maxFields.append(fieldLabels[i])
        maxFields.append('Activities')

        return maxFields

    similarities = []
    reqMembers = reqs['members'] if 'members' in reqs else None
    reqEstablished = reqs['established'] if 'established' in reqs else None

    for k in keys:

        # Numeric values
        rangeSimilarities = []

        ngo = client.jsonget(k, '.')

        if reqMembers is not None and 'members' in ngo and ngo['members'] is not None:
            rangeSimilarities.append(numericSimilarity(reqMembers, ngo['members'], memberRange, higher=False))
        else:
            rangeSimilarities.append(0)

        if reqEstablished is not None and 'established' in ngo and ngo['established'] is not None:
            rangeSimilarities.append(numericSimilarity(reqEstablished, ngo['established'], establishedRange))
        else:
            rangeSimilarities.append(0)

        # Categorical values
        ngoSimilarities = []

        for field in catFields:
            if (field in ngo and ngo[field] is not None and field in reqs and reqs[field] is not None):
                if isinstance(ngo[field], str):
                    ngo[field] = [ngo[field]]
                else: # array, filter None
                    ngo[field] = [i for i in ngo[field] if i]
                    ngo[field] = list(dict.fromkeys(ngo[field]))
                if len(ngo[field]) > 0 and len(reqs[field]) > 0:
                    ngoSimilarities.append(categoricalSimilarity([ngo[field]] if isinstance(ngo[field], str) else ngo[field], [reqs[field]] if isinstance(reqs[field], str) else reqs[field], field))
                else:
                    ngoSimilarities.append(0)
            else:
                ngoSimilarities.append(0)

        # Compute similarity
        allSimil = rangeSimilarities+ngoSimilarities

        if forInsert:
            similarities.append(allSimil)

        else:
            weightedSimil = []

            for i in range(0, len(allSimil)):
                weightedSimil.append(allSimil[i]*stats[allFields[i]]['factor']*(4.5 if allFields[i] == 'activities' else 0.5625))

            similarities.append({
                'simil': sum(weightedSimil),
                'max': getMaxSimil(weightedSimil)
            })

    return similarities