# %%
from datetime import datetime
import pennylane as qml
from pennylane import numpy as np

# %%
import matplotlib.pyplot as plt
import seaborn as sns
import time
from os.path import join
import os

from sklearn.decomposition import PCA
from scipy.stats import multivariate_normal

import torch
import torchvision
from torchvision import datasets, transforms
import torch.optim as optim
from torchvision.utils import save_image

import argparse
torch.manual_seed(0)
np.random.seed(1)

# %%
# parameters
parser = argparse.ArgumentParser(
    description='learns gan on mnist dataset compressed with pca',
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument(
    '--batch_size',type=int, default=10)
parser.add_argument("--dimensions", type=int, default=4,
                    help="dimension of pca compressed data")
parser.add_argument("--epoch",       default=1,     type=int, help="number of epochs to learn")
parser.add_argument("--d_lr",        default=1e-3,  type=float, help="learning rate of the discriminator")
parser.add_argument("--g_lr",        default=1e-3,  type=float, help="learning rate of the generator")
parser.add_argument("--model",       default="q_exp",   type=str, choices=["q_exp", "q_sample", "c"],
                    help="c -- classial GAN,\n" +
                    "q_exp -- expectation value base quantum model with classical noise\n"+
                    "q_sample -- quantum sample based model. Uses quantum randomness")
parser.add_argument("--d_layers",    default=1,     type=int, help="Number of layers of quantum discriminator")
parser.add_argument("--g_layers",    default=1,     type=int, help="Number of layers of quantum generator")
parser.add_argument("--dataset_size",default=1000,  type=int,)

params, unknown = parser.parse_known_args()
if unknown:
    print("Warning, ignored unknown args:", *unknown)
interactive = False
params.dataset_size = min(60_000, params.dataset_size)

# #%%
# # handmade parameter adjust
# params.epoch = 50
# params.model = "q"
# params.d_lr = 1e-2
# params.g_lr = 0*1e-3
# params.dimensions = 2
# params.d_layers = 1
# params.g_layers = 9
# interactive = True

#%%
# wandb
import wandb
from dotenv import load_dotenv
import os

load_dotenv("wandb.env")
wandb.login(key=os.environ["WANDB_API_KEY"])

wandb.init(
    project="QuGAN-mnist",
    notes="",
    config=params,
    save_code=True,
)
# wandb.run.log_code(include_fn=lambda path: path.endswith("translate_train.ipynb"))
# wandb.run.log_code(
#     include_fn=lambda path: path.endswith(".ipynb") or path.endswith(".py")
# )
config = wandb.config

# %%
data_dimensions = config.dimensions

# Hyperparameters
batch_size = config.batch_size
g_lr = config.g_lr
d_lr = config.d_lr

if config.model == "q_exp":
    quantum = True
    use_noise = True
elif config.model == "q_sample":
    quantum = True
    use_noise = False
elif config.model == "c":
    quantum = False

# parameters
log_step = 10

# %% [markdown]
# Dataset preprocessing
# %%
# create name for data folder

time_stamp = datetime.now().strftime("%d.%m_%H:%M:%S")
folder = join("logs", f'{config.model}GAN__d={data_dimensions}__{time_stamp}')
os.makedirs(folder)
# %%
transform = transforms.Compose([
    transforms.ToTensor(),
    # transforms.Normalize((0.1307,), (0.3081,))
])
dataset = datasets.MNIST(root="~/.mnist", train=True, download=True,
                         transform=transform)

# %%
train_images = torch.stack([image.flatten() for image, label in dataset])
train_labels = torch.tensor([label for image, label in dataset])

# %%
# --------------------------------------------------
# ---------------- PCA Section ---------------------
# --------------------------------------------------
k = data_dimensions
pca = PCA(n_components=k)
pca.fit(train_images)
pca_data = pca.transform(train_images).astype(np.float32)

valid_labels = (train_labels == 3) | (train_labels == 6) | (train_labels == 9)

pca_data = pca_data[valid_labels][:config.dataset_size]
data_labels = train_labels[valid_labels][:config.dataset_size]
# %%
n_images = 10

plt.figure(figsize=(4*n_images, 4*2))
original_images = train_images[:n_images]

plt.suptitle(f"Real images vs {data_dimensions} dimensions PCA")
for i in range(n_images):
    plt.subplot(2, n_images, i+1)
    plt.imshow(original_images[i].reshape(28, 28), cmap="gray", vmin=0, vmax=1, interpolation="none")

pca.fit(train_images)
mixed_images = pca.inverse_transform(pca.transform(original_images))

for i in range(n_images):
    plt.subplot(2, n_images, n_images + i+1)
    plt.imshow(mixed_images[i].reshape(28, 28), cmap="gray", vmin=0, vmax=1, interpolation="none")
plt.savefig(join(folder, "pca_effect"))
plt.close()

# %%
# --------------------------------------------------
# --------  Normalize vector components ------------
# --------------------------------------------------
# scale data to be in [-1, 1]
pca_descaler = [[] for _ in range(k)]
for i in range(k):
    a, b = pca_data[:, i].min(), pca_data[:, i].max(),
    pca_descaler[i] = [(a+b)/2, (b-a)/2] # mean, scale
    pca_data[:, i] -= pca_descaler[i][0]
    pca_data[:, i] /= pca_descaler[i][1]

train_dataset = pca_data

print(f"The Total Explained Variance of {k} Dimensions is {sum(pca.explained_variance_ratio_).round(3)}")

# --------------------------------------------------
# Define a function that can take in PCA'ed data and return an image
# --------------------------------------------------
def descale_points(d_point, scales=pca_descaler, tfrm=pca):
    for col in range(d_point.shape[1]):
        d_point[:, col] *= scales[col][1]
        d_point[:, col] += scales[col][0]
    reconstruction = tfrm.inverse_transform(d_point)
    return reconstruction

# %%
if quantum:
    # ### Pennylane quantum model

    n_qubits = data_dimensions

    dev = qml.device("lightning.qubit", wires=n_qubits)
    shot_dev = qml.device("lightning.qubit", wires=n_qubits)
    diff_method = "adjoint"

    g_params = torch.from_numpy(np.random.uniform(0, 2*np.pi, size=(config.g_layers, 3, n_qubits))).requires_grad_(True)
    d_params = torch.from_numpy(np.random.uniform(0, 2*np.pi, size=(config.d_layers, 3, n_qubits))).requires_grad_(True)

    def generator_circ(noise, params):
        if use_noise:
            for i in range(n_qubits):
                qml.RX(noise[i], wires=i)

        for l in range(config.g_layers):
            for i in range(n_qubits):
                qml.RY(params[l, 0, i], wires=i)
            for i in range(n_qubits):
                qml.IsingYY(params[l, 1, i], wires=[i, (i+1) % n_qubits])
            for i in range(n_qubits):
                qml.CRY(params[l, 2, i], wires=[i, (i+1) % n_qubits])
        # todo try with different axes rotations

    def data_loading_circ(values):
        assert len(values) == n_qubits
        assert min(values) >= -1, max(values) <= 1
        for i in range(n_qubits):
            qml.RY(np.arccos(values[i]), wires=i)

    def discriminator(params):
        for l in range(config.d_layers):
            for i in range(n_qubits):
                qml.RY(params[l, 0, i], wires=i)
            for i in range(n_qubits):
                qml.IsingYY(params[l, 1, i], wires=[i, (i+1) % n_qubits])
            for i in range(n_qubits):
                qml.CRY(params[l, 2, i], wires=[i, (i+1) % n_qubits])

    @qml.qnode(dev, diff_method=diff_method)
    def latent_sample(noise, params):
        generator_circ(noise, params)
        return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]

    @qml.qnode(shot_dev,)
    def quantum_sample(params):
        generator_circ(None, params)
        return qml.sample()

    @qml.qnode(dev, diff_method=diff_method, interface="torch")
    def discriminate_generated_circ(input, real=True):
        if real:
            data_loading_circ(input)
        else:
            generator_circ(input, g_params)
        qml.adjoint(discriminator)(d_params)
        return qml.expval(qml.Hermitian(projector, wires=range(n_qubits)))

        # return qml.probs(wires=range(n_qubits))
        # + take [0] element when using (but doesn't support adjoint diff)
        # and so far throws error "QuantumFunctionError: Adjoint differentiation method does not support expectation return type mixed with other return types"

    projector = torch.zeros((2**n_qubits, 2**n_qubits))
    projector[0, 0] = 1

    def discrim_real(data):
        '''generates probabilities of discriminating'''
        return torch.stack([discriminate_generated_circ(x, real=True)[None] for x in data]).type(data.dtype)

    def discrim_fake(z):
        '''generates probabilities of discriminating'''
        return torch.stack([discriminate_generated_circ(noise, real=False)[None] for noise in z]).type(z.dtype)

    def gen_data(noise):
        if use_noise:
            return [latent_sample(x, g_params) for x in noise]
        else:
            shots = 20
            shot_dev.shots = shots*noise.shape[0]
            res = quantum_sample(g_params).reshape(shots, noise.shape[0], n_qubits) \
                .float().mean(axis=0)
            res = res*2 - 1  # from [0, 1] to [-1, 1]
            return res

    d_optimizer = optim.Adam([d_params], lr=d_lr)
    g_optimizer = optim.Adam([g_params], lr=g_lr)

    latent_size = n_qubits
    device = torch.device('cpu')

    number_of_generator_params = g_params.numel()
    number_of_discriminator_params = d_params.numel()
## %%
# ### classical model
else:
    import torch.nn as nn

    # Device configuration
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # Hyperparameters
    image_size = data_dimensions
    latent_size = 4
    hidden_size = 4

    # Generator model
    class Generator(nn.Module):
        def __init__(self):
            super(Generator, self).__init__()
            self.model = nn.Sequential(
                nn.Linear(latent_size, image_size),
                nn.Tanh()  # To ensure pixel values are in the range [-1, 1]
            )

        def forward(self, x):
            return self.model(x)

    # Discriminator model
    class Discriminator(nn.Module):
        def __init__(self):
            super(Discriminator, self).__init__()
            self.model = nn.Sequential(
                nn.Linear(image_size, hidden_size),
                nn.LeakyReLU(0.02),
                nn.Linear(hidden_size, hidden_size),
                nn.LeakyReLU(0.02),
                nn.Linear(hidden_size, hidden_size),
                nn.LeakyReLU(0.02),
                nn.Linear(hidden_size, 1),
                nn.Sigmoid()
            )

        def forward(self, x):
            return self.model(x)

        def predict_real(self, data):
            return self.model(data)

        def predict_fake(self, noise):
            return self.model(self.gen(noise).detach())

    # Initialize models and optimizers
    generator = Generator().to(device)
    discriminator = Discriminator().to(device)
    d_optimizer = optim.Adam(discriminator.parameters(), lr=d_lr)
    g_optimizer = optim.Adam(generator.parameters(), lr=g_lr)

    def discrim_real(data):
        return discriminator(data)

    def discrim_fake(noise):
        return discriminator(generator(noise))

    def gen_data(noise):
        return generator(noise)

    number_of_generator_params, number_of_discriminator_params = \
    (sum(p.numel() for p in model.parameters() if p.requires_grad)
     for model in [generator, discriminator])

# %% some additional functions

def kl_divergance(p, q):
    eps = 0.1/p.size    # to be smaller, the uniform proba
    p = np.maximum(eps, p)
    p /= np.sum(p)
    q = np.maximum(eps, q)
    q /= np.sum(q)
    return np.sum(p * np.log(p / q))

def fake_hellinger_distance(p: np.array, q: np.array):
    results = []
    n = len(p.shape)
    for i in range(n):
        prior_p = np.sum(p, axis=tuple(range(i)) + tuple(range(i+1, n)))
        prior_q = np.sum(q, axis=tuple(range(i)) + tuple(range(i+1, n)))
        results.append(hellinger_distance(prior_p, prior_q))
    return np.mean(results)

def hellinger_distance(p, q):
    pointwise_dist =  (np.sqrt(p) - np.sqrt(q))**2
    return np.sqrt(np.sum(pointwise_dist)/2)

def visualize_discriminator():
    assert data_dimensions == 2
    n = 10
    x = np.linspace(*data_ranges[0], n, dtype=np.float32)
    y = np.linspace(*data_ranges[1], n, dtype=np.float32)
    grids = np.meshgrid(x, y)
    grids = [t.reshape(-1, 1) for t in grids]
    xy = np.concatenate(grids, axis=1)
    with torch.no_grad():
        z = discrim_real(torch.from_numpy(xy))
    z = z.reshape((n, n))
    full_frame()
    plt.contourf(z.T, vmin=0, vmax=1, levels=12)

def estimate_density(samples, bins=10):
    # Compute the histogram of samples
    hist, _ = np.histogramdd(samples, bins=bins, range=data_ranges)
    return hist/len(samples)

data_ranges = list(zip(np.min(train_dataset, axis=0), np.max(train_dataset, axis=0)))

true_dft = estimate_density(train_dataset) # for future computations

def fit_multidimensional_normal(data):
    # Compute the mean and covariance of the data
    mean = np.mean(data, axis=0)
    covariance_matrix = np.cov(data, rowvar=False)

    # Create a multivariate normal distribution with the computed mean and covariance
    fitted_distribution = multivariate_normal(mean=mean, cov=covariance_matrix)

    return fitted_distribution

def full_frame():
    """helper function to plot without axis and margins"""
    import matplotlib as mpl
    mpl.rcParams['savefig.pad_inches'] = 0
    ax = plt.axes([0,0,1,1], frameon=False)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    plt.autoscale(tight=True)

# %% [markdown]
# ## Learning

# %%
# ----- Learning -------
criterion = torch.nn.BCELoss()
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

wandb.run.summary["g_params"] = number_of_generator_params
wandb.run.summary["d_params"] = number_of_discriminator_params

def get_latent(size):
    return torch.randn(size, latent_size).to(device)

def fit_epoch():
    for i, images in enumerate(train_loader):
        batch_size = images.size(0)
        images = images.view(batch_size, -1).to(device)

        # Create real and fake labels for the loss functions
        real_labels = torch.ones(batch_size, 1).to(device)
        fake_labels = torch.zeros(batch_size, 1).to(device)

        # Train the discriminator
        # Real images
        outputs = discrim_real(images)
        d_loss_real = criterion(outputs, real_labels)

        # Fake images
        z = get_latent(batch_size)
        outputs = discrim_fake(z)
        d_loss_fake = criterion(outputs, fake_labels)

        # Total discriminator loss
        d_loss = d_loss_real + d_loss_fake
        # Backprop and optimize discriminator
        d_optimizer.zero_grad()
        d_loss.backward()
        d_optimizer.step()

        # Train the generator
        z = get_latent(batch_size)
        outputs = discrim_fake(z)   # todo check whether it is possible to reuse previous computation ouf this outputs
        g_loss = criterion(outputs, real_labels)

        # Backprop and optimize generator
        g_optimizer.zero_grad()
        g_loss.backward()
        g_optimizer.step()

        # Print progress
        if (i + 1) % log_step == 0:
            print(f'Epoch [{epoch+1}/{config.epoch}], Step [{i+1}/{len(train_loader)}], '
                  f'Discr Loss: {d_loss.item():.4f}, Gen Loss: {g_loss.item():.4f}')
            wandb.log({"d_loss": d_loss.item(), "g_loss": g_loss.item()})

def inference():
    # generate images
    if quantum and use_noise:
        n_samples = 200
    else:
        n_samples = 2000

    points_alpha = min(1, 0.2*1e3/config.dataset_size)
    with torch.no_grad():
        fake_data = np.array(gen_data(get_latent(n_samples)))

    # n_visual = 10
    # fake_images = torch.tensor(pca.inverse_transform(fake_data[:n_visual]), dtype=torch.float32)
    # # os.makedirs(join(folder, "gen_images"), exist_ok=True)
    # # save_image(fake_images.view(fake_images.size(0), 1, 28, 28),
    # #             join(folder, "gen_images", f'generated_images_epoch_{epoch}.jpg'))
    # plt.figure(figsize=(2*n_visual, 2))
    # for i in range(n_visual):
    #     plt.subplot(1, n_visual, i+1)
    #     plt.imshow(fake_images[i].view(28, 28), cmap='gray', vmin=0, vmax=1, interpolation="none")
    #     plt.axis('off')
    # wandb.log({"generated_images": wandb.Image(plt)}, commit=False)

    # # plt.savefig(join(folder, "gen_images", f'my_generated_images_epoch_{epoch}.jpg'))
    # plt.close()

    # compare distributions on plot

    vis_dims = [0, 1]

    for l in data_labels.unique():
        plt.scatter(pca_data[data_labels == l, vis_dims[0]],
                    pca_data[data_labels == l, vis_dims[1]], label=l.item(), c='blue', alpha=points_alpha, s=8)
    plt.title("PCA values of different images")
    plt.scatter(fake_data[:, vis_dims[0]],
                fake_data[:, vis_dims[1]], label="fake", alpha=points_alpha, s=8)

    plt.legend(loc='upper right')
    wandb.log({"distributions":  wandb.Image(plt)}, commit=False)
    plt.close()

    # plot density function approximation
    sampled_data = fake_data[:, vis_dims]

    # # normal distribution approximation
    # fitted_distribution = fit_multidimensional_normal(sampled_data)
    # x, y = np.meshgrid(np.linspace(*data_ranges[vis_dims[0]], 100),
    #                    np.linspace(*data_ranges[vis_dims[1]], 100))
    # pos = np.dstack((x, y))
    # pdf_values = fitted_distribution.pdf(pos)
    # plt.contourf(x, y, pdf_values.T, cmap='Reds', alpha=0.7)
    # plt.title('2D Density Function of Fitted 2D Normal Distribution')
    # wandb.log({"dft_approx":  wandb.Image(plt)}, commit=False)
    # plt.close()

    # handmade hist for full control and contourf plot
    full_frame()
    pdf_values, edges = np.histogramdd(sampled_data, range=[data_ranges[j] for j in vis_dims], density=True)
    plt.contourf(pdf_values.T, cmap='Reds', alpha=0.7,
                 extent=[edges[0][0], edges[0][-1], edges[1][0], edges[1][-1]])
    # plt.title('Density Function Contourf plot')
    for l in data_labels.unique():
        plt.scatter(pca_data[data_labels == l, vis_dims[0]],
                    pca_data[data_labels == l, vis_dims[1]], label=l.item(), c='blue', alpha=points_alpha, s=8)
    plt.legend(loc='upper right')
    wandb.log({"dft_contourf":  wandb.Image(plt)}, commit=False)
    if interactive:
        plt.show()
    plt.close()

    # full_frame()
    # # plt.title('Density Function histogram plot')
    # plt.hist2d(sampled_data[:, 0], sampled_data[:, 1], range=[data_ranges[j] for j in vis_dims],
    #            alpha=0.7, cmap="Reds", bins=15)
    # for l in data_labels.unique():
    #     plt.scatter(pca_data[data_labels == l, vis_dims[0]],
    #                 pca_data[data_labels == l, vis_dims[1]], label=l.item(), c='blue', alpha=points_alpha, s=8)
    # plt.legend(loc='upper right')
    # wandb.log({"dft_approx_hist":  wandb.Image(plt)}, commit=False)
    # if interactive:
    #     plt.show()
    # plt.close()

    if data_dimensions == 2:
        # visualize discriminator thoughts
        visualize_discriminator()
        wandb.log({"disciminator_predictions":  wandb.Image(plt)}, commit=False)
        if interactive:
            plt.show()
        plt.close()

    # compute metrics
    metrics = {}
    fake_dft = estimate_density(fake_data)
    with torch.no_grad():
        metrics["kl_divergence"] = kl_divergance(true_dft, fake_dft).item()
        metrics["hellinger_distance"] = hellinger_distance(true_dft, fake_dft).item()
        metrics["fake_hellinger_distance"] = fake_hellinger_distance(true_dft, fake_dft).item()
        wandb.log(metrics, commit=False)
    print(metrics)

    return metrics

# Training loop
for epoch in range(config.epoch):
    start = time.time()
    # Save generated images each epoch
    inference()

    print('Time for inference is {} sec'.format(time.time() - start))

    start = time.time()
    fit_epoch()
    print('Time for Epoch {} is {} sec'.format(epoch + 1, time.time() - start))

epoch+=1
inference()

# Save models
if quantum:
    torch.save(g_params, join(folder, 'generator_model.pth'))
    torch.save(d_params, join(folder, 'discriminator_model.pth'))
else:
    torch.save(generator.state_dict(), join(folder, 'generator_model.pth'))
    torch.save(discriminator.state_dict(), join(folder, 'discriminator_model.pth'))

# %%
wandb.finish()


#Definindo os modelos de bicicleta
modelos = {
    "M01": ["Caloi Vulcan Aro 29 com 21 Velocidades", "Caloi", "Comum", "21", "2021"],
    "M02": ["Caloi E-Vibe City", "Caloi", "Elétrica", "0", "2020"],
    "M03": ["Sense Bike Impulse E Trail Comp 2021/22", "Sense", "Elétrica", "0", "2022"],
    "M04": ["MTB KLS Sport Gold Aro 29 Freio Disco 21 Marchas", "KLS", "Comum", "21", "2019"]
}

#Definindo as bicicletas
bicicletas = {
    "B0001": ["M01", "C3C00010", "E01", "Disponível"],
    "B0002": ["M01", "C3C00018", "E02", "Disponível"],
    "B0003": ["M02", "C1E00002", "E01", "Disponível"],
    "B0004": ["M02", "C1E00027", "E02", "Disponível"],
    "B0005": ["M03", "S5E000010", "E01", "Disponível"],
    "B0006": ["M03", "S5E000011", "E02", "Disponível"],
    "B0007": ["M04", "K11C00046", "E01", "Disponível"],
    "B0008": ["M04", "K11C00057", "E02", "Disponível"]
}

#Definindo reservas
reservas = {
    "M01": ["U001"],
    "M02": [],
    "M03": [],
    "M04": []
}

#Definindo Usuários
usuarios = {
    "U001": ["ind", "Maria Bonita"],
    "U003": ["coo", "Associação Itabaiana de Guias de Turismo"]
}

#Definindo empréstimos
#"U003": ["M01"]
emprestimos = {
    "U001": [],
    "U003": []
}

#Definindo estações
estacao1 = ["B0001", "B0003", "B0005", "B0007"]
estacao2 = ["B0002", "B0004", "B0006", "B0008"]

#Funcão de Consulta
def consulta(dados_consulta):
    try:
        if dados_operacao[1] == "*":
            for a in modelos:
                aux_exibicao_modelo = modelos[a]
                print("Código:", a + ",", "Descrição:", aux_exibicao_modelo[0] + ",", "Marca:", aux_exibicao_modelo[1] + ",", "Categoria:", aux_exibicao_modelo[2]+ ",", "Marchas:", aux_exibicao_modelo[3] + ",", "Ano:", aux_exibicao_modelo[4])
        elif dados_operacao[1] == "M01" or dados_operacao[1] == "M02" or dados_operacao[1] == "M03" or dados_operacao[1] == "M04":
            aux_consulta1 = modelos[dados_consulta[1]]
            print("Descrição:", aux_consulta1[0])
            aux_consulta2 = reservas[dados_consulta[1]]
            if len(aux_consulta2) != 0:
                print("Quantidade de reservas:", len(aux_consulta2))
                aux_consulta3 = reservas[dados_consulta[1]]
                for b in usuarios:
                    for c in aux_consulta3:
                        if b == c:
                            aux_consulta4 = usuarios[b]
                            print("Usuário com reserva:", aux_consulta4[1])
            else:
                print("Quantidade de reservas: 0")
            if dados_consulta[1] == "M01":
                aux_codigo_bicicleta1 = "B0001"
                aux_codigo_bicicleta2 = "B0002"
            elif dados_consulta[1] == "M02":
                aux_codigo_bicicleta1 = "B0003"
                aux_codigo_bicicleta2 = "B0004"
            elif dados_consulta[1] == "M03":
                aux_codigo_bicicleta1 = "B0005"
                aux_codigo_bicicleta2 = "B0006"
            else:
                aux_codigo_bicicleta1 = "B0007"
                aux_codigo_bicicleta2 = "B0008"
            print("Código da bicicleta modelo", dados_consulta[1] + ":", aux_codigo_bicicleta1)
            print("Código da bicicleta modelo", dados_consulta[1] + ":", aux_codigo_bicicleta2)
            aux_consulta5 = 0
            aux_consulta6 = 0
            for d in estacao1:
                if aux_codigo_bicicleta1 == d:
                    print("Bicicleta", aux_codigo_bicicleta1,"disponível na estação 1")
                    aux_consulta5 = 1
                if aux_codigo_bicicleta2 == d:
                    print("Bicicleta", aux_codigo_bicicleta2,"disponível na estação 1")
                    aux_consulta6 = 1
            for e in estacao2:
                if aux_codigo_bicicleta1 == e:
                    print("Bicicleta", aux_codigo_bicicleta1,"disponível na estação 2")
                    aux_consulta5 = 1
                if aux_codigo_bicicleta2 == e:
                    print("Bicicleta", aux_codigo_bicicleta2,"disponível na estação 2")
                    aux_consulta6 = 1
            aux_consulta9 = 0
            if aux_consulta5 == 0:
                for f in emprestimos:
                    aux_consulta7 = emprestimos[f]
                    for g in aux_consulta7:
                        if g == dados_consulta[1]:
                            aux_consulta8 = usuarios[f]
                            if aux_consulta9 == 1:
                                print("Bicicleta", aux_codigo_bicicleta2, "emprestada para", aux_consulta8[1])
                            else:
                                print("Bicicleta", aux_codigo_bicicleta1, "emprestada para", aux_consulta8[1])
                                aux_consulta9 = 1
            if aux_consulta6 == 0:
                if aux_consulta9 == 0:
                    for f in emprestimos:
                        aux_consulta7 = emprestimos[f]
                        for g in aux_consulta7:
                            if g == dados_consulta[1]:
                                aux_consulta8 = usuarios[f]
                                print("Bicicleta", aux_codigo_bicicleta2, "emprestada para", aux_consulta8[1])
        else:
            print("Informações para consulta inválidas")
    except IndexError:
        print("Erro: Informações para consulta insuficientes")

#Início do programa
operacao = input("Operação: ")
while operacao != "fim":
    dados_operacao = operacao.split()
    if dados_operacao[0] == "cdu":
        cadastro(dados_operacao)
    elif dados_operacao[0] == "emb":
        emprestimo(dados_operacao)
    elif dados_operacao[0] == "dvb":
        devolucao(dados_operacao)
    elif dados_operacao[0] == "reb":
        reserva(dados_operacao)
    elif dados_operacao[0] == "bik":
        consulta(dados_operacao)
    operacao = "fim"


from operator import itemgetter
import re

import numpy as np
import pandas as pd

get_unit_and_periods = itemgetter('unit', 'periods')

def parse_treasury_csv_column(column):
    """
    Parse a treasury CSV column into a more human-readable format.

    Columns start with 'RIFLGFC', followed by Y or M (year or month), followed
    by a two-digit number signifying number of years/months, followed by _N.B.
    We only care about the middle two entries, which we turn into a string like
    3month or 30year.
    """
    column_re = re.compile(
        r"^(?P<prefix>RIFLGFC)"
        "(?P<unit>[YM])"
        "(?P<periods>[0-9]{2})"
        "(?P<suffix>_N.B)$"
    )

    match = column_re.match(column)
    if match is None:
        raise ValueError("Couldn't parse CSV column %r." % column)
    unit, periods = get_unit_and_periods(match.groupdict())

    # Roundtrip through int to coerce '06' into '6'.
    return str(int(periods)) + ('year' if unit == 'Y' else 'month')

def earliest_possible_date():
    """
    The earliest date for which we can load data from this module.
    """
    # The US Treasury actually has data going back further than this, but it's
    # pretty rare to find pricing data going back that far, and there's no
    # reason to make people download benchmarks back to 1950 that they'll never
    # be able to use.
    return pd.Timestamp('1980', tz='UTC')

def get_treasury_data(start_date, end_date):
    return pd.read_csv(
        "https://www.federalreserve.gov/datadownload/Output.aspx"
        "?rel=H15"
        "&series=bf17364827e38702b42a58cf8eaa3f78"
        "&lastObs="
        "&from="  # An unbounded query is ~2x faster than specifying dates.
        "&to="
        "&filetype=csv"
        "&label=include"
        "&layout=seriescolumn"
        "&type=package",
        skiprows=5,  # First 5 rows are useless headers.
        parse_dates=['Time Period'],
        na_values=['ND'],  # Presumably this stands for "No Data".
        index_col=0,
    ).loc[
        start_date:end_date
    ].dropna(
        how='all'
    ).rename(
        columns=parse_treasury_csv_column
    ).tz_localize('UTC') * 0.01  # Convert from 2.57% to 0.0257.

def dataconverter(s):
    try:
        return float(s) / 100
    except:
        return np.nan

def get_daily_10yr_treasury_data():
    """Download daily 10 year treasury rates from the Federal Reserve and
    return a pandas.Series."""
    url = "https://www.federalreserve.gov/datadownload/Output.aspx?rel=H15" \
          "&series=bcb44e57fb57efbe90002369321bfb3f&lastObs=&from=&to=" \
          "&filetype=csv&label=include&layout=seriescolumn"
    return pd.read_csv(url, header=5, index_col=0, names=['DATE', 'BC_10YEAR'],
                       parse_dates=True, converters={1: dataconverter},
                       squeeze=True)


def solution(N, road, K):

    def smallestNode(N):
        min = 1e9
        index = 0
        for i in range(1, N + 1):
            if distance[i] < min and not visited[i]:
                min = distance[i]
                index = i
        return index

    def dijkstra(N, start):
        distance[start] = 0
        visited[start] = True
        for i in graph[start]:
            distance[i[0]] = i[1]
        for i in range(N - 1):
            now = smallestNode(N)
            visited[now] = True
            for j in graph[now]:
                cost = distance[now] + j[1]
                if cost < distance[j[0]]:
                    distance[j[0]] = cost

    answer = 0
    graph = []
    for i in range(N + 1):
        graph.append([])
    visited = [False] * (N + 1)
    distance = [int(1e9)] * (N + 1)
    for i in range(len(road)):
        graph[road[i][0]].append([road[i][1], road[i][2]])
        graph[road[i][1]].append([road[i][0], road[i][2]])

    dijkstra(N, 1)

    for i in distance:
        if i <= K:
            answer += 1

    return answer

프로그래머스/2단계/배달.py


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

# In[1]:

import pyspark

# In[2]:

from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
conf = pyspark.SparkConf().setAppName('appName').setMaster('local')
sc = pyspark.SparkContext(conf=conf)
spark = SparkSession(sc)

# In[3]:

#read the whole dataset
flight_data = spark.read.option("inferSchema", "true").option("header", "true").csv("671009038_T_ONTIME_REPORTING.csv")
flight_data.count()

# In[4]:

flight_data.show(3)

# In[5]:

flight_data.createOrReplaceTempView("flight_data")

# In[23]:

#convert the NULL values with 0 number
flight_data = spark.sql("""
SELECT *, ifnull(DEP_DELAY, '0') as DEP_DELAY1, ifnull(ARR_DELAY, '0') as ARR_DELAY1
FROM flight_data
""")
flight_data.show(3)

# In[24]:

#drop the previous columns referring to the delays
flight_data = flight_data.drop("DEP_DELAY", "ARR_DELAY")
flight_data.createOrReplaceTempView("flight_data")

# In[25]:

flight_data.show(3)

# In[26]:

#create the dataset of the airports (in the DEST column the airports were
#the same as in the origin column)
flights = spark.sql("""
SELECT ORIGIN AS airports, count(ORIGIN) as flight_numbers
FROM flight_data
GROUP BY ORIGIN
HAVING count(ORIGIN) > (SELECT count(ORIGIN)*0.01 FROM flight_data)
""")
flights.show() #the number of flights for each airport

# In[27]:

print(flights.count()) #the size of the unique airports

# In[28]:

#create the dataset of the airways
airways = spark.sql("""
SELECT CARRIER AS airways, count(CARRIER) as flight_numbers
FROM flight_data
GROUP BY CARRIER
HAVING count(CARRIER) > (SELECT count(CARRIER)*0.01 FROM flight_data)
""")
airways.show()

# In[29]:

print(airways.count()) #the size of the unique airways

# In[30]:

#make those datasets views in order to use them in the below queries
flights.createOrReplaceTempView("flights")
airways.createOrReplaceTempView("airways")

# In[31]:

#Report 1
#we want the avg for the departures delays for every airport (origin)
avg_airports = spark.sql("""
SELECT fd.ORIGIN as Airports, round(avg(fd.DEP_DELAY1), 3) as Departures_Delay
FROM flight_data as fd, flights as f
WHERE fd.ORIGIN = f.airports
GROUP BY fd.ORIGIN
ORDER BY avg(fd.DEP_DELAY1) DESC""")
avg_airports.show()

# In[32]:

avg_airports.toPandas().to_csv('task2-ap-avg.csv', header=False)

# In[33]:

#Report 2
#place an iterator
c_airports = spark.sql("""
SELECT ROW_NUMBER() OVER(PARTITION BY ORIGIN
                            ORDER BY DEP_DELAY1 ASC) AS NUM_ROW,
        ORIGIN, DEP_DELAY1 AS DEP_DELAY
FROM flight_data, flights as f
WHERE ORIGIN = f.airports
""")
c_airports.show()

# In[34]:

#save the length of the airport's flights
count_airports = spark.sql("""
SELECT ORIGIN, count(ORIGIN) as COR
FROM flight_data, flights as f
WHERE ORIGIN = f.airports
GROUP BY ORIGIN
""")
count_airports.show()

# In[35]:

count_airports.createOrReplaceTempView("count_airports")

# In[36]:

#find the lines where the medians are
count_airports = spark.sql("""
SELECT ORIGIN, COR, CAST(IF(MOD(COR, 2)=0, COR/2, (COR+1)/2) AS int) AS P,
        CAST(IF(MOD(COR, 2)=0, COR/2+1, 0) AS int) AS P1
FROM count_airports
""")
count_airports.show()

# In[37]:

c_airports.createOrReplaceTempView("c_airports")
count_airports.createOrReplaceTempView("count_airports")

# In[38]:

#match to each P value the recording row of the delay
p_set = spark.sql("""
SELECT air.ORIGIN as AIRPORT, DEP_DELAY as DELAY1
FROM c_airports as air, count_airports as c
WHERE air.ORIGIN = c.ORIGIN AND NUM_ROW = P
""")
p_set.show()

# In[39]:

#match to each P1 value the recording row of the delay
p1_set = spark.sql("""
SELECT air.ORIGIN as AIRPORT, DEP_DELAY as DELAY2
FROM c_airports as air, count_airports as c
WHERE air.ORIGIN = c.ORIGIN AND NUM_ROW = P1
""")
p1_set.show()

# In[40]:

p_set.createOrReplaceTempView("p_set")
p1_set.createOrReplaceTempView("p1_set")

# In[41]:

#join the above tables to a final one
med_air = spark.sql("""
SELECT p_set.AIRPORT, DELAY1, DELAY2
FROM p_set
LEFT JOIN p1_set
ON p_set.AIRPORT = p1_set.AIRPORT
""")
med_air.show()

# In[42]:

med_air.createOrReplaceTempView("med_air")

# In[43]:

#find the median and sort it
med_air = spark.sql("""
SELECT AIRPORT, IF((DELAY2 IS NULL), DELAY1, (DELAY1 + DELAY2)/2) AS MEDIAN
FROM med_air
ORDER BY MEDIAN DESC
""")
med_air.show()

# In[44]:

med_air.toPandas().to_csv('task2-ap-med.csv', header=False)

# In[45]:

#Report 3
#we want the avg for the departures delays for every airway (carrier)
avg_airways = spark.sql("""
SELECT fd.CARRIER as Airways, round(avg(fd.DEP_DELAY1), 3) as Departures_Delay
FROM flight_data as fd, airways as a
WHERE fd.CARRIER = a.airways
GROUP BY fd.CARRIER
ORDER BY avg(fd.DEP_DELAY1) DESC""")
avg_airways.show()

# In[46]:

avg_airways.toPandas().to_csv('task2-aw-avg.csv', header=False)

# In[47]:

#Report 4
#The same steps as the "Report 2"
c_airways = spark.sql("""
SELECT ROW_NUMBER() OVER(PARTITION BY CARRIER
                            ORDER BY DEP_DELAY1 ASC) AS NUM_ROW,
        CARRIER, DEP_DELAY1 AS DEP_DELAY
FROM flight_data, airways as a
WHERE CARRIER = a.airways
""")
c_airways.show()

# In[49]:

count_airways = spark.sql("""
SELECT CARRIER, count(CARRIER) as CAR
FROM flight_data, airways as a
WHERE CARRIER = a.airways
GROUP BY CARRIER
""")
count_airways.show()

# In[50]:

count_airways.createOrReplaceTempView("count_airways")

# In[51]:

count_airways = spark.sql("""
SELECT CARRIER, CAR, CAST(IF(MOD(CAR, 2)=0, CAR/2, (CAR+1)/2) AS int) AS P,
        CAST(IF(MOD(CAR, 2)=0, CAR/2+1, 0) AS int) AS P1
FROM count_airways
""")
count_airways.show()

# In[52]:

c_airways.createOrReplaceTempView("c_airways")
count_airways.createOrReplaceTempView("count_airways")

# In[53]:

p_set = spark.sql("""
SELECT air.CARRIER as AIRWAY, DEP_DELAY as DELAY1
FROM c_airways as air, count_airways as c
WHERE air.CARRIER = c.CARRIER AND NUM_ROW = P
""")
p_set.show()

# In[54]:

p1_set = spark.sql("""
SELECT air.CARRIER as AIRWAY, DEP_DELAY as DELAY2
FROM c_airways as air, count_airways as c
WHERE air.CARRIER = c.CARRIER AND NUM_ROW = P1
""")
p1_set.show()

# In[55]:

p_set.createOrReplaceTempView("p_set")
p1_set.createOrReplaceTempView("p1_set")

# In[56]:

med_air = spark.sql("""
SELECT p_set.AIRWAY, DELAY1, DELAY2
FROM p_set
LEFT JOIN p1_set
ON p_set.AIRWAY = p1_set.AIRWAY
""")
med_air.show()

# In[57]:

med_air.createOrReplaceTempView("med_air")

# In[ ]:

med_air = spark.sql("""
SELECT AIRWAY, IF((DELAY2 IS NULL), DELAY1, (DELAY1 + DELAY2)/2) AS MEDIAN
FROM med_air
ORDER BY MEDIAN DESC
""")
med_air.show()

# In[ ]:

med_air.toPandas().to_csv('task2-aw-med.csv', header=False)

# In[ ]:

sc.stop()


from .main_panel import ALINKER_PT_MainPanel

classes = (
    ALINKER_PT_MainPanel,
)

def register_panels():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

def unregister_panels():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)


def delete(self, url, name, params=None, headers=None, connection=None):
        """
        Synchronous DELETE request. ``data`` must be a JSONable value.
        """
        if not name: name = ''
        params = params or {}
        headers = headers or {}
        endpoint = self._build_endpoint_url(url, name)
        self._authenticate(params, headers)
        return make_delete_request(endpoint, params, headers, connection=connection)


def build_tmp_h5(data, samples):
    """ build tmp h5 arrays that can return quick access for nloci"""
    ## get samples and names, sorted
    snames = [i.name for i in samples]
    snames.sort()

    ## Build an array for quickly indexing consens reads from catg files.
    ## save as a npy int binary file.
    uhandle = os.path.join(data.dirs.across, data.name+".utemp.sort")
    bseeds = os.path.join(data.dirs.across, data.name+".tmparrs.h5")

    ## send as first async1 job
    get_seeds_and_hits(uhandle, bseeds, snames)


import subprocess
from subprocess import Popen

global x
x = input("Enter Port No.:")
print ("Port number is :" + x)

def open_cmd(port):

	cmd_prompt = 'cmd'
	command = 'appium -p'
	full_command = cmd_prompt+' '+'/k'+' '+command+' '+str(port)
	print("command is : "+full_command)
	start=Popen(full_command,shell=True)
open_cmd(x)

def adb_call():
	subprocess.run( 'start cmd',shell=True)
	subprocess.call('adb shell am start -a android.intent.action.CALL -d tel:9893973624',shell=True)
adb_call()


# Создать телефонный справочник с возможностью импорта и экспорта данных в нескольких форматах.

import logic, menu

def view(dict):
    for key in dict:
        print(f'{key} - {dict[key]}')

def main():
    catalog = {}
    catalog = logic.fill_first_dict(catalog)
    inputCommand = 0
    while inputCommand != 6:
        print('Список доступных комманд!')
        view(menu.ui_menu())
        inputCommand = int(input('Введи номер команды: '))
        if inputCommand == 0:
            confirm = input('Данное действие перезапишет текущий каталог! Уверен (yes/no)? - ')
            if confirm == 'yes':
                catalog = logic.fill_test_dict(catalog)
                print('Вроде получилось)')
            elif confirm == 'no':
                print('Отмена!!!')
            else:
                print('Я не понял, но на всякий случай отменил.')
        elif inputCommand == 1:
            catalog = logic.add_to_file(catalog)
        elif inputCommand == 2:
            view(catalog)
            input_update_value = input('Введи номер строки для изменения или no для отмены - ')
            if input_update_value == 'no':
                print('Отмена!!!')
            elif input_update_value == '0':
                print('Невозможно изменить заголовок')
                print('Отмена!!!')
            else:
                catalog = logic.update_value(catalog, input_update_value)
        elif inputCommand == 3:
            logic.import_by_line(catalog)
        elif inputCommand == 4:
            logic.import_to_line(catalog)
        elif inputCommand == 5:
            view(catalog)
        else:
            print('Пока-пока!')

if __name__ == "__main__":
    main()


# pylint: disable=arguments-differ
# pylint: disable=too-few-public-methods
# pylint: disable=invalid-name
"""CoreX Objects

The unit of the request body when sending corex apis. Support turning
each CoreX Object to json format.

"""
import json

from decanter.core.extra.decorators import corex_obj

class ComplexEncoder(json.JSONEncoder):
    """Extending JSONEncoder

    Define own JSON Encoder for the object having the function `jsonable`. It will
    return Dictionary object is having `jsonable`.

    Returns:
        function: `jsonable`
    """

    def default(self, obj):
        if hasattr(obj, "jsonable"):
            return obj.jsonable()
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

class CoreBodyObj:
    """Base class of all CoreX Object.

    Has the jsonable function to return the Dictionary of object.

    """

    def __init__(self, **kwargs):
        """Add the argument pass in as attributes."""
        self.__dict__.update((k, v) for k, v in kwargs.items() if v is not None)

    def jsonable(self):
        """Reutrn the Dictionary of Object"""
        return self.__dict__

class CVTrain(CoreBodyObj):
    """Specification for the train fold"""

    @classmethod
    @corex_obj(required={"start", "end"})
    def create(cls, **kwargs):
        """Return CVTrain object with passed kwargs as attributes"""
        return cls(**kwargs)

class CVObject(CoreBodyObj):
    """The specifications for the cross validation folds"""

    @classmethod
    @corex_obj(required={"cvTrain", "cvTest"})
    def create(cls, **kwargs):
        """Return CVObject object with passed kwargs as attributes"""
        return cls(**kwargs)

class Column(CoreBodyObj):
    """The column names of the data"""

    @classmethod
    @corex_obj(required={"id", "data_type"})
    def create(cls, **kwargs):
        """Return Column object with passed kwargs as attributes"""
        return cls(**kwargs)

class TimeSeriesSplit(CoreBodyObj):
    """The specifications for the folds in time series cross validation.
    Cannot be used if nfold is specified"""

    @classmethod
    @corex_obj(required={"split_by", "cv", "train"})
    def create(cls, **kwargs):
        """Return TimeSeriesSplit object with passed kwargs as attributes"""
        return cls(**kwargs)

class TrainBody(CoreBodyObj):
    """The specifications for the folds in time series cross validation.
    Cannot be used if nfold is specified"""

    @classmethod
    @corex_obj(required={"target", "train_data_id", "algos"})
    def create(cls, **kwargs):
        """Return TrainBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class ClusterTrainBody(CoreBodyObj):
    """ClusterTrainBody"""

    @classmethod
    @corex_obj(required={"train_data_id"})
    def create(cls, **kwargs):
        """Return ClusterTrainBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class ModelBuildControl(CoreBodyObj):
    """Model build control"""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return ModelBuildControl object with passed kwargs as attributes"""
        return cls(**kwargs)

class GeneticAlgorithmParams(CoreBodyObj):
    """The Genetic Algorithm parameters to be used in Auto Time Series"""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return GeneticAlgorithmParams object with passed kwargs as
        attributes.

        Dictionary at_least specifies that the arguments passed in must
        includes at least one of the feature in the Dictionary.

        """
        at_least = {
            "max_iteration",
            "generation_size",
            "mutation_rate",
            "crossover_rate",
        }
        if all(kwargs[x] is None for x in at_least):
            return None
        return cls(**kwargs)

class BuildControl(CoreBodyObj):
    """Model build control."""

    @classmethod
    @corex_obj(required={"genetic_algorithm"})
    def create(cls, **kwargs):
        """Return BuildControl object with passed kwargs as attributes"""
        return cls(**kwargs)

class ModelSpec(CoreBodyObj):
    """The model specification"""

    @classmethod
    @corex_obj(required={"endogenous_features"})
    def create(cls, **kwargs):
        """Return ModelSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class TSGroupBy(CoreBodyObj):
    """Time series group by."""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return TSGroupBy object with passed kwargs as attributes"""
        return cls(**kwargs)

class InputSpec(CoreBodyObj):
    """The input specification"""

    @classmethod
    @corex_obj(
        required={
            "train_data_id",
            "target",
            "datetime_column",
            "forecast_horizon",
            "gap",
        }
    )
    def create(cls, **kwargs):
        """Return InputSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class BuildSpec(CoreBodyObj):
    """Attribute for TrainAutoTSBody."""

    @classmethod
    @corex_obj(required={"genetic_algorithm"})
    def create(cls, **kwargs):
        """Return BuildSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class TrainAutoTSBody(CoreBodyObj):
    """Body for auto time series train api."""

    @classmethod
    @corex_obj(required={"build_spec", "input_spec"})
    def create(cls, **kwargs):
        """Return TrainAutoTSBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class PredictBody(CoreBodyObj):
    """Body for predict api."""

    @classmethod
    @corex_obj(required={"data_id", "model_id"})
    def create(cls, **kwargs):
        """Return PredictBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class PredictBodyTSModel(CoreBodyObj):
    """Body for predict time series forecast model api."""

    @classmethod
    @corex_obj(required={"data_id", "model_id"})
    def create(cls, **kwargs):
        """Return PredictBodyTSModel object with passed kwargs as attributes"""
        return cls(**kwargs)

class SetupBody(CoreBodyObj):
    """Body for setup api."""

    @classmethod
    @corex_obj(required={"data_source", "data_columns"})
    def create(cls, **kwargs):
        """Return SetupBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class Accessor(CoreBodyObj):
    """Data accessor"""

    @classmethod
    @corex_obj(required={"uri", "format"})
    def create(cls, **kwargs):
        """Return SetupBody object with passed kwargs as attributes"""
        return cls(**kwargs)

def column_array(cols):
    """Turn Josn columns to list of Column objects."""
    if cols is None:
        return None
    result = []
    for col in cols:
        nullable = col.get("nullable", None)
        try:
            result.append(
                Column(id=col["id"], data_type=col["data_type"], nullable=nullable)
            )
        except KeyError as err:
            raise ValueError("missing required value in Column %s" % err)
    return result

def cv_obj_array(cv):
    """Turn JSON cv to list of CVObjects."""

    def build_cv_obj(cv):
        cv_objs = []
        for cv_obj in cv:
            try:
                cv_objs.append(Column(train=cv_obj["train"], test=cv_obj["test"]))
            except KeyError as err:
                raise ValueError("missing required value in cv types %s" % err)
        return cv_objs

    return None if cv is None else build_cv_obj(cv)


# coding=<utf8>

import serial
import time

ser = serial.Serial("/dev/ttyAMA0", 9600)

Payload = {'iTOW' : 0,
               'lon' : 0,
               'lat' : 0,
               'height' : 0,
               'hMSL' : 0,
               'hAcc' : 0,
               'vAcc' : 0}
CK_A = 0
CK_B = 0
UBX_NAV_VELNED = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x12,
               'Length' : 36,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_SOL = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x06,
               'Length' : 52,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

numCh = 0
UBX_NAV_SVINFO = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x30,
               'Length' : 8 + 12 * numCh,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_STATUS = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x03,
               'Length' : 16,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_POSLLH = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x02,
               'Length' : 28,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

def getData():
    count = ser.inWaiting()
    if count != 0:
        data = ser.read(911)  ####################################################################各帧不按顺序发送？
        if parseData(data) == 0:
            print("parseData failed!")
    else:
        data = getData()
    return data

def parseData(data):
    data = map(ord, data)
    indexs = []
    i = 0
    flag = 0
    start = 0
    end = 0
    for byte in data:
        if byte == 0xB5:
            indexs.append(i)
        i += 1
    if indexs is None:
        return 0
    for index in indexs:
        if index + 24 < len(data):
            if data[index + 1] == 0x62:
                # find start frame and end frame
                if data[index + 3] == 0x12:
                    if flag == 0:
                        flag += 1
                        start = index
                    elif flag == 1:
                        end = index
                        flag += 1

    j = 0
    # get 5 complete frames
    for index in indexs:
        if index < start or index >= end:
            indexs.pop(j)
        j += 1

    for index in indexs:
        if index + 24 > len(data):
            return 0
        if data[index + 3] == 0x12:
            print("UBX_NAV_VELNED")
        elif data[index + 3] == 0x06:
            print("UBX_NAV_SOL")
        elif data[index + 3] == 0x30:
            print("UBX_NAV_SVINFO")
        elif data[index + 3] == 0x03:
            print("UBX_NAV_STATUS")
        elif data[index + 3] == 0x02:
            print("UBX_NAV_POSLLH")
            UBX_NAV_POSLLH['Payload']['lon'] = ((data[index + 8] << 24)
                                                + (data[index + 7] << 16)
                                                + (data[index + 6] << 8)
                                                + (data[index + 5])) / 10000000 * 3600
            UBX_NAV_POSLLH['Payload']['lat'] = ((data[index + 12] << 24)
                                                + (data[index + 11] << 16)
                                                + (data[index + 10] << 8)
                                                + (data[index + 9])) / 10000000 * 3600
    return 1

def getGPS():
    while True:
        getData()
        print("lon={},lat={}".format(UBX_NAV_POSLLH['Payload']['lon'], UBX_NAV_POSLLH['Payload']['lat']))
        time.sleep(0.1)

if __name__ == '__main__':
    try:
        getGPS()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()


import setting as s
import math
import os
import glob
from mutagen.mp3 import MP3
from line.messenger import push_message
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

# GoogleDrive共有フォルダID
MUSIC_FOLDER_ID = s.MUSIC_FOLDER_ID
VIDEO_FOLDER_ID = s.VIDEO_FOLDER_ID

# コンテンツ
def uploader(tag,data,dir,lineapi,lineid):

    scope = "https://www.googleapis.com/auth/drive"
    json_file = glob.glob("credentials/*.json")[0]
    gauth = GoogleAuth()
    gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scope)
    drive = GoogleDrive(gauth)

    video_ext_dicts = {
        '.mp4':'video/mp4',
        '.webm':'video/webm',
        '.mkv':'video/x-matroska'
    }
    title = data.lstrip(dir)
    if tag == "/mp3":
        f = drive.CreateFile(
            {
            'title': title,
            'mimeType': 'audio/mpeg',
            'parents':
                [{'kind': 'drive#fileLink', 'id':MUSIC_FOLDER_ID}]
            }
        )
        file_length = MP3(data).info.length
        dur = math.floor(file_length * 1000)
    else:
        dur = None
        _, ext = os.path.splitext(data)
        mimeType = video_ext_dicts.get(ext)

        f = drive.CreateFile(
            {
            'title': title,
            'mimeType': mimeType,
            'parents':
                [{'kind': 'drive#fileLink', 'id':VIDEO_FOLDER_ID}]
            }
        )

    f.SetContentFile(dir + f['title'])
    # GoogleDriveにアップロード
    f.Upload()

    # GoogleDriveのファイルIDを取得
    file_id = drive.ListFile(
        {'q': 'title =\"' + title +  '\"'}
    ).GetList()[0]['id']

    link = f"https://drive.google.com/uc?export=view&id={file_id}"
    push_message(link,lineid,tag,title,dur,lineapi)
    os.remove(data)


from abc import ABCMeta, abstractmethod

class TransformerBase(object):

    __metaclass__ = ABCMeta

    RENAME_DATA = None

    def __init__(self, data):

        self.data = data
        self.transformed = False

    def _rename_keys(self, containing_frame, path, new_name):

        if isinstance(containing_frame, list):
            for item in containing_frame:
                self._rename_keys(item, path, new_name)

            return

        sections = path.split(".")

        if len(sections) == 1:
            try:
                self._rename_key(containing_frame, path, new_name)
            except KeyError:
                pass

            return

        current_key = sections[0]
        remaining_path = ".".join(sections[1:])

        # Ignore wrong paths
        try:
            containing_frame = containing_frame[current_key]
        except KeyError:
            return

        self._rename_keys(containing_frame, remaining_path, new_name)

    def _rename_key(self, frame, key_name, new_name):
        frame[new_name] = frame[key_name]
        del frame[key_name]

    def _del_keys(self, containing_frame, path):

        if isinstance(containing_frame, list):
            for item in containing_frame:
                self._del_keys(item, path)

            return

        sections = path.split(".")

        if len(sections) == 1:
            try:
                del containing_frame[path]
            except KeyError:
                pass

            return

        current_key = sections[0]
        remaining_path = ".".join(sections[1:])

        # Ignore wrong paths
        try:
            containing_frame = containing_frame[current_key]
        except KeyError:
            return

        self._del_keys(containing_frame, remaining_path)

    def _rename_transformation(self):

        for path, new_name in self.RENAME_DATA.items():
            self._rename_keys(self.data, path, new_name)

        return self.data

    def _del_transformation(self):

        try:
            paths = getattr(self, "DEL_DATA")
        except AttributeError:
            return

        for path in paths:
            self._del_keys(self.data, path)

        return self.data

    @abstractmethod
    def rearrange_transformation(self):
        """
        Implement custom schema changes in this function
        If no transformations schema wise, return data as is
        """
        return

    def transform(self):

        if self.transformed:
            return self.data

        self._del_transformation()
        self._rename_transformation()
        self.rearrange_transformation()
        self.transformed = True

        return self.data


import numpy as np
from shapely.geometry import Point

def possibly_intersecting(dataframebounds, geometry, buffer=0):
    """
    Efficiently determine possibly intersecting geometries using a bounding box.

    Finding intersecting profiles for each branch is a slow process in case of large datasets
    To speed this up, we first determine which profile intersect a square box around the branch
    With the selection, the intersecting profiles can be determines much faster.

    Parameters
    ----------
    dataframebounds : numpy.array
    geometry : shapely.geometry.Polygon
    """

    geobounds = geometry.bounds
    idx = (
        (dataframebounds[0] - buffer < geobounds[2])
        & (dataframebounds[2] + buffer > geobounds[0])
        & (dataframebounds[1] - buffer < geobounds[3])
        & (dataframebounds[3] + buffer > geobounds[1])
    )
    # Get intersecting profiles
    return idx

def find_nearest_branch(branches, geometries, method="overall", maxdist=5):
    """
    Determine nearest branch for each geometry.

    The nearest branch can be found by finding t from both ends (ends) or the nearest branch from the geometry
    as a whole (overall), the centroid (centroid), or intersecting (intersect).

    Parameters
    ----------
    branches : geopandas.GeoDataFrame
        Geodataframe with branches
    geometries : geopandas.GeoDataFrame
        Geodataframe with geometries to snap
    method='overall' : str
        Method for determine branch
    maxdist=5 : int or float
        Maximum distance for finding nearest geometry
    minoffset : int or float
        Minimum offset from the end of the corresponding branch in case of method=equal
    """
    # Check if method is in allowed methods
    allowed_methods = ["intersecting", "overall", "centroid", "ends"]
    if method not in allowed_methods:
        raise NotImplementedError(f'Method "{method}" not implemented.')

    # Add columns if not present
    if "branch_id" not in geometries.columns:
        geometries["branch_id"] = ""
    if "branch_offset" not in geometries.columns:
        geometries["branch_offset"] = np.nan

    if method == "intersecting":
        # Determine intersection geometries per branch
        geobounds = geometries.bounds.to_numpy().T
        for branch in branches.itertuples():
            selection = geometries.loc[
                possibly_intersecting(geobounds, branch.geometry)
            ].copy()
            intersecting = selection.loc[
                selection.intersects(branch.geometry).to_numpy()
            ]

            # For each geometry, determine offset along branch
            for geometry in intersecting.itertuples():
                # Determine distance of profile line along branch
                geometries.loc[geometry.Index, "branch_id"] = branch.Index

                # Calculate offset
                branchgeo = branch.geometry
                mindist = min(0.1, branchgeo.length / 2.0)
                offset = round(
                    branchgeo.project(
                        branchgeo.intersection(geometry.geometry).centroid
                    ),
                    3,
                )
                offset = max(mindist, min(branchgeo.length - mindist, offset))
                geometries.loc[geometry.Index, "branch_offset"] = offset

    else:
        branch_bounds = branches.bounds.to_numpy().T
        # In case of looking for the nearest, it is easier to iteratie over the geometries instead of the branches
        for geometry in geometries.itertuples():
            # Find near branches
            nearidx = possibly_intersecting(
                branch_bounds, geometry.geometry, buffer=maxdist
            )
            selection = branches.loc[nearidx]

            if method == "overall":
                # Determine distances to branches
                dist = selection.distance(geometry.geometry)
            elif method == "centroid":
                # Determine distances to branches
                dist = selection.distance(geometry.geometry.centroid)
            elif method == "ends":
                # Since a culvert can cross a channel, it is
                crds = geometry.geometry.coords[:]
                dist = (
                    selection["geometry"]
                    .apply(
                        lambda x: max(
                            x.distance(Point(*crds[0])), x.distance(Point(*crds[-1]))
                        )
                    )
                    .astype(float)
                )
                # dist = (
                #     selection.distance(Point(*crds[0]))
                #     + selection.distance(Point(*crds[-1]))
                # ) * 0.5

            # Determine nearest
            if dist.min() < maxdist:
                branchidxmin = dist.idxmin()
                geometries.loc[geometry.Index, "branch_id"] = dist.idxmin()
                if isinstance(geometry.geometry, Point):
                    geo = geometry.geometry
                else:
                    geo = geometry.geometry.centroid

                # Calculate offset
                branchgeo = branches.loc[branchidxmin, "geometry"]
                mindist = min(0.1, branchgeo.length / 2.0)
                offset = max(
                    mindist,
                    min(branchgeo.length - mindist, round(branchgeo.project(geo), 3)),
                )
                geometries.loc[geometry.Index, "branch_offset"] = offset


import logging
import os
from typing import List
from pydantic import BaseModel

import requests
import uvicorn
from dotenv import load_dotenv
from fastapi import BackgroundTasks, FastAPI, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from utils import chroma, openai, s3
from utils.types import CompletionObj, DeleteObj, NoteObj, PromptTooLongError, UploadObj

load_dotenv()

app = FastAPI()

def upload_notes(notes: List[NoteObj]):
    print("Start: uploading notes", len(notes))
    for note in notes:
        qa = chroma.tokenize(note)
        openai.summarize(qa, note)
    print("End: uploading notes")

@app.post("/upload")
async def upload(data: UploadObj, background_tasks: BackgroundTasks):
    background_tasks.add_task(upload_notes, data.notes)
    return {
        "success": True
    }

@app.post("/completion")
def completion(data: CompletionObj):
    context = []
    try:
        assistantMsg, context = openai.get_completion_q_a(data)
    except PromptTooLongError as e:
        print("Prompt too long", data.classId, data.noteId)
        assistantMsg = e.assistantMsg
    except Exception as e:
        print("Error in completion", data.classId, data.noteId, e)
        assistantMsg = "Sorry, but I've encountered rate limits from OpenAI. Please try again in a few seconds."
    print(context)
    return {
        "assistantMsg": assistantMsg,
        "context": context
    }

@app.post("/collection/delete")
def deleteCollection(data: DeleteObj):
    print("Start: deleting collection", data.classId)
    try:
        requests.post(
            f"{os.getenv('CHROMA_URL')}/delete_collection",
            json={
                "classId": data.classId,
                "noteId": data.noteId
            }
        )
        s3.delete_obj(data.classId, data.noteId)
        print("End: deleting collection", data.classId)
        return {
            "success": True
        }
    except Exception as e:
        print("Error in deleteCollection", data.classId, data.noteId, e)
        return {
            "success": False,
            "message": e
        }

@app.get("/")
def hello():
    return {
        "success": True
    }

class CompletionApp(BaseModel):
    prompt: str

@app.post("/completion/app")
def completion_app(data: CompletionApp):
    return {
        "assistantMsg": openai.get_completion_summary_for_app(data.prompt)
    }

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
	exc_str = f'{exc}'.replace('\n', ' ').replace('   ', ' ')
	logging.error(f"{request}: {exc_str}")
	content = {'status_code': 10422, 'message': exc_str, 'data': None}
	return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)

if os.getenv("RAILWAY_ENVIRONMENT") == "production":
    origins = ["https://www.jini.gg",]
else:
    origins = ["http://localhost:3000"]

app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_origins=origins,
    allow_methods=["*"],
    allow_headers=["*"],
)

if __name__ == "__main__":
    if os.getenv("RAILWAY_ENVIRONMENT") == "production":
        uvicorn.run("main:app", host="0.0.0.0",
                     port=int(os.getenv("PORT", 8000)), log_level="info", workers=4)
    else:
        uvicorn.run("main:app", host="0.0.0.0", port=8002,
        log_level="info", reload=True)


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 26 16:57:41 2018

@author: FVA3
"""
import numpy as np
from tqdm import tqdm
from math import sqrt
from collections import Counter
import scipy.stats as st

class Edge:
    def __init__(self,node1,node2,weight):
        node1.add_edge(self,weight)
        node2.add_edge(self,weight)
        self.node1 = node1
        self.node2 = node2
        self.weight = weight

class Node:
    def __init__(self, label,features = None, cgr = None):
        self.features = features
        self.label = label
        self.cgr = cgr
        self.edges = []
        self.weights = []
    def add_edge(self,node,weight):
        self.edges.append(node)
        self.weights.append(weight)

class RelativeNeighborhoodGraph:
    def __init__(self):
        self.nodes = []
        self.edges = []
        self.features = None
        self.labels = None
        self.dist_matrix = None
        self.adjacency_matrix = None
        pass

    def fit(self,features,labels,neighbors = 150, cgr = None):
        self.features = features
        self.labels = labels
        self.cgr = cgr
        self.counter =  Counter(labels)
        self.dist_matrix = self.pairwise_squared_dist_matrix(features)
        self.adjacency_matrix = np.zeros(self.dist_matrix.shape)
        self.class_matrix = self.same_class_matrix()
        self.create_graph(neighbors)
        self.weight_matrix = self.pairwise_weight_matrix()

    def create_graph(self, neighbors = 150):
        assert(np.any(self.dist_matrix))
        assert(np.any(self.features))
        for i in tqdm(range(self.dist_matrix.shape[0])):
            row = self.dist_matrix[i,:]
            nearests = np.argsort(row)[:neighbors]
            for j in nearests:
                dist = self.dist_matrix[i,j]
                has_edge = True
                for k in nearests:
                    if k != i and k != j:
                        dist_1 = self.dist_matrix[i,k]
                        dist_2 = self.dist_matrix[j,k]
                        if max(dist_1,dist_2) < dist:
                            has_edge = False
                if has_edge:
                    self.adjacency_matrix[i,j] = 1
                    self.adjacency_matrix[j,i] = 1
                    #self.edges.append(Edge(self.nodes[i],self.nodes[j],1/(1 + sqrt(self.dist_matrix[i,j]))))
        pass

    def cut_edges_and_cluster(self):
        dist_matrix = self.dist_matrix*self.adjacency_matrix
        #we cut the connections between objects of different classes
        self.cut_dist_matrix = np.multiply(dist_matrix,self.class_matrix)
        self.find_clusters()

    def find_clusters(self):
        self.clusters = []
        self.clusters_labels = []
        self.already_clustered = []
        for i in range(self.cut_dist_matrix.shape[0]):
            if i not in self.already_clustered:
                self.cur_cluster = [] # list(connections)
                self.search_depth(i)
                self.clusters.append(self.cur_cluster)
        for i in range(len(self.clusters)):
            self.clusters_labels.append(self.labels[self.clusters[i][0]])
        self.clusters = np.array(self.clusters)
        self.clusters_labels = np.array(self.clusters_labels)

    def search_depth(self, i):
        if i not in self.already_clustered:
            self.already_clustered.append(i)
            self.cur_cluster.append(i)
        row = np.where(self.cut_dist_matrix[i])[0]
        for obj in row:
            if obj not in self.already_clustered:
                self.already_clustered.append(obj)
                self.cur_cluster.append(obj)
                self.search_depth(obj)

    def pairwise_squared_dist_matrix(self,features):
        dot_product = np.matmul(features,np.transpose(features))
        squared_norm = np.diag(dot_product)
        distances = np.array([squared_norm,]*dot_product.shape[0]) - 2*np.diag(squared_norm) + np.array([squared_norm,]*dot_product.shape[0]).transpose() - 2*dot_product
        distances[np.where(distances < 0)] = 0
        return distances

    def pairwise_weight_matrix(self):
        weight_matrix = self.dist_matrix*self.adjacency_matrix
        for i in range(weight_matrix.shape[0]):
            for j in range(weight_matrix.shape[1]):
                weight_matrix[i,j] = 1/(1 + sqrt(weight_matrix[i,j])) if weight_matrix[i,j] > 0 else 0
        return weight_matrix

    def calculate_weight_of_cut_edges_for_each_i(self):
        '''
        Ji = sum of weight[i,j]*Ii in j, where Ii = 1 if j isn't from the same class
        of i
        In other words, Ji is the sum of weights of cut edges
        '''
        not_class_matrix = (self.class_matrix == False)
        self.cut_weights_vector = np.sum(not_class_matrix*self.weight_matrix,axis = 1)
        self.cut_weights_expectation = np.zeros(self.cut_weights_vector.shape)
        self.cut_weights_variance = np.zeros(self.cut_weights_vector.shape)
        self.normalized_cut_weights_vector = self.cut_weights_vector.copy()
        self.p_value = np.ones(self.cut_weights_vector.shape)*1000
        for i in range(self.cut_weights_expectation.shape[0]):
            proportion_of_class = self.counter[self.labels[i]]/self.labels.shape[0]
            self.cut_weights_expectation[i] = (1 - proportion_of_class)*np.sum(self.weight_matrix[i])
            self.cut_weights_variance[i] = proportion_of_class*(1 - proportion_of_class)*np.sum(self.weight_matrix[i]**2)
            if self.cut_weights_variance[i] == 0:
                self.normalized_cut_weights_vector[i] = -100
            else:
                self.normalized_cut_weights_vector[i] = (self.normalized_cut_weights_vector[i] - self.cut_weights_expectation[i])/sqrt(self.cut_weights_variance[i])
                self.p_value[i] = st.norm.cdf(self.normalized_cut_weights_vector[i])

    def _get_mislabelled_objects(self,risk = 0.05):
        z_score = st.norm.ppf(risk)
        return self.normalized_cut_weights_vector > z_score

    def mislabelled_objects(self,risk = 0.05):
        self.calculate_weight_of_cut_edges_for_each_i()
        return self._get_mislabelled_objects(risk)

    def same_class_matrix(self):
        matrix = np.array([self.labels,]*self.labels.shape[0])
        return matrix==matrix.transpose()

    def test_dist_matrix(self):
        t = np.array([[1,0,0],[0,1,0],[3,1,1]])
        d = self.pairwise_dist_matrix(t)
        result = np.sum(d < 0) == 0
        if result:
            result = d
        return result

    def report_separability(self):
        count = Counter(self.labels)
        for u in np.unique(self.clusters_labels):
            temp = np.where(self.cluster_labels == u)[0]
            print('Class: ', u)
            print('Number of clusters: ', temp.shape[0], 'Size of data :', count[u])
            print('Ratio: ', 1- temp.shape[0]/count[u])


from __future__ import division
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.contrib import messages

import pandas as pd
import csv
import numpy as np

# Create your views here.
def index(request):
    return render(request,"polls/index.html",
        {
        'title':"Penelitian",
        }
    )

def convert(request):
    if request.method == 'POST':
            file = request.FILES['csv_file']
            fs = FileSystemStorage(location='static/datasnp')
            filename = fs.save(file.name, file)
            file_url = fs.url(filename)

            df=pd.read_csv('.'+settings.STATIC_URL+'datasnp/'+filename) #import datasnp.csv
            df.columns=["ID", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10","V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21"]
            mrthreshold = float(request.POST.get('mrthreshold')) #nilai mrthreshold diinput user
            mr = pd.DataFrame(columns=['SNP', 'MissingRate'])
            for i in range(0, int(len(df))):  #counter dari baris (ID-SNP) 1 s.d. banyak data SNP (1546)
                missingrt = (df.iloc[i] == '--').sum()/len(df.iloc[i]) #menghitung missing rate setiap SNP
                mr = mr.append({'SNP': df.ID[i], 'MissingRate': missingrt}, ignore_index=True) #memasukkan nilai missing rate ke list mr

            filteredSNP = mr['MissingRate'] < mrthreshold  # Create dataframe with TRUE if MissingRate is less than 0.05
            mrSNP=df[filteredSNP]

            mrSNP.to_csv('static/datasnp/drop_mr.csv', index=False)

            df=pd.read_csv('.'+settings.STATIC_URL+'datasnp/drop_mr.csv') #import datasnp.csv
            df.columns=["ID", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10","V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21"]

            mafSNP = pd.DataFrame(columns=['SNP', 'MAF'])
            mafthreshold = float(request.POST.get('mafthreshold'))
            for i in range(0, int(len(df))):  #counter dari baris (ID-SNP) 1 s.d. banyak data SNP (1546)
                a=df.iloc[i][:].str.count("A").sum()
                t=df.iloc[i][:].str.count("T").sum()
                g=df.iloc[i][:].str.count("G").sum()
                c=df.iloc[i][:].str.count("C").sum()
                allelfreq=[a,t,g,c]
                allelfreq.sort(reverse=True)
                maf=allelfreq[1]/sum(allelfreq)
                mafSNP = mafSNP.append({'SNP': df.ID[i], 'MAF': maf}, ignore_index=True)

            filtermaf = mafSNP['MAF'] > mafthreshold  # Create variable with TRUE if MAF is less than 0.05
            mafSNP=df[filtermaf]
            mafSNP.to_csv('static/datasnp/drop_maf.csv', index=False)

            dfr=pd.read_csv('.'+settings.STATIC_URL+'datasnp/drop_maf.csv') #import datasnp.csv
            dfr.columns=["ID", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10","V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21"]
            df=dfr
            df = df.drop('ID', 1)
            listhwe = []
            hwethreshold = float(request.POST.get('hwethreshold')) #input nili hwe dari user
            for i in range(0, int(len(df))):  #counter dari baris (ID-SNP) 1 s.d. banyak data SNP (1546)
                a=df.iloc[i][:].str.count("A").sum() #menghitung jumlah allel A yang muncul di data SNP ke-i
                t=df.iloc[i][:].str.count("T").sum() #menghitung jumlah allel T yang muncul di data SNP ke-i
                g=df.iloc[i][:].str.count("G").sum() #menghitung jumlah allel G yang muncul di data SNP ke-i
                c=df.iloc[i][:].str.count("C").sum() #menghitung jumlah allel C yang muncul di data SNP ke-i
                allelfreq=[a,t,g,c]
                allelfreq.sort(reverse=True) #mengurutkan
                s=sum(allelfreq)
                allel=pd.DataFrame(columns=['p','q'])
                allel = allel.append({'p': (allelfreq[0])/s, 'q': (allelfreq[1])/s}, ignore_index=True)
                AAe = allel.p**2
                BBe = allel.q**2
                ABe = 2 * allel.p * allel.q

                gen_count=df.iloc[i].value_counts() #menghitung frekuensi kemunculan allel.
                a = gen_count.rename_axis('genotype').reset_index(name='counts')
                a = a[a.genotype != "--"].reset_index(drop=True)
                if len(a) > 2:
                    if a.genotype[0][0] == a.genotype[0][1]: #apakah allel frekuensi terbanyak pertama homozigot dominan?
                        AAo = a.counts[0]/a.counts.sum(axis=0) # homozygot dominan observed
                        if a.genotype[1][0] == a.genotype[1][1]: # apakah di urutan kedua homozigot?
                            BBo = a.counts[1]/a.counts.sum(axis=0) #homozygot resesif observed
                            ABo = a.counts[2]/a.counts.sum(axis=0) #heterozygot observed
                        else:
                            ABo = a.counts[1]/a.counts.sum(axis=0) #heterozygot observed
                            BBo = a.counts[2]/a.counts.sum(axis=0) #homozygot resesif observed
                    else:
                        ABo = a.counts[0]/a.counts.sum(axis=0)
                        AAo = a.counts[1]/a.counts.sum(axis=0)
                        BBo = a.counts[2]/a.counts.sum(axis=0)

                if len(a) == 2:
                    if a.genotype[0][0] == a.genotype[0][1]: #apakah allel frekuensi terbanyak pertama homozigot dominan?
                        AAo = a.counts[0]/a.counts.sum(axis=0) # homozygot dominan observed
                        if a.genotype[1][0] == a.genotype[1][1]: # apakah di urutan kedua homozigot?
                            BBo = a.counts[1]/a.counts.sum(axis=0) #homozygot resesif observed
                        else:
                            ABo = a.counts[1]/a.counts.sum(axis=0) #heterozygot observed
                    else:
                        ABo = a.counts[0]/a.counts.sum(axis=0)
                        AAo = a.counts[1]/a.counts.sum(axis=0)
                hwe=((AAo-AAe)**2/AAe)+((ABo-ABe)**2/ABe)+((BBo-BBe)**2/BBe)
                listhwe.append(hwe)

            listhwe = pd.DataFrame(listhwe)
            listhwe.columns=["HWE"]
            filterhwe = listhwe['HWE'] > hwethreshold
            hweSNP=dfr[filterhwe]
            hweSNP.to_csv('static/datasnp/drop_hwe.csv', index=False)

            df=pd.read_csv('.'+settings.STATIC_URL+'datasnp/drop_hwe.csv')
            df.columns=["ID","V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10","V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21"]

            for i in range(0, int(len(df))):  #counter dari baris (ID-SNP) 1 s.d. panjang SNP
                allel=df.iloc[i].drop('ID').value_counts()
                a = allel.rename_axis('allel').reset_index(name='counts')
                a = a[a.allel != "--"].reset_index(drop=True)
                if len(a) > 2:
                    if a.allel[0][0] == a.allel[0][1]: #apakah allel frekuensi terbanyak pertama homozigot?
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [0]) # urutan pertama homozgt => 0
                        if a.allel[1][0] == a.allel[1][1]: # apakah di urutan kedua homozigot?
                            df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [2]) #jika ya, ganti urutan kedua jadi 2
                            df.iloc[i]=df.iloc[i].replace([a.allel[2][0]+a.allel[2][1]], [1]) #urutan 3 jadi 1
                        else:
                            df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [1]) #jika tidak ganti 1
                            df.iloc[i]=df.iloc[i].replace([a.allel[2][0]+a.allel[2][1]], [2])
                    else:
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [1])
                        df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [0])
                        df.iloc[i]=df.iloc[i].replace([a.allel[2][0]+a.allel[2][1]], [2])
                if len(a) == 2:
                    if a.allel[0][0] == a.allel[0][1]: #apakah allel frekuensi terbanyak pertama homozigot?
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [0]) # urutan pertama homozgt => 0
                        if a.allel[1][0] == a.allel[1][1]: # apakah di urutan kedua homozigot?
                            df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [2]) #jika ya, ganti jadi 2
                        else:
                            df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [1]) #jika tidak ganti 1
                    else:
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [1])
                        df.iloc[i]=df.iloc[i].replace([a.allel[1][0]+a.allel[1][1]], [0])
                if len(a) == 1:
                    if a.allel[0][0] == a.allel[0][1]:
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [0])
                    else:
                        df.iloc[i]=df.iloc[i].replace([a.allel[0][0]+a.allel[0][1]], [1])
            df.to_csv('static/datasnp/snp_encoded.csv', index=False)

            messages.success(request, 'Silahkan Download data.')

            return HttpResponseRedirect(reverse('index'))


from util import *
import matplotlib.pyplot as plt

page_path= 'dataset/PALEOGRAPHY_TRAIN_AND_TEST_v3/TEST/orientalsquare/0129_FL12951770.jpg'
patches = sample_patches_from_page(page_path, number_of_patches=5)

for patch in patches:
    plt.imshow(patch)
    plt.show()


from django.shortcuts import render
# from django.shortcuts import redirect
# from django.http import HttpResponse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
# from django.views.decorators.csrf import csrf_protect
import base64
from PIL import Image
from io import BytesIO
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
import cv2
import json

def index(request):
    return render(request, 'classify/index.html')

@login_required
def home(request):
    return render(request, 'classify/home.html')

@login_required
def about(request):
    return render(request, 'classify/about.html')

@login_required
@csrf_exempt
def predict(request):
    if request.method == "POST" and request.FILES:
        file_object = request.FILES['filePath']
        messages.success(request, 'Classification Completed!')
        # print('Something Happened! REDIRECTING!', type(file_object))

        encoded = base64.b64encode(file_object.read())
        decoded = base64.b64decode(encoded)
        image = Image.open(BytesIO(decoded))
        image_np = np.array(image)
        resized = cv2.resize(image_np, (224, 224))
        normalized = resized / 255.0
        input_image = normalized.reshape(1, 224, 224, 3)
        # print(image_np.shape)
        # print(input_image.shape)

        model_graph = tf.compat.v1.Graph()
        with model_graph.as_default():
            tf_session = tf.compat.v1.Session()
            with tf_session.as_default():
                model = load_model('models/StrangerFaceNet_5024_100.h5')
                prediction = model.predict(input_image)
                idx = prediction.argmax(axis=1)[0]
                prob = (max(prediction[0])*100).round(3)

        with open('models/classes.json', 'r') as f:
            classes = f.read()

        classes = json.loads(classes)

        person = (classes[str(idx)])[1]

        # print(prediction, idx, prob, person)

        context = {'person': person, 'confidence': prob}
        return render(request, 'classify/home.html', context)
    else:
        return render(request, 'classify/home.html')


#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets, permissions
from OpsManage.serializers import *
from OpsManage.models import *
from rest_framework import status
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.contrib.auth.decorators import permission_required
from django.db.models import Sum, Count

@api_view(['GET', 'PUT'])
@permission_required('OpsManage.can_read_tf_asset_soft_info', raise_exception=True)
def tf_asset_soft_num(request, type, format=None):
    try:
        snippet = TF_Asset_Soft_Info.objects.filter(sw_type=type).values('sw_name').annotate(sw_sum=Count('sw_name'))
    except TF_Asset_Soft_Info.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        data = []
        for item in snippet:
            tmp = {}
            tmp['sw_name'] = item['sw_name']
            tmp['sw_sum'] = item['sw_sum']
            data.append(tmp)
        return Response(data)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_required('OpsManage.can_read_tf_asset_soft_info', raise_exception=True)
def tf_asset_soft_name(request, name, format=None):
    try:
        snippet = TF_Asset_Soft_Info.objects.filter(sw_name=name)
    except TF_Asset_Soft_Info.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = TfAssetSoftInfoSerializer(snippet, many=True)
        return Response(serializer.data)


# This example demonstrates upscaling an existing video using the tools provided in this package.
# This is not a primary use case for the package, but it may be useful. It also illustrates how you
# may prefer to integrate shell commands to ffmpeg into your video workflows, rather than using
# generate.video_tools methods.
# Note that Real-ESRGAN will run out of VRAM if you try to upscale a large video.

from vqgan_clip import esrgan, video_tools
import os

input_video_path = 'small_video.mp4'
output_root_dir = 'example media'
final_output_filename = f'{output_root_dir}{os.sep}upscaled video.mp4'
extracted_video_frames_path = f'{output_root_dir}{os.sep}extracted video frames'
upscaled_video_frames_path = f'{output_root_dir}{os.sep}upscaled video frames'
extraction_framerate = 30

# Use a wrapper for FFMPEG to extract stills from the original video.
original_video_frames = video_tools.extract_video_frames(input_video_path,
                                                         extraction_framerate=extraction_framerate,
                                                         extracted_video_frames_path=extracted_video_frames_path)

# This is equivalent to
# os.system(f'ffmpeg -i small_video.mp4 -filter:v fps=30 video_frames\\frame_%12d.jpg')

# Upscale using Real-ESRGAN
esrgan.inference_realesrgan(input=extracted_video_frames_path,
                            output_images_path=upscaled_video_frames_path,
                            face_enhance=False,
                            # Careful! This will delete everything in the output_images_path!
                            purge_existing_files=True,
                            model_filename='RealESRGAN_x2plus.pth',
                            model_url='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
                            netscale=2,
                            outscale=2)

# Encode the video.
generated_video_no_audio = f'{output_root_dir}{os.sep}output no audio.mp4'
ffmpeg_input_path = f'\"{upscaled_video_frames_path}{os.sep}frame_%12d.jpg\"'
os.system(
    f'ffmpeg -y -f image2 -i {ffmpeg_input_path} -r 30 -vcodec libx264 -crf 23 -pix_fmt yuv420p -strict -2 \"{generated_video_no_audio}\"')

# Copy audio from the original file
video_tools.copy_video_audio(
    input_video_path, generated_video_no_audio, final_output_filename)
os.remove(generated_video_no_audio)

# This is equiavalent to
# os.system(f'ffmpeg -i small_video.mp4 -vn -acodec copy extracted_original_audio.aac')
# os.system(f'ffmpeg -i output_no_audio.mp4 -i extracted_original_audio.aac -c copy -map 0:v:0 -map 1:a:0 upscaled_video.mp4')


import math as m

## FKM-Richtlinie Nichtlinear
## Rechnerischer Festigkeitsnachweis unter expliziter Erfassung nichtlinearen Werkstoffverformungsverhaltens

## Statischer Nachweis

# Festlegung der elastisch-plastischen Ergebnisse aus der FEM-Rechnung
sigma_xx = 280.0
sigma_yy = 90.0
sigma_zz = 50.0
tau_xy = 100.0
tau_yz = 80.0
tau_zx = 40.0

# Von Mises Vergleichsspannung
sigma_V = 1.0/m.sqrt(2) * m.sqrt((sigma_xx-sigma_yy)**2 + (sigma_yy-sigma_zz)**2 + (sigma_zz-sigma_xx)**2 + 6 * (tau_xy**2 + tau_yz**2 + tau_zx**2))
# Hydrostatische Spannung
sigma_H = 1.0/3.0 * (sigma_xx + sigma_yy + sigma_zz)
# Spannungsmehrachsigkeitsgrad
h = sigma_H/sigma_V

# Versagensgrenzkurve


import cv2
import numpy as np

o = cv2.imread('2.jpg')
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,
 cv2.RETR_TREE,
 cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[0]
#hull = cv2.convexHull(cnt,returnPoints = False)
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)

for i in range(defects.shape[0]):
    s,e,f,d = defects[i,0]
    start = tuple(cnt[s][0])           # 得到的是索引，要再轮廓中选出来
    end = tuple(cnt[e][0])
    far = tuple(cnt[f][0])
    cv2.line(o,start,end,[0,0,255],2)
    cv2.circle(o,far,5,[255,0,0],-1)

cv2.imshow('result',o)
cv2.waitKey(0)
cv2.destroyAllWindows()


import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()


def insertion_sort(elements):
    for i in range(1, len(elements)):
        anchor = elements[i]
        j = i - 1
        while anchor < elements[j] and j >= 0:
            elements[j+1] = elements[j]
            j -= 1
        elements[j+1] = anchor

def median(elements):
    length = len(elements)
    if length % 2 == 0:
        print('Elements: ', elements, "\nMedian: ", (elements[length//2] + elements[(length//2) - 1])/2, "\n\n")
    else:
        print('Elements: ', elements, "\nMedian: ", elements[length//2], '\n\n')

def insertion_sort_median(elements):
    for i in range(1, len(elements)):
        anchor = elements[i]
        j = i - 1
        while anchor < elements[j] and j >= 0:
            elements[j+1] = elements[j]
            j -= 1
        elements[j+1] = anchor

        median(elements[:i+1])

if __name__ == '__main__':
    elements = [11,9,29,7,2,15,28]
    print(elements)
    #
    tests = [
        [11,9,29,7,2,15,28],
        [3, 7, 9, 11],
        [25, 22, 21, 10],
        [29, 15, 28],
        [],
        [6]
    ]

    for elements in tests:
        insertion_sort(elements)
        print(f'sorted array: {elements}')

    elements = [11,9,29,7,2,15,28]
    insertion_sort_median(elements)

# Exercise: Insertion Sort
# Compute the running median of a sequence of numbers.
# That is, given a stream of numbers, print out the median of the list so far on each new element.
#
# Recall that the median of an even-numbered list is the average of the two middle numbers in a sorted list.
#
# For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out:
#
# 2
# 1.5
# 2
# 3.5
# 2
# 2
# 2


import socket
from typing import Tuple

class Server:
    def __init__(self, port: int):
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serversocket.bind(('', 8080))
        serversocket.listen(10)
        self.serversocket = serversocket

    def put_point(self, position: Tuple[float, float]):
        (x, y) = position
        self.serversocket.send()


class Stores(list):
  """ One or more stores retrieved from the storage. """
  def __str__(self):
    """ Can be used as URL parameter. """
    lines = []

    for store in self:
      latitude = str(store[1])
      longitude = str(store[2])
      lines.append(latitude + ',' + longitude)

    return '|'.join(lines)

  def __getslice__(self, i, j):
    """ Helpful when amount of stores to process is limited. """
    list_slice = super(Stores, self).__getslice__(i, j)
    return Stores(list_slice)


#TO update the values of keys
a={1:2,  "Sumit": "A coder"    ,   "Mobile":"IQOO"}
a["Mobile"]="Apple"                                       #method 1
(a.update({"Sumit":"A gamer"}))                           #method 2
print(a)                                                #in both method values of keys are replaced so old values of the keys are no
                                                                            #longer in dictonary

# to add the keys and values in dictonary

a["Pen"]="Doms"
(a.update({"Laptop":"Dell"}))
print(a)
sj={
    "butter":"Chicken", "home":"mathura"
}
a.update(sj)
print(a)


import torch
from torchvision.transforms.functional import to_tensor
from PIL import Image

from recognite.utils import three_crop, collate_three_crops,\
    embeddings_three_crops

def test_three_crop_horizontal():
    exp_start = Image.new('RGB', (100, 100), (255, 0, 0))
    exp_center = Image.new('RGB', (100, 100), (0, 255, 0))
    exp_end = Image.new('RGB', (100, 100), (0, 0, 255))

    img = Image.new('RGB', (300, 100))
    img.paste(exp_start, (0, 0))
    img.paste(exp_center, (100, 0))
    img.paste(exp_end, (200, 0))
    img = to_tensor(img)

    ret_start, ret_center, ret_end = three_crop(img)

    assert (ret_start == to_tensor(exp_start)).all()
    assert (ret_center == to_tensor(exp_center)).all()
    assert (ret_end == to_tensor(exp_end)).all()

def test_three_crop_vertical():
    exp_start = Image.new('RGB', (100, 100), (255, 0, 0))
    exp_center = Image.new('RGB', (100, 100), (0, 255, 0))
    exp_end = Image.new('RGB', (100, 100), (0, 0, 255))

    img = Image.new('RGB', (100, 300))
    img.paste(exp_start, (0, 0))
    img.paste(exp_center, (0, 100))
    img.paste(exp_end, (0, 200))
    img = to_tensor(img)

    ret_start, ret_center, ret_end = three_crop(img)

    assert (ret_start == to_tensor(exp_start)).all()
    assert (ret_center == to_tensor(exp_center)).all()
    assert (ret_end == to_tensor(exp_end)).all()

def test_collate_with_three_crops():
    B, T, C, H, W = 10, 3, 1, 50, 100
    tuple_batch = [
        (torch.randn(T, C, H, W), 0)
        for _ in range(B)
    ]
    ret_3_crops, ret_labels = collate_three_crops(tuple_batch)

    assert ret_3_crops.shape == (B, T, C, H, W)
    assert ret_labels.shape == (B,)

def test_get_embeddings_three_crops():
    crops_1 = torch.stack([
        to_tensor(Image.new('RGB', (100, 100), (255, 0, 0))),
        to_tensor(Image.new('RGB', (100, 100), (0, 255, 0))),
        to_tensor(Image.new('RGB', (100, 100), (0, 0, 255)))
    ])
    crops_2 = torch.stack([
        to_tensor(Image.new('RGB', (100, 100), (255, 255, 0))),
        to_tensor(Image.new('RGB', (100, 100), (0, 255, 255))),
        to_tensor(Image.new('RGB', (100, 100), (255, 0, 255)))
    ])
    batch = torch.stack([crops_1, crops_2])

    mean_crops_1 = (crops_1[0] + crops_1[1] + crops_1[2])/3
    mean_crops_2 = (crops_2[0] + crops_2[1] + crops_2[2])/3
    exp_embs = torch.stack([mean_crops_1, mean_crops_2])

    ret_embs = embeddings_three_crops(torch.nn.Identity(), batch)

    assert (exp_embs == ret_embs).all()


def sys_openat(self, dirfd, buf, flags, mode):
        """
        A version of openat that includes a symbolic path and symbolic directory file descriptor

        :param dirfd: directory file descriptor
        :param buf: address of zero-terminated pathname
        :param flags: file access bits
        :param mode: file permission mode
        """

        if issymbolic(dirfd):
            logger.debug("Ask to read from a symbolic directory file descriptor!!")
            # Constrain to a valid fd and one past the end of fds
            self.constraints.add(dirfd >= 0)
            self.constraints.add(dirfd <= len(self.files))
            raise ConcretizeArgument(self, 0)

        if issymbolic(buf):
            logger.debug("Ask to read to a symbolic buffer")
            raise ConcretizeArgument(self, 1)

        return super().sys_openat(dirfd, buf, flags, mode)


#!/usr/bin/env python3
import numpy as np

import rospy
from geometry_msgs.msg import Twist
from hwctrl.msg import MotorCmd, DriveMotorCmd

# Make these members of class later
failed = False
try:
    wheel_radius = rospy.get_param('wheel_radius')
    effective_robot_width = rospy.get_param("effective_robot_width")
except KeyError:
    failed = True

class CmdToWheelSpeedsNode:
    def __init__(self):
        rospy.init_node("cmd_to_wheel_speeds_node", anonymous=False)

        if failed:
            rospy.logerr("Parameters could not be found, please ensure they are on the param server")
            rospy.spin()

        self.motor_acceleration = rospy.get_param('/motor_command_accel')

        self.motor_setpoint_pub = rospy.Publisher("/glenn_base/motor_cmds", DriveMotorCmd, queue_size=2)
        rospy.Subscriber("/glenn_base/cmd_vel", Twist, self.cmd_vel_callback, queue_size=1)

        rospy.spin()

    def cmd_vel_callback(self, msg):
        vel = msg.linear.x
        angular_vel = msg.angular.z

        right_speed = (vel + angular_vel * effective_robot_width / 2) * 30 / (np.pi * wheel_radius)
        left_speed = (vel - angular_vel * effective_robot_width / 2) * 30 / (np.pi * wheel_radius)

        right_cmd = MotorCmd(setpoint=right_speed, acceleration=self.motor_acceleration)
        left_cmd = MotorCmd(setpoint=left_speed, acceleration=self.motor_acceleration)
        self.motor_setpoint_pub.publish(right=right_cmd, left=left_cmd)