import math
from collections import defaultdict
import gc

import networkx as nx
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Embedding
from sklearn.metrics import roc_auc_score
from parlai.utils.misc import round_sigfigs

from logger import Logger
from tqdm import tqdm
from time import time

from attnio_model_new import AttnIO

def _get_instance_path_loss(graph, path):
    epsilon = 1e-30
    # denominator = (graph.num_nodes()*epsilon+1)
    scores = []
    for i in range(len(path)):
        # node_time_scores = ((graph.ndata["a_"+str(i)])/denominator) + (epsilon/denominator)
        node_time_scores = graph.ndata["a_"+str(i)] + epsilon
        node_time = path[i]
        score = node_time_scores[node_time]
        scores.append(-torch.log(score))
    scores = torch.stack(scores)
    return scores.sum(-1)

class AttnIOModel(nn.Module):
    def __init__(self, opt):
        super(AttnIOModel, self).__init__()

        self.device = opt["device"]
        self.n_entity = opt["n_entity"]
        self.n_relation = opt["n_relation"]
        self.out_dim = opt["out_dim"]
        self.in_dim = opt["in_dim"]
        self.lr = opt["lr"]
        self.lr_reduction_factor = opt["lr_reduction_factor"]
        self.epochs = opt["epoch"]
        self.attn_heads = opt["attn_heads"]
        self.beam_size = opt["beam_size"]
        self.clip = opt["clip"]
        self.self_loop_id = opt["self_loop_id"]

        self.model_directory = opt["model_directory"]
        self.model_name = opt["model_name"]

        self.entity_emb = Embedding(self.n_entity, 768)
        self.entity_emb.weight.data.copy_(opt["entity_embeddings"])

        self.relation_emb = Embedding(self.n_relation, 768)
        self.relation_emb.weight.data.copy_(opt["relation_embeddings"])
        self.model = AttnIO(self.in_dim, self.out_dim, self.attn_heads, self.entity_emb, self.relation_emb, self.self_loop_id, self.device)

        self.optimizer = torch.optim.Adam( filter(lambda p: p.requires_grad, self.parameters()), self.lr)
        self.lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(self.optimizer, patience=10)

        self.metrics = defaultdict(float)
        self.counts = defaultdict(int)
        self.reset_metrics()

    def reset_metrics(self):
        for key in self.metrics:
            self.metrics[key] = 0.0
        for key in self.counts:
            self.counts[key] = 0

    def report(self):
        m = {}
        # Top-k recommendation Recall
        for x in sorted(self.metrics):
            if x.startswith("recall"):
                m[x] = self.metrics[x] / self.counts[x]

        for k, v in m.items():
            # clean up: rounds to sigfigs and converts tensors to floats
            m[k] = round_sigfigs(v, 4)
        return m

    def forward(self, dialogue_representation, seed_entities, subgraph):
        subgraph = self.model(subgraph, seed_entities, dialogue_representation)
        return subgraph

    def get_batch_data(self, batch):
        dialogue_representations, seed_entities, subgraphs, paths = batch[0], batch[1], batch[2], batch[3]
        return dialogue_representations, seed_entities, subgraphs, paths

    def process_batch(self, batch, train=False):
        dialogue_representations, seed_entities, subgraphs, paths = self.get_batch_data(batch)
        batch_size = len(batch[0])
        batch_loss = []
        updated_subgraphs = []
        for i in range(batch_size):
            dialogue_representation = dialogue_representations[i].to(self.device)
            seed_entity = seed_entities[i].to(self.device)
            subgraph = subgraphs[i].to(self.device)
            path = paths[i].to(self.device)
            updated_subgraph = self(dialogue_representation, seed_entity, subgraph)
            updated_subgraphs.append(updated_subgraph)
            instance_loss = _get_instance_path_loss(updated_subgraph, path)
            batch_loss.append(instance_loss)

        batch_loss = torch.stack(batch_loss).sum(-1)/batch_size

        # print(batch_loss.item())
        if train:
            self.optimizer.zero_grad()
            batch_loss.backward()
            torch.nn.utils.clip_grad_norm_(self.parameters(), 5)
            self.optimizer.step()

        return batch_loss.item(), updated_subgraphs

    def train_model(self, trainDataLoader, devDataLoader):
        # logger = Logger("logs/")
        self.optimizer.zero_grad()
        ins=0
        for epoch in range(self.epochs):
            self.train()
            train_loss = 0
            cnt = 0
            for idx, batch in tqdm(enumerate(trainDataLoader)):
                batch_loss, _ = self.process_batch(batch, train=True)
                train_loss += batch_loss
                cnt+=1

            train_loss = train_loss/cnt
            cnt = 0
            dev_loss = 0
            for idx, batch in tqdm(enumerate(devDataLoader)):
                batch_loss, _ = self.process_batch(batch, train=False)
                dev_loss += batch_loss
                cnt+=1
            dev_loss = dev_loss/cnt

            self.lr_scheduler.step(dev_loss)
            print("Epoch: %d, Train Loss: %f" %(epoch, train_loss))
            print("Epoch: %d, Dev Loss: %f" %(epoch, dev_loss))

            # Logging parameters
            p = list(self.named_parameters())
            # logger.scalar_summary("Train Loss", train_loss, ins+1)
            # logger.scalar_summary("Dev Loss", dev_loss, ins+1)
            ins+=1
            if (epoch+1)%2==0:
                torch.save(self.state_dict(), self.model_directory+self.model_name+"_"+str(epoch+1))


from django.conf.urls import patterns, url

from calorietracker import views, import_old_db

urlpatterns = patterns('',
                       url(r'^$', views.index, name='index'),
                       url(r'^jstree.json$', views.jstree_json, name='jstree.json'),
                       url(r'^import_old_db$', import_old_db.import_old_db, name='import_old_db'),
                       url(r'^calories.json$', views.calories_json, name='calories.json'),
                       url(r'^invalidate_cache$', views.invalidate_cache, name='invalidate_cache'),
                       url(r'^food.json$', views.food_json, name='food.json'),
                       url(r'^calorie_graph.js$', views.calorie_graph_js, name='calorie_graph.js'),
                       url(r'^meal_tree.js$', views.meal_tree_js, name='meal_tree.js'),
                       url(r'^add_meal$', views.add_meal, name='add_meal'),
                       url(r'^delete_meal$', views.delete_meal, name='delete_meal'),
                       url(r'^weights.json$', views.weights_json, name='weights.json'),
                       url(r'^weight_graph.js$', views.weight_graph_js, name='weight_graph.js'),
                       url(r'^add_weight', views.add_weight, name='add_weight'),
                       url(r'^mobile',views.mobile,name='mobile'),

)


import sys
input = sys.stdin.readline

def sol(c):
    if c == 'Z':
        return 'A'
    return chr(ord(c)+1)

for t in range(int(input())):
    name = list(map(sol, input().rstrip()))
    if t > 0:
        print()
    print('String #'+str(t+1))
    print(''.join(name))


# -*- coding: utf-8 -*-

""" Tests for the `piecewiselinear` module."""

import pytest
from pytest import approx
import numpy as np
from scipy.integrate import quad
from ufoldy.piecewisefunction import PLF, PCF

def argsort(seq):
    return sorted(range(len(seq)), key=seq.__getitem__)

@pytest.fixture
def small():
    x = [0, 1, 3]
    y = [1, 2, -1]

    norm = 2.5
    integral13 = 1.0
    integral15 = 1.0

    return x, y, norm, integral13, integral15

def test_PLF_init_from_sorted_list(small):
    x, y, *r = small

    p = PLF(x, y)

    for i in range(len(x)):
        assert x[i] == p.x[i]
        assert y[i] == p.y[i]

def test_PLF_init_from_unsorted_list(small):
    x, y, *r = small

    x = x[::-1]
    y = y[::-1]

    p = PLF(x, y)

    xx = x.copy()
    yy = y.copy()

    idx = argsort(xx)

    for i in range(len(x)):
        assert xx[idx[i]] == p.x[i]
        assert yy[idx[i]] == p.y[i]

def test_PLF_init_wrong_xlist():
    x = []
    y = [1, 2, 3]

    with pytest.raises(ValueError):
        p = PLF(x, y)

def test_PLF_init_wrong_ylist():
    x = [1, 2, 3]
    y = []

    with pytest.raises(ValueError):
        p = PLF(x, y)

def test_PLF_init_noncompatlist():
    x = [1, 2]
    y = [1, 2, 3]

    with pytest.raises(ValueError):
        p = PLF(x, y)

def test_PLF_init_nparray(small):
    x, y, *r = small

    xa = np.array(x)
    ya = np.array(y)

    p = PLF(xa, ya)

    for i in range(len(x)):
        assert xa[i] == p.x[i]
        assert ya[i] == p.y[i]

def test_PLF_init_x2darray():
    x = np.array([[1, 2], [3, 4]])
    y = np.array([1, 2, 3, 4])

    with pytest.raises(ValueError):
        p = PLF(x, y)

def test_PLF_init_y2darray():
    x = np.array([1, 2, 3, 4])
    y = np.array([[1, 2], [3, 4]])

    with pytest.raises(ValueError):
        p = PLF(x, y)

def test_PLF_evaluate(small):
    x, y, *r = small

    p = PLF(x, y)

    assert p(-1) == approx(0.0)
    assert p(0) == approx(1.0)
    assert p(0.5) == approx(1.5)
    assert p(1) == approx(2)
    assert p(2) == approx(0.5)
    assert p(7 / 3) == approx(0.0)
    assert p(3) == approx(-1.0)
    assert p(4) == approx(0.0)

    assert p([-1, 0, 0.5, 1, 2, 7 / 3, 3, 4]) == approx(
        np.array([0.0, 1.0, 1.5, 2.0, 0.5, 0.0, -1.0, 0.0])
    )

def test_PLF_init_normalized(small):
    x, y, *r = small

    p = PLF(x, y, normalize=True)

    assert p.norm() == approx(1.0)

def test_PLF_init_normalized_to_number(small):
    x, y, *r = small

    p = PLF(x, y, normalize=True, normvalue=8.0)

    assert p.norm() == approx(8.0)

def test_PLF_init_emptyxy():
    p = PLF([], [])

    assert p.x[0] == approx(0.0)
    assert p.x[1] == approx(1.0)
    assert p.y[0] == approx(0.0)
    assert p.y[1] == approx(0.0)

def test_PLF_xsetter(small):
    x, y, *r = small

    p = PLF(x, y)

    with pytest.raises(ValueError):
        p.x = [-1]

def test_PLF_ysetter(small):
    x, y, *r = small

    p = PLF(x, y)

    p.y = [1, 1, 3]

    assert p.y[0] == approx(1.0)

    assert p(0.5) == approx(1.0)

def test_PLF_ysetter_wrongshape(small):
    x, y, *r = small

    p = PLF(x, y)

    newy = [1, 2]

    with pytest.raises(ValueError):
        p.y = newy

def test_PLF_slopes_setter(small):
    x, y, *r = small

    p = PLF(x, y)

    with pytest.raises(ValueError):
        p.slopes = 1.0

def test_PLF_str(small):
    x, y, *r = small

    p = PLF(x, y)

    strval = "[+0.0000e+00, +1.0000e+00, +3.0000e+00]\n[+1.0000e+00, +2.0000e+00, -1.0000e+00]"

    assert f"{p}" == strval

def test_PLF_norm(small):
    x, y, norm, *r = small

    p = PLF(x, y)

    assert norm == approx(p.norm())

def test_PLF_norm_partial1(small):
    x, y, *r = small

    p = PLF(x, y)

    assert p.norm(1) == approx(1.0)

def test_PLF_norm_partial2(small):
    x, y, *r = small

    p = PLF(x, y)

    assert p.norm(0, 1) == approx(1.5)

def test_PLF_insert_node_before_left(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes(-1, -1)

    assert p(-0.5) == approx(0.0)

def test_PLF_insert_node_after_right(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes(4, 1)

    assert p(3.5) == approx(0.0)

def test_PLF_insert_node(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes(0.5, 1)

    assert p(0.25) == approx(1.0)
    assert p(0.5) == approx(1.0)
    assert p(1.0) == approx(2.0)

def test_PLF_insert_node_noy(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes(7 / 3)

    assert p(-1) == approx(0.0)
    assert p(0) == approx(1.0)
    assert p(0.5) == approx(1.5)
    assert p(1) == approx(2)
    assert p(2) == approx(0.5)
    assert p(7 / 3) == approx(0.0)
    assert p(3) == approx(-1.0)
    assert p(4) == approx(0.0)

def test_PLF_insert_nodes(small):
    x, y, *r = small

    p = PLF(x, y)
    q = PLF(x, y)

    newx = [(p.x[i] + p.x[i + 1]) / 2 for i in range(2)]
    newy = p(newx)

    q.insert_nodes(newx, newy)

    evalx = np.linspace(x[0], x[-1], 11)

    for xx in evalx:
        assert p(xx) == approx(q(xx))

def test_PLF_insert_nodes_mixed(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes([0.5, 1], [0, 0])

    assert p(-1) == approx(0.0)
    assert p(0) == approx(1.0)
    assert p(0.5) == approx(0.0)
    assert p(1) == approx(0)
    assert p(2) == approx(-0.5)
    assert p(3) == approx(-1.0)
    assert p(4) == approx(0.0)

def test_PLF_insert_nodes_mixed2(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes([0.5, 1], [1.5, 2])

    assert p(-1) == approx(0.0)
    assert p(0) == approx(1.0)
    assert p(0.5) == approx(1.5)
    assert p(1) == approx(2)
    assert p(2) == approx(0.5)
    assert p(7 / 3) == approx(0.0)
    assert p(3) == approx(-1.0)
    assert p(4) == approx(0.0)

def test_PLF_insert_nodes_mixed_noy(small):
    x, y, *r = small

    p = PLF(x, y)

    p.insert_nodes([0.5, 1])

    assert p(-1) == approx(0.0)
    assert p(0) == approx(1.0)
    assert p(0.5) == approx(1.5)
    assert p(1) == approx(2)
    assert p(2) == approx(0.5)
    assert p(7 / 3) == approx(0.0)
    assert p(3) == approx(-1.0)
    assert p(4) == approx(0.0)

def test_PCF_insert_nodes_sim(small):
    x, y, *r = small

    p = PCF(x, y)

    x = p.x
    y = p.y

    p.insert_nodes(x)

    assert p.x == approx(x)
    assert p.y == approx(y)

def test_PLF_refine_lin():
    x = np.array([-2, 0, 2])
    y = np.array([1, 2, 1])

    p = PLF(x, y)

    p.refine_lin()

    assert (p.x) == approx(np.array([-2, -1, 0, 1, 2]))

def test_PLF_refine_log():
    x = np.power(10.0, [-2, 0, 2])
    y = np.array([1, 2, 1])

    p = PLF(x, y)

    p.refine_log()

    assert (np.log10(p.x)) == approx(np.array([-2, -1, 0, 1, 2]))

def test_PLF_copy(small):
    x, y, *r = small

    a = PLF(x, y)

    b = a.copy()

    assert np.any(a.x == b.x) and not a.x is b.x
    assert np.any(a.y == b.y) and not a.y is b.y

def test_PLF_flat_noy():
    a = PLF.flat(-1, 1)

    assert a.x[0] == approx(-1.0)
    assert a.x[1] == approx(+1.0)
    assert a.y[0] == approx(0.5)
    assert a.y[1] == approx(0.5)

def test_PLF_flat_y():
    a = PLF.flat(-1, 1, 10)

    assert a.x[0] == approx(-1.0)
    assert a.x[1] == approx(+1.0)
    assert a.y[0] == approx(10.0)
    assert a.y[1] == approx(10.0)

def test_PLF_flat_swapx():
    a = PLF.flat(+1, -1)

    assert a.x[0] == approx(-1.0)
    assert a.x[1] == approx(+1.0)
    assert a.y[0] == approx(0.5)
    assert a.y[1] == approx(0.5)

def test_PCF_eval(small):
    x, y, *r = small

    p = PCF(x, y)

    assert p.y[-1] == p.y[-2]

def test_PCF_eval(small):
    x, y, *r = small

    p = PCF(x, y)

    xeval = [-1.0, +0.0, +0.5, +1.0, +2.0, +3.0, +4.0]
    yeval = [0.0, +1.0, +1.0, +2.0, +2.0, +0.0, 0.0]

    for xe, ye in zip(xeval, yeval):
        assert p(xe) == approx(ye)

def test_PCF_slopes(small):
    x, y, *r = small

    p = PCF(x, y)

    assert p.slopes == approx(np.ones(2))

def test_PCF_norm(small):
    x, y, *r = small

    p = PCF(x, y)

    assert p.norm() == approx(5.0)

def test_PCF_copy(small):
    x, y, *r = small

    a = PCF(x, y)

    b = a.copy()

    assert np.any(a.x == b.x) and not a.x is b.x
    assert np.any(a.y == b.y) and not a.y is b.y

def test_plf_partial_integrals(small):
    x, y, *r = small

    a = PLF(x, y)

    b = PCF([0, 1, 3], [1, 1, 1])

    cnodes = np.array(
        [[0, 1, 3], [-1, 1, 3], [0.5, 1, 3], [-1, 1, 4], [-1, 1, 2], [0.5, 1, 2]]
    )

    cvals = np.array([0.5, 1, np.nan])

    convs = np.array(
        [[1.5, 1.0], [1.5, 1.0], [0.875, 1.0], [1.5, 1.0], [1.5, 1.25], [0.875, 1.25]]
    )

    for c, cint in zip(cnodes, convs):
        b = PCF(c, cvals)
        res = a.partial_integrals(b)
        assert res == approx(cint)

def test_convolute(small):
    x, y, *r = small

    a = PLF(x, y)

    cnodes = np.array(
        [[0, 1, 3], [-1, 1, 3], [0.5, 1, 3], [-1, 1, 4], [-1, 1, 2], [0.5, 1, 2]]
    )
    cvals = np.array([0.5, 1, np.nan])

    convs = np.array([1.75, 1.75, 1.4375, 1.75, 2, 1.6875])

    for c, cint in zip(cnodes, convs):
        b = PCF(c, cvals)
        res = a.convolute(b)
        assert res == approx(cint)


# CNN_1.py
# editor: tongpl
# data: 2020.3.9

import torch

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import imageio

# 学习率
LR = 0.0005

class MyRelu:
    """
    http://gmei test
    """

    def __init__(self):
        self.x = None

    def __call__(self,x):
        self.x = x
        #must use copy in nump to avaoid pass by reference
        out = slef.x.copy()
        out[out<0] = 0
        return out

    def backward(self, d_loss):
        relu_mask = (self.x >= 0)
        dx = d_loss * relu_mask
        return dx

##class Net(torch.nn.Module):
##    """Some Information about Net"""
##
##    def __init__(self):
##        super(Net, self).__init__()
##        # sequential: 时序容器，以他们传入的顺序被添加到容器中
##        m=torch.nn.ReLU();
##        self.conv1 = torch.nn.Sequential(
##            torch.nn.Conv2d(3, 16, 3, padding=1),  # 3*32*32 -> 16*32*32
##            MyRelu(),
##            #torch.nn.ReLU(),
##            torch.nn.MaxPool2d(2, 2)  # 16*32*32 -> 16*16*16
##        )
##        self.conv2 = torch.nn.Sequential(
##            torch.nn.Conv2d(16, 32, 3, padding=1),  # 16*16*16 -> 32*16*16
##            MyRelu(),
##            #torch.nn.ReLU(),
##            torch.nn.MaxPool2d(2, 2)  # 32*16*16 -> 32*8*8
##        )
##        self.conv3 = torch.nn.Sequential(
##            torch.nn.Conv2d(32, 64, 3, padding=1),  # 32*8*8 -> 64*8*8
##            MyRelu(),
##            #torch.nn.ReLU(),
##            torch.nn.MaxPool2d(2, 2)  # 64*8*8 -> 64*4*4
##        )
##        self.fc1 = torch.nn.Sequential(
##            torch.nn.Linear(64*4*4, 32),
##            MyRelu(),
##            #torch.nn.ReLU(),
##            # torch.nn.Dropout()
##        )
##        self.fc2 = torch.nn.Linear(32, 10)
##
##    def forward(self, x):
##        x = self.conv1(x)
##        x = self.conv2(x)
##        x = self.conv3(x)
##        x = x.view(-1, 64*4*4)
##        x = self.fc1(x)
##        x = self.fc2(x)
##        return x
##

class Net1(torch.nn.Module):
    """Some Information about CNN"""

    def __init__(self):
        super(Net1, self).__init__()
        self.conv1 = torch.nn.Sequential(
            torch.nn.Conv2d(3, 16, 3, padding=1),  # 3*32*32 -> 16*32*32
            torch.nn.ReLU(),
        )
        self.conv2 = torch.nn.Sequential(
            torch.nn.Conv2d(16, 32, 3, padding=1),  # 16*32*32 -> 32*32*32
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2, 2)  # 32*32*32-> 32*16*16
        )
        self.conv3 = torch.nn.Sequential(
            torch.nn.Conv2d(32, 64, 3, padding=1),  #  32*16*16 -> 64*16*16
            torch.nn.ReLU(),
        )

        self.conv4 = torch.nn.Sequential(
            torch.nn.Conv2d(64, 128, 3, padding=1),  #  64*16*16 -> 128*16*16
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2, 2)  # 128*16*16 -> 128*8*8
        )

        self.conv5 = torch.nn.Sequential(
            torch.nn.Conv2d(128, 256, 3, padding=1),  #  128*8*8 -> 256*8*8
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2, 2)  # 256*8*8 -> 256*4*4
        )

        ##self.gap = torch.nn.AvgPool2d(4,4)
        self.gap = torch.nn.MaxPool2d(4,4)
        self.fc = torch.nn.Linear(256, 10)

    def forward(self, x):
        x = self.conv1(x)
        ##print('cov1 out shape',x.shape);
        ##plt.figure(1)
        ##for i in range(1, 16 + 1):
        ##    plt.subplot(4, 4, i)
        ##    plt.imshow(x[0,i-1])
        x = self.conv2(x)
        ##print('cov2 out shape',x.shape);
        ##plt.figure(2)
        ##for i in range(1, 32 + 1):
        ##    plt.subplot(4, 8, i)
        ##    plt.imshow(x[0, i - 1])
        x = self.conv3(x)
        ##print('cov3 out shape',x.shape);
        ##plt.figure(3)
        ##for i in range(1, 64 + 1):
        ##    plt.subplot(4, 16, i)
        ##    plt.imshow(x[0, i - 1])
        x = self.conv4(x)
        ##print('cov4 out shape',x.shape);
        ##plt.figure(4)
        ##for i in range(1, 128 + 1):
        ##    plt.subplot(8, 16, i)
        ##    plt.imshow(x[0, i - 1])
        x = self.conv5(x)
        ##print('cov5 out shape',x.shape);
        x = self.gap(x)
        ##print('gap out shape',x.shape);
        ##plt.show()
        x = x.view(-1, 256)
        x = self.fc(x)
        return x

##class Net2(torch.nn.Module):
##    """Some Information about Net"""
##
##    def __init__(self):
##        super(Net2, self).__init__()
##        # sequential: 时序容器，以他们传入的顺序被添加到容器中
##        self.conv1 = torch.nn.Sequential(
##            torch.nn.Conv2d(3, 31, 3, padding=1),  # 3*32*32 -> 31*32*32
##            torch.nn.ReLU()
##        )
##        self.conv2 = torch.nn.Sequential(
##            torch.nn.Conv2d(31, 32, 3, padding=1),  # 31*32*32 -> 32*32*32
##            torch.nn.ReLU()
##        )
##        self.conv3 = torch.nn.Sequential(
##            torch.nn.Conv2d(32, 33, 3, padding=1),  # 32*32*32 -> 33*32*32
##            torch.nn.ReLU(),
##            torch.nn.MaxPool2d(2, 2)  # 64*32*32 -> 64*16*16
##        )
##        self.conv4 = torch.nn.Sequential(
##            torch.nn.Conv2d(33, 64, 3, padding=1),  # 33*16*16 -> 64*16*16
##            torch.nn.ReLU()
##        )
##        self.conv5 = torch.nn.Sequential(
##            torch.nn.Conv2d(64, 65, 3, padding=1),  # 64*16*16 -> 65*16*16
##            torch.nn.ReLU(),
##            torch.nn.MaxPool2d(2, 2)  # 65*16*16 -> 65*8*8
##        )
##
##        self.fc1 = torch.nn.Sequential(
##            torch.nn.Linear(65*8*8, 10),
##            torch.nn.Softmax(dim=1),
##            # torch.nn.Dropout()
##        )
##
##    def forward(self, x):
##        x = self.conv1(x)
##        x = self.conv2(x)
##        x = self.conv3(x)
##        x = self.conv4(x)
##        x = self.conv5(x)
##        x = x.view(-1, 65*8*8)
##        x = self.fc1(x)
##        return x
##

net = Net1()
#net.cuda()
net.cpu()

criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=LR)


# VECTOR AUTOREGRESSION
# https://www.machinelearningplus.com/time-series/vector-autoregression-examples-python/

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import grangercausalitytests

from portfolio_optimization.modeling.arima import find_d_arima
from statsmodels.tsa.api import VAR

from portfolio_optimization.helper import convert_to_non_stationary, forecast_accuracy
from portfolio_optimization.modeling.modeling import get_readytomodel_df
from statsmodels.tsa.vector_ar.vecm import coint_johansen

def grangers_causation_matrix(data, variables, test='ssr_chi2test', verbose=False):
    """Check Granger Causality of all possible combinations of the Time series.
    The rows are the response variable, columns are predictors. The values in the table
    are the P-Values. P-Values lesser than the significance level (0.05), implies
    the Null Hypothesis that the coefficients of the corresponding past values is
    zero, that is, the X does not cause Y can be rejected.

    data      : pandas dataframe containing the time series variables
    variables : list containing names of the time series variables.
    """

    maxlag = 12

    df = pd.DataFrame(np.zeros((len(variables), len(variables))), columns=variables, index=variables)
    for c in df.columns:
        for r in df.index:
            test_result = grangercausalitytests(data[[r, c]], maxlag=maxlag, verbose=False)
            p_values = [round(test_result[i + 1][0][test][1], 4) for i in range(maxlag)]
            if verbose: print(f'Y = {r}, X = {c}, P Values = {p_values}')
            min_p_value = np.min(p_values)
            df.loc[r, c] = min_p_value
    df.columns = [var + '_x' for var in variables]
    df.index = [var + '_y' for var in variables]
    return df

def cointegration_test(df, alpha=0.05, summary=True):
    """Perform Johanson's Cointegration Test and Report Summary"""
    out = coint_johansen(df,-1,5)
    d = {'0.90':0, '0.95':1, '0.99':2}
    traces = out.lr1
    cvts = out.cvt[:, d[str(1-alpha)]]
    def adjust(val, length= 6): return str(val).ljust(length)

    # Summary
    if summary:
        print('Name   ::  Test Stat > C(95%)    =>   Signif  \n', '--'*20)
    not_sign_cols = []
    for col, trace, cvt in zip(df.columns, traces, cvts):
        if summary:
            print(adjust(col), ':: ', adjust(round(trace,2), 9), ">", adjust(cvt, 8), ' =>  ' , trace > cvt)
        if not trace > cvt:
            not_sign_cols.append(col)

    return not_sign_cols

def train_var():

    feature_columns, target_columns, df = get_readytomodel_df([0], stationary=False)
    df = df.asfreq("MS")
    df = df.dropna()

    print(df.shape)  # (123, 8)
    print(df.tail())

    # # Plot
    # fig, axes = plt.subplots(nrows=7, ncols=3, dpi=120, figsize=(10, 6))
    # for i, ax in enumerate(axes.flatten()):
    #     data = df[df.columns[i]]
    #     ax.plot(data, color='red', linewidth=1)
    #     # Decorations
    #     ax.set_title(df.columns[i])
    #     ax.xaxis.set_ticks_position('none')
    #     ax.yaxis.set_ticks_position('none')
    #     ax.spines["top"].set_alpha(0)
    #     ax.tick_params(labelsize=6)
    #
    # plt.tight_layout()
    # plt.show()

    # caus_matrix = grangers_causation_matrix(df, variables=df.columns)
    # print(caus_matrix)

    not_sign_cols = cointegration_test(df, summary=False)

    df = df.drop(not_sign_cols, axis=1)
    print(df.tail())

    test_size = 36
    df_train, df_test = df[0:-test_size], df[-test_size:]

    # Check size
    print(df_train.shape)  # (119, 8)
    print(df_test.shape)  # (4, 8)

    for name, column in df.iteritems():
        d = find_d_arima(column)
        print(name, d)

    df_diff = df_train.copy()
    for i in range(1):
        df_diff = df_diff.diff().dropna()

    model = VAR(df_diff)
    # for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    #     result = model.fit(i)
    #     print('Lag Order =', i)
    #     print('AIC : ', result.aic)
    #     print('BIC : ', result.bic)
    #     print('FPE : ', result.fpe)
    #     print('HQIC: ', result.hqic, '\n')
    #
    # x = model.select_order(maxlags=12)
    # print(x.summary())

    p = 3 # selected order with min AIC
    model_fitted = model.fit(p)
    print(model_fitted.summary())

    # Get the lag order
    lag_order = model_fitted.k_ar
    print(lag_order)  # > 4

    # Input data for forecasting
    forecast_input = df_diff.values[-lag_order:]
    print(forecast_input)

    # Forecast
    fc = model_fitted.forecast(y=forecast_input, steps=test_size)
    df_forecast = pd.DataFrame(fc, index=df.index[-test_size:], columns=df.columns)
    print(df_forecast)

    df_results = convert_to_non_stationary(df_train, df_forecast, order=1)
    print(df_results)

    fig, axes = plt.subplots(nrows=int(len(df.columns) / 2), ncols=2, dpi=150, figsize=(10, 10))
    for i, (col, ax) in enumerate(zip(df.columns, axes.flatten())):
        df_train[col].plot(legend=False, ax=ax, color="blue") # train
        df_results[col].plot(legend=False, ax=ax, color="red").autoscale(axis='x', tight=True) # forecast
        df_test[col][-test_size:].plot(legend=False, ax=ax, color="green") # actual
        ax.set_title(col + ": Forecast vs Actuals")
        ax.xaxis.set_ticks_position('none')
        ax.yaxis.set_ticks_position('none')
        ax.spines["top"].set_alpha(0)
        ax.tick_params(labelsize=6)

    plt.tight_layout()
    plt.show()

    for c in df_results.columns:
        print(c)
        accuracy_prod = forecast_accuracy(df_results[c].values, df_test[c.split("_")[0]])
        for k, v in accuracy_prod.items():
            print(k, ': ', round(v, 4))
        print()


import numpy as np

from .activations import *
from ._grads import *
from .Initializers import initialization_mapping
from .Layers import LearnableLayer

class SimpleRNN(LearnableLayer):

    def __init__(self, units, weight_init="std",
                 activation=None, recurrent_activation='tanh', bptt_trunc = 5,
                 use_bias=True, return_sequences=False, return_state=False):
        """
        The Recurrent Neural Network.
        Parameters
        ----------
        units: (integer) number of neurons in the units.
        weight_init: (string) either `he_normal`, `xavier_normal`, `he_uniform`, `glorot_uniform` or standard normal distribution.
        """
        assert weight_init in initialization_mapping.keys(),\
                "Unknow weight initialization type."
        assert activation in ["swish", "sigmoid", "tanh", "relu", swish, sigmoid, tanh, relu, None],\
                "Unknown activation function: " + str(activation)
        assert recurrent_activation in ["swish", "sigmoid", "tanh", "relu", swish, sigmoid, tanh, relu],\
                "Unknown activation function: " + str(activation)

        self.n_units = units
        self.weight_init = weight_init
        self.output = None
        self.paramater = { 'W_xh' : None, 'W_hh' : None, 'W_ho' : None}

        if isinstance(activation, str):
            self.activation = activation_mapping[activation]
        else:
            self.activation = activation

        if isinstance(recurrent_activation, str):
            self.recurrent_activation = activation_mapping[recurrent_activation]
        else:
            self.recurrent_activation = recurrent_activation

        self.use_bias = use_bias
        self.return_sequences = return_sequences
        self.return_state = return_state
        self.bptt_trunc = bptt_trunc

    def forward(self, X):
        """
        """
        self.layer_input = X
        self.batch_size, self.timesteps, self.input_dim = X.shape

        if self.paramater['W_xh'] is None:
            self.paramater['W_xh'] = initialization_mapping[self.weight_init](weight_shape=(self.n_units, self.input_dim))
        if self.paramater['W_hh'] is None:
            self.paramater['W_hh'] = initialization_mapping[self.weight_init](weight_shape=(self.n_units, self.n_units))
        if self.paramater['W_ho'] is None:
            self.paramater['W_ho'] = initialization_mapping[self.weight_init](weight_shape=((self.input_dim, self.n_units)))

        # Save these values for use in backprop.
        self.state_input = np.zeros((self.batch_size, self.timesteps, self.n_units))
        self.states = np.zeros((self.batch_size, self.timesteps+1, self.n_units))
        self.outputs = np.zeros((self.batch_size, self.timesteps, self.input_dim))

        # Set last time step to zero for calculation of the state_input at time step zero
        self.states[:, -1] = np.zeros((self.batch_size, self.n_units))
        for t in range(self.timesteps):
            # Input to state_t is the current input and output of previous states
            self.state_input[:, t]= X[:, t].dot(self.paramater['W_xh'].T) + self.states[:, t-1].dot(self.paramater['W_hh'].T)
            self.states[:, t] = self.recurrent_activation(self.state_input[:, t])
            self.outputs[:, t] = self.states[:, t].dot(self.paramater['W_ho'].T)
            if self.activation is not None:
                self.outputs[:, t] = self.activation(self.states[:, t].dot(self.paramater['W_ho'].T))

        if(self.return_sequences):
            self.output = self.outputs
            return self.outputs
        else:
            self.output = self.outputs[:,-1,:]
            return self.outputs[:,-1,:]

        # if(self.return_state):
        #     return_outs.append(self.states[:,-1,:])

    def backward(self, d_prev, prev_layer):
        """
        """
        if len(d_prev.shape) == 2:
            _,h_dim = d_prev.shape
            zeros = np.zeros((self.batch_size,self.timesteps,h_dim))
            zeros[:,-1,:] = d_prev
            d_prev = zeros

        # _, timesteps, _ = d_prev.shape
        #U = Wxh, W = Whh, V = Who

        # Variables where we save the accumulated gradient w.r.t each parameter
        grad_Wxh = np.zeros_like(self.paramater['W_xh'])
        grad_Whh = np.zeros_like(self.paramater['W_hh'])
        grad_Who = np.zeros_like(self.paramater['W_ho'])
        # The gradient w.r.t the layer input.
        # Will be passed on to the previous layer in the network
        d_prev_next = np.zeros_like(d_prev)

        # Back Propagation Through Time
        for t in reversed(range(self.timesteps)):
            # Update gradient w.r.t V at time step t
            grad_Who += d_prev[:, t].T.dot(self.states[:, t])
            # Calculate the gradient w.r.t the state input
            grad_wrt_state = d_prev[:, t].dot(self.paramater['W_ho'])
            if self.activation is not None:
                grad_wrt_state *= eval(self.activation.__name__+'_grad')(self.state_input[:, t])
            # Gradient w.r.t the layer input
            d_prev_next[:, t] = grad_wrt_state.dot(self.paramater['W_xh'])
            # Update gradient w.r.t W and U by backprop. from time step t for at most
            # self.bptt_trunc number of time steps
            for t_ in reversed(np.arange(max(0, t - self.bptt_trunc), t+1)):
                grad_Wxh += grad_wrt_state.T.dot(self.layer_input[:, t_])
                grad_Whh += grad_wrt_state.T.dot(self.states[:, t_-1])
                # Calculate gradient w.r.t previous state
                grad_wrt_state = grad_wrt_state.dot(self.paramater['W_hh'])
                if self.recurrent_activation is not None:
                    grad_wrt_state *= eval(self.recurrent_activation.__name__+'_grad')(self.state_input[:, t_-1])

        dWs = {'W_xh': grad_Wxh, 'W_hh': grad_Whh, 'W_ho': grad_Who}
        # Update weights
        return d_prev_next, dWs, None


import os
import re
import shutil
import unittest
import utils
import django_chuck.template.base
from django_chuck.exceptions import TemplateError

class UtilsTest(unittest.TestCase):
    def setUp(self):
        self.test_file = os.path.join("test", "project_dir", "test_file")

        if os.path.isfile(self.test_file):
            os.unlink(self.test_file)

    def test_get_files(self):
        files = utils.get_files(os.curdir)
        self.assertTrue(len(files) > 0)

        for file in files:
            self.assertTrue(os.path.isfile(file))

    def test_write_to_file(self):
        test_data = "some wicked cool stuff"
        utils.write_to_file(self.test_file, test_data)

        with open(self.test_file, "r") as f:
            self.assertEqual(f.read(), test_data)

    def test_append_to_file(self):
        test_data = "some wicked cool stuff"
        test_data2 = "even more test data"

        utils.write_to_file(self.test_file, test_data)
        utils.append_to_file(self.test_file, test_data2)

        with open(self.test_file, "r") as f:
            self.assertEqual(f.read(), test_data + test_data2)

    def test_find_chuck_module_path(self):
        self.assertTrue(os.path.exists(utils.find_chuck_module_path()))

    def test_find_chuck_command_path(self):
        self.assertTrue(os.path.exists(utils.find_chuck_command_path()))

    def test_find_commands(self):
        self.assertTrue(len(utils.find_commands()) > 0)

    def test_autoload_commands(self):
        import argparse
        parser = argparse.ArgumentParser()
        subparsers = parser.add_subparsers()
        cfg = {}
        cmds = utils.find_commands()

        self.assertTrue(utils.autoload_commands(subparsers, cfg, cmds))

    def test_get_template_engine(self):
        obj = utils.get_template_engine("test", "test/project_dir")
        self.assertTrue(issubclass(obj.__class__, django_chuck.template.base.BaseEngine))

    def test_compile_template_with_extension(self):
        shutil.copy("test/templates/base.html", "test/project_dir/base.html")
        placeholder = {"WHO": "Balle"}

        result = utils.compile_template("test/templates/site.html", "test/project_dir/site.html", placeholder, "test", "test/project_dir")
        self.assertTrue(result)
        self.assertFalse(os.path.isfile("test/project_dir/site.html"))

        with open("test/project_dir/base.html", "r") as f:
            content = f.read()
            self.assertIn("<html>", content)
            self.assertIn("Hello Balle", content)

    def test_compile_template_without_extension(self):
        placeholder = {"WHO": "Balle"}

        result = utils.compile_template("test/templates/placeholder.html", "test/project_dir/placeholder.html", placeholder, "test", "test/project_dir")
        self.assertTrue(os.path.isfile("test/project_dir/placeholder.html"))

        with open("test/project_dir/placeholder.html", "r") as f:
            content = f.read()
            self.assertIn("Hello Balle", content)

if __name__ == '__main__':
    unittest.main()


import aiohttp
import pytest

from aio_osservaprezzi import OsservaPrezzi, Fuel

from . import load_fixture

SAMPLE = {"town": "Lanciano", "region": "Abruzzo", "province": "CH"}

@pytest.mark.asyncio
async def test_station(aresponses):
    """Test getting station information."""
    aresponses.add(
        "carburanti.mise.gov.it",
        "/OssPrezziSearch/ricerca/localita",
        "POST",
        aresponses.Response(
            status=200,
            text=load_fixture("response.json"),
            headers={"Content-Type": "application/json"},
        ),
    )

    async with aiohttp.ClientSession() as session:
        osservaprezzi = OsservaPrezzi(SAMPLE, session)
        fuel: Fuel = (await osservaprezzi.get_station_by_id(12345)).fuels[0]
        assert fuel
        assert fuel.name == "Carb1"
        assert fuel.id == 1
        assert not fuel.is_self
        assert fuel.price == "1,234"


from omni.isaac.kit import SimulationApp

simulation_app = SimulationApp({"headless": False})

from omni.isaac.core import World
from omni.isaac.franka.franka import Franka
from omni.isaac.core.utils.stage import add_reference_to_stage
from omni.isaac.core.utils.types import ArticulationAction
import numpy as np
import math
import math_type_define.dyros_math as DyrosMath
import lula
import keyboard
import pandas as pd
from omni.isaac.core.articulations.articulation_view import ArticulationView

np.set_printoptions(precision=3, suppress=True)

# ---------------------------------------- Setting of Simulation -----------------------------------------------------------
hz = 100
model_path = "/home/dyros/Isaac_Sim/panda_controller/model"
world = World(stage_units_in_meters=1.0)
world.set_simulation_dt(1/hz, 1/hz)
asset_path = model_path + "/panda_arm_hand.usd"
add_reference_to_stage(usd_path=asset_path, prim_path="/World/panda")

# define the manipulator
franka = world.scene.add(Franka(prim_path="/World/panda", name="franka_robot",
                                end_effector_prim_name="panda_hand",
                                gripper_dof_names = ["panda_finger_joint1", "panda_finger_joint2"],
                                gripper_open_position = np.array([0.04, 0.04]),
                                gripper_closed_position = np.array([0, 0]),))
kinematic = lula.RobotDescription.kinematics(lula.load_robot(robot_description_file=model_path + "/panda_robot_description.yaml",
                                                             robot_urdf=model_path + "/panda_arm_hand.urdf"))

# define force sensor of each finger
# right_finger_ft = ArticulationView(prim_paths_expr="/World/panda/panda_rightfinger", enable_dof_force_sensors=True)
ft = ArticulationView(prim_paths_expr="/World/panda", name="ft_viewer")
world.scene.add(ft)
# ft.initialize()
# if ft.initialized:
#     print("true")

# set the default positions of the other gripper joints to be opened so
# that its out of the way of the joints we want to control when gripping an object for instance.
joints_default_positions = np.zeros(9)
joints_default_positions[3] = -math.pi/2
joints_default_positions[5] = math.pi/2
joints_default_positions[6] = math.pi/4
franka.set_joints_default_state(positions=joints_default_positions)
world.scene.add_default_ground_plane()
world.reset()
# ----------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------ Initializing of Data -----------------------------------------------------------
q_init = joints_default_positions[:7]
q = q_init
q_desired = q_init
j = np.zeros((6,7))
j_qd = np.zeros((6,7))
pose_init = np.zeros((4,4))
pose = np.zeros((4,4))
pose_qd = np.zeros((4,4))
play_time = 0.0
control_start_time = 0.0
tick_init = 0
tick = 0
is_mode_changed = False
control_mode = "default"
# ----------------------------------------------------------------------------------------------------------------------------

# -------------------------------------------- Getting Trajecroty -----------------------------------------------------------
file_path = "/home/dyros/Isaac_Sim/panda_controller/trajectory"
traj_data = pd.read_table(file_path+"/circle.txt", sep=" ", header=None)
traj_data = traj_data.to_numpy()

x_traj = np.zeros(traj_data.shape[0])
y_traj = traj_data[:, 1]
z_traj = traj_data[:, 2]

vx_traj = np.zeros(traj_data.shape[0])
vy_traj = traj_data[:, 3]
vz_traj = traj_data[:, 4]
# ---------------------------------------------------------------------------------------------------------------------------

# -------------------------------------------------- Simulation -----------------------------------------------------------
while simulation_app.is_running():
    world.step(render=True)
    if world.is_playing():
        if world.current_time_step_index == 0:
            world.reset()

    if keyboard.is_pressed("i"):
        is_mode_changed = True
        control_mode = "joint_ctrl_home"
    elif keyboard.is_pressed("1"):
        is_mode_changed = True
        control_mode = "CLIK"

    if is_mode_changed:
        print("Change the Mode : " + control_mode)
        is_mode_changed = False
        q_init = franka.get_joint_positions()[:7].T
        q = q_init
        q_desired = q_init
        j = kinematic.jacobian(cspace_position=q.astype(np.float64), frame="panda_hand")
        j_qd = kinematic.jacobian(cspace_position=q_desired.astype(np.float64), frame="panda_hand")
        pose_init = kinematic.pose(cspace_position=q.astype(np.float64), frame="panda_hand").matrix()
        pose = pose_init
        pose_qd = pose_init
        control_start_time = world.current_time
        play_time = control_start_time
        tick_init = world.current_time_step_index
        tick = tick_init

        # x_traj += np.ones(x_traj.shape[0]) * pose_init[0, 3]
        # y_traj += np.ones(y_traj.shape[0]) * pose_init[1, 3]
        # z_traj += np.ones(z_traj.shape[0]) * pose_init[2, 3]

    # --------------------------------- Update States -----------------------------------------------
    q = franka.get_joint_positions()[:7].T
    j = kinematic.jacobian(cspace_position=q.astype(np.float64), frame="panda_hand")
    j_qd = kinematic.jacobian(cspace_position=q_desired.astype(np.float64), frame="panda_hand")
    pose = kinematic.pose(cspace_position=q.astype(np.float64), frame="panda_hand").matrix()
    pose_qd = kinematic.pose(cspace_position=q_desired.astype(np.float64), frame="panda_hand").matrix()
    # ------------------------------------------------------------------------------------------------

    #---------------------------------- CLIK -----------------------------------------
    if control_mode=="joint_ctrl_home":
        q_desired = DyrosMath.cubicVector(play_time, control_start_time, control_start_time+1,
                                          q_init, joints_default_positions[:7], np.zeros(7), np.zeros(7))
    elif control_mode=="CLIK":
        x_desired = np.zeros(3)  # position
        rotation_desired = np.zeros((3,3)) # rotation
        xd_desired = np.zeros(6) # v, w

        # ---------------------------------- Cubic Spline -------------------------------------------
        # duration = 2.0
        # target_pose = np.array([[1,  0,  0, 0.25],
        #                         [0, -1,  0, 0.28],
        #                         [0,  0, -1, 0.65],
        #                         [0,  0,  0, 1]])

        # for i in range(0,3):
        #     x_desired[i] = DyrosMath.cubic(play_time, control_start_time, control_start_time+duration,
        #                                    pose_init[i,3], target_pose[i,3], 0, 0)
        #     xd_desired[i] = DyrosMath.cubicDot(play_time, control_start_time, control_start_time+duration,
        #                                        pose_init[i,3], target_pose[i,3], 0, 0)
        # rotation_desired = DyrosMath.rotationCubic(play_time, control_start_time, control_start_time+duration,
        #                                            pose_init[:3,:3], target_pose[:3,:3])
        # xd_desired[3:] = DyrosMath.rotationCubicDot(play_time, control_start_time, control_start_time+duration,
        #                                             np.zeros(3), np.zeros(3), pose_init[:3,:3], target_pose[:3,:3])
        # -----------------------------------------------------------------------------------------------

        # --------------------------------- Traj from TXT -----------------------------------------------
        index = tick - tick_init

        if(index < 0):
            x_desired = pose_init[:3,3]
            rotation_desired = pose_init[:3,:3]
            xd_desired = np.zeros(6)
        elif(index >= traj_data.shape[0]):
            x_desired = np.array([x_traj[-1]+pose_init[0, 3], y_traj[-1]+pose_init[1, 3], z_traj[-1]+pose_init[2, 3]])
            rotation_desired = pose_init[:3,:3]
            xd_desired = np.zeros(6)
        else:
            x_desired = np.array([x_traj[index]+pose_init[0, 3], y_traj[index]+pose_init[1, 3], z_traj[index]+pose_init[2, 3]])
            rotation_desired = pose_init[:3,:3]
            xd_desired = np.array([vx_traj[index], vy_traj[index], vz_traj[index],
                                0, 0, 0])
        # -----------------------------------------------------------------------------------------------

        x_error = np.zeros(6)
        x_error[:3] = x_desired - pose_qd[:3,3]
        x_error[3:] = DyrosMath.getPhi(pose_qd[:3,:3], rotation_desired)

        kp = np.zeros((6,6))
        np.fill_diagonal(kp, val=[50, 50, 50, 10, 10, 10])

        pseudo_j_inv = np.dot( j_qd.T, np.linalg.inv( np.dot(j_qd, j_qd.T) ) )
        qd_desired = np.dot(pseudo_j_inv, xd_desired + np.dot(kp, x_error) )
        q_desired = q_desired + qd_desired/hz
    #-------------------------------------------------------------------------------------

    play_time = world.current_time
    tick = world.current_time_step_index
    franka.set_joint_positions(positions=np.concatenate((q_desired, np.zeros(2)), axis=0))

    #------------------------------- Print States ----------------------------------
    if( world.current_time_step_index % 50 == 0 ):
        print("position : ")
        print(pose[:3, 3].T)
        print("orientation : ")
        print(pose[:3,:3])
        print("\n\n")

        # print("right_ft")
        # print(right_finger_ft._physics_view.get_force_sensor_forces())
        print("left_ft")
        print(ft._physics_view.get_force_sensor_forces())
        # print("ft_sensor : ")
        # print(ft_sensor._physics_view)

    #-------------------------------------------------------------------------------

simulation_app.close()
# ----------------------------------------------------------------------------------------------------------------------------


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

# In[1]:

import os
os.getcwd()

# In[2]:

fptr = open('file1.txt', 'r')

# In[3]:

type(fptr)

# In[4]:

stra = fptr.read(4)

# In[5]:

print(stra)

# In[6]:

strb = fptr.readline()

# In[7]:

print(strb)

# In[8]:

fptr.close()

# In[9]:

fptr = open('file1.txt', 'r')
stra = fptr.read()

# In[10]:

stra

# In[11]:

fptr = open('file2.txt', 'w')

# In[12]:

fptr = open('file2.txt', 'w')

# In[13]:

#import os
#os.chown()

# In[14]:

fptr.write("dinesh wants a holiday tomm")

# In[15]:

fptr.close()

# In[16]:

import os

# In[17]:

with open('file1.txt', 'r') as fptr:
    data = fptr.read()
    print(data)

# In[18]:

with open('file3.txt', 'w') as fptr:
    fptr.write('some hands, all hands')

# In[23]:

fptr = open('file2.txt', 'r')

# In[25]:

fptr.read(4)

# In[26]:

fptr.tell()

# In[22]:

fptr.close()

# In[27]:

fptr.seek(10)

# In[28]:

fptr.tell()

# In[29]:

fptr.read()

# In[ ]:


class Solution:
    def longestCommonPrefix(self, strs) -> str:
        substring = []
        for counter, simb in enumerate(sorted(strs, key=len)[0]):
            if all(map(lambda x: simb if x[counter] == simb else False, strs)):
                substring.append(simb)
            else:
                break
        return ''.join(substring)

t1 = ["flower","flow","flight"], 'fl'
t2 = ["dog","racecar","car"], ''
t3 = ['getrs', 'getter', 'gertrude'], 'ge'
t4 = ['subject', 'subjective', 'substruct'], 'sub'
t5 = ['boston', 'buy', 'bruclin'], 'b'
t6 = ['', '', ''], ''
t7 = ['a'], 'a'
t8 = ['ab', 'a'], 'a'

def test_func(*args):
    obj = Solution()
    for set in args:
        array = set[0]
        answer = set[1]
        res = obj.longestCommonPrefix(array)
        assert res == answer, f'test faild, expected value: "{answer}", the resulting value: "{res}"'
        print(f'the test case: "{set}" is passed')
    print('all tests are completed')

test_func(t1, t2, t3, t4, t5, t6, t7, t8)


__author__ = "JJ.sven"

'''这里加入core_path, 是为了，core下其它模块相互间导入能直接import， 不用导入根路径方式,
   参见main内my的导入方式，但这种没提示'''
import os, sys
core_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(core_path)

# 注意package 每次被from 或者 improt 都会被执行一次，某些情况下需要注意
print('core __init__')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Imports
from sklearn.model_selection import KFold
from DNNModel import *
from numpy import *
from external_functions import *
import matplotlib
matplotlib.use('Agg')
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD, RMSprop, Adadelta, Adam

class DNNMTModel(DNNModel):

    def create_DNN_model(self, print_model=True):
        print("Creating DNN model")
        fundamental_parameters = ['dropout', 'output_activation', 'optimization', 'learning_rate',
                                  'units_in_input_layer',
                                  'units_in_hidden_layers', 'nb_epoch', 'batch_size']
        for param in fundamental_parameters:
            if self.parameters[param] == None:
                print("Parameter not set: " + param)
                return
        self.print_parameter_values()
        model = Sequential()
        # Input layer
        model.add(Dense(self.parameters['units_in_input_layer'], input_dim=self.feature_number, activation='relu'))
        model.add(Dropout(self.parameters['dropout']))
        # constructing all hidden layers
        for layer in self.parameters['units_in_hidden_layers']:
            model.add(Dense(layer, activation='relu'))
            model.add(Dropout(self.parameters['dropout']))
        # constructing the final layer - Multi task DNN - 6 endpoints predicts the outcome given an input X.
        model.add(Dense(6, activation=self.parameters['output_activation']))
        if self.parameters['optimization'] == 'SGD':
            optim = SGD()
            optim.lr.set_value(self.parameters['learning_rate'])
        elif self.parameters['optimization'] == 'RMSprop':
            optim = RMSprop()
            optim.lr.set_value(self.parameters['learning_rate'])
        elif self.parameters['optimization'] == 'Adam':
            optim = Adam()
        elif self.parameters['optimization'] == 'Adadelta':
            optim = Adadelta()
        model.compile(loss='binary_crossentropy', optimizer=optim, metrics=[matthews_correlation])
        if self.verbose == 1: str(model.summary())
        self.model = model
        print("DNN model sucessfully created")

    def model_selection(self, X, y, n_iter_search=2, n_folds=2):
        print("Selecting best DNN model")
        seed = 7
        old_parameters = self.parameters.copy()
        self.model_selection_history = []
        for iteration in range(n_iter_search):
            mean_train_matthews_correlation = []
            mean_train_loss = []
            mean_val_matthews_correlation = []
            mean_val_loss = []
            mean_time_fit = []
            print("Iteration no. " + str(iteration + 1))
            new_parameters = self.batch_parameter_shufller()
            temp_values = new_parameters.copy()
            for key in new_parameters:
                self.parameters[key] = new_parameters[key]
            i = 0
            kf = KFold(n_splits=n_folds, shuffle=False, random_state=seed)
            for train, valid in kf.split(X, y):
                print("Running Fold " + str(i + 1) + str("/") + str(n_folds))
                X_train, X_valid = X[train], X[valid]
                y_train, y_valid = y[train], y[valid]
                self.create_DNN_model()
                self.create_DNN_model()
                time_fit = self.fit_model(X_train, X_valid, y_train, y_valid)
                train_scores = self.model.evaluate(X_train, y_train)
                train_matthews_correlation = train_scores[1]
                train_loss = train_scores[0]
                val_scores = self.model.evaluate(X_valid, y_valid)
                val_matthews_correlation = val_scores[1]
                val_loss = val_scores[0]
                self.print_fit_results(train_scores, val_scores)
                mean_train_matthews_correlation.append(train_matthews_correlation)
                mean_train_loss.append(train_loss)
                mean_val_matthews_correlation.append(val_matthews_correlation)
                mean_val_loss.append(val_loss)
                mean_time_fit.append(time_fit)
                temp_values['train_matthews_correlation_' + str(i + 1)] = train_matthews_correlation
                temp_values['train_loss_' + str(i + 1)] = train_loss
                temp_values['val_matthews_correlation_' + str(i + 1)] = val_matthews_correlation
                temp_values['val_loss_' + str(i + 1)] = val_loss
                temp_values['time_fit_' + str(i + 1)] = time_fit
                i += 1
            temp_values['mean_train_matthews_correlation'] = np.mean(mean_train_matthews_correlation)
            temp_values['mean_train_loss'] = np.mean(mean_train_loss)
            temp_values['mean_val_matthews_correlation'] = np.mean(mean_val_matthews_correlation)
            temp_values['mean_val_loss'] = np.mean(mean_val_loss)
            temp_values['mean_time_fit'] = np.mean(mean_time_fit)
            self.model_selection_history.append(temp_values)
        self.parameters = old_parameters.copy()
        print("Best DNN model successfully selected")

    def cv_fit(self, X, y, cv=5):
        if self.parameters['nb_epoch'] and self.parameters['batch_size']:
            cvscores = []
            cvhistory = []
            i = 0
            kf = KFold(n_splits=cv, shuffle=False, random_state=1)
            for train, valid in kf.split(X, y):
                print("Running Fold " + str(i + 1) + str("/") + str(cv))
                X_train, X_valid = X[train], X[valid]
                y_train, y_valid = y[train], y[valid]
                self.create_DNN_model()
                time_fit = self.fit_model(X_train, X_valid, y_train, y_valid)
                cvscores.append(self.evaluate_model(X_valid, y_valid))
                cvhistory.append(self.history)
                i += 1
        return cvscores, cvhistory

    def evaluate_model(self, X_test, y_test):
        print("Evaluating model with hold out test set.")
        scores = dict()
        endpoints = ['endpoint_Sex', 'endpoint_EFSAll', 'endpoint_ClassLabel', 'endpoint_OSAll', 'endpoint_OSHR',
                     'endpoint_EFSHR']
        predictions = self.model.predict(X_test).round().astype(int)
        print(predictions)
        print(y_test)
        scores['roc_auc'] = []
        scores['accuracy'] = []
        scores['f1_score'] = []
        scores['mcc'] = []
        scores['precision'] = []
        scores['recall'] = []
        scores['log_loss'] = []
        for column in range(predictions.shape[1]):
            y_true = y_test[:, column]
            y_pred = predictions[:, column]
            scores['roc_auc'].append(roc_auc_score(y_true, y_pred))
            scores['accuracy'].append(accuracy_score(y_true, y_pred))
            scores['f1_score'].append(f1_score(y_true, y_pred))
            scores['mcc'].append(matthews_corrcoef(y_true, y_pred))
            scores['precision'].append(precision_score(y_true, y_pred))
            scores['recall'].append(recall_score(y_true, y_pred))
            scores['log_loss'].append(log_loss(y_true, y_pred))
        for i in range(len(endpoints)):
            print(endpoints[i])
            for metric, score in scores.items():
                print(metric + ': ' + str(score[i]))
        return scores

    def format_scores_cv(self, scores_cv_list):
        form_scores = dict()
        for key in scores_cv_list[0].keys():
            form_scores[key] = []
        for score in scores_cv_list:
            form_scores['roc_auc'].append(score['roc_auc'])
            form_scores['accuracy'].append(score['accuracy'])
            form_scores['f1_score'].append(score['f1_score'])
            form_scores['mcc'].append(score['mcc'])
            form_scores['precision'].append(score['precision'])
            form_scores['recall'].append(score['recall'])
            form_scores['log_loss'].append(score['log_loss'])
        mean_scores = dict()
        sd_scores = dict()
        for key in form_scores.keys():
            mean_scores[key] = []
            sd_scores[key] = []
        for key, value in mean_scores.items():
            mean_scores[key] = [[] for i in range(6)]
            sd_scores[key] = [[] for i in range(6)]
        for endpoint in range(6):

            for metric in form_scores.keys():
                temp_cv_array = []
                for fold in range(self.cv):
                    temp_cv_array.append(form_scores[metric][fold][endpoint])
                mean_scores[metric][endpoint] = np.mean(temp_cv_array)
                sd_scores[metric][endpoint] = np.std(temp_cv_array)
            for metric in mean_scores.keys():
                print(metric, ': ', str(mean_scores[metric][endpoint]), ' +/- ', sd_scores[metric][endpoint])
        return mean_scores, sd_scores

    def write_report(self, mean_scores, sd_scores, root_dir, file_name):
        if not os.path.exists(root_dir):
            os.makedirs(root_dir)
        i = 0
        while os.path.exists(root_dir + '/' + file_name + '_report_' + str(i) + '.txt'):
            i += 1
        final_path = os.path.join(root_dir, file_name + '_report_' + str(i) + '.txt')
        print("Writing report file with path: " + final_path)
        endpoints = ['endpoint_Sex', 'endpoint_EFSAll', 'endpoint_ClassLabel', 'endpoint_OSAll', 'endpoint_OSHR',
                     'endpoint_EFSHR']
        out = open(final_path, 'w')
        out.write('Hyperparameters')
        out.write('\n')
        out.write('=' * 25)
        out.write('\n')
        for key in sorted(self.parameters):
            out.write(key + ": " + str(self.parameters[key]))
            out.write('\n')
        out.write('\n')
        out.write('Scores')
        out.write('\n')
        out.write("=" * 25)
        out.write('\n')
        for i in range(len(endpoints)):
            out.write('\n')
            out.write(endpoints[i])
            out.write('\n')
            for metric in mean_scores.keys():
                out.write(str(metric) + ': ' + str(mean_scores[metric][i]) + ' +/- ' + str(sd_scores[metric][i]))
                out.write('\n')
        out.close()
        print("Report file successfully written.")

def dnn_mt_model_fit(dropout, output_activation, optimization, learning_rate, units_in_input_layer,
                  units_in_hidden_layers, nb_epoch, batch_size, early_stopping, patience, root_dir, file_name, cv=5,
                  **kwargs):
    dnn = DNNMTModel(kwargs)
    dnn.parameters = {

        'dropout': dropout,
        'output_activation': output_activation,
        'optimization': optimization,
        'learning_rate': learning_rate,
        'units_in_input_layer': units_in_input_layer,
        'units_in_hidden_layers': units_in_hidden_layers,
        'nb_epoch': nb_epoch,
        'batch_size': batch_size,
        'early_stopping': early_stopping,
        'patience': patience

    }
    dnn.create_DNN_model()
    if not dnn.splitted:
        cv_scores, cv_history = dnn.cv_fit(dnn.X, dnn.y, cv)
        dnn.print_fit_results()
        dnn.plot_model_performance(cv_history, root_dir, file_name)
        mean_scores, sd_scores = dnn.format_scores_cv(cv_scores)
        dnn.write_report(mean_scores, sd_scores, root_dir, file_name)
    else:
        cv_scores, cv_history = dnn.cv_fit(dnn.X_test, dnn.y_test, cv)
        dnn.print_fit_results()
        dnn.plot_model_performance(cv_history, root_dir, file_name)
        mean_scores, sd_scores = dnn.format_scores_cv(cv_scores)
        dnn.write_report(mean_scores, sd_scores, root_dir, file_name)

def dnn_mt_model_selection_split(X_train, X_test, y_train, y_test, root_dir, experiment_designation, n_iter=10, cv=5):
    dnn = DNNMTModel(X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test, cv=cv)
    file_name = experiment_designation
    dnn.model_selection(dnn.X_train, dnn.y_train, n_iter, cv)
    dnn.write_model_selection_results(root_dir, file_name)
    dnn.select_best_model()
    dnn.create_DNN_model()
    cv_scores, cv_history = dnn.cv_fit(dnn.X_test, dnn.y_test, cv)
    dnn.plot_model_performance(cv_history, root_dir, file_name)
    mean_scores, sd_scores = dnn.format_scores_cv(cv_scores)
    dnn.write_report(mean_scores, sd_scores, root_dir, file_name)

def dnn_mt_model_selection_cv(X, y, root_dir, experiment_designation, n_iter=10, cv=5):
    dnn = DNNMTModel(X=X, y=y, cv=cv)
    file_name = experiment_designation
    dnn.model_selection(dnn.X, dnn.y, n_iter, cv)
    dnn.write_model_selection_results(root_dir, file_name)
    dnn.select_best_model()
    dnn.create_DNN_model()
    cv_scores, cv_history = dnn.cv_fit(dnn.X, dnn.y, cv)
    dnn.plot_model_performance(cv_history, root_dir, file_name)
    mean_scores, sd_scores = dnn.format_scores_cv(cv_scores)
    dnn.write_report(mean_scores, sd_scores, root_dir, file_name)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 17:21:30 2019

@author: jlee
"""

import time
start_time = time.time()

import numpy as np
import glob, os

# ----- File name & directory ----- #
cpath = os.path.abspath(".")
dir_iraf = "/".join(cpath.split("/")[:-1])+"/"
rawdir = dir_iraf+"raw/"
caldir = dir_iraf+"calibrations/"
if (glob.glob(caldir) == []):
	os.mkdir(caldir)
lst_bias = 'bias.lis'
procbias = 'Mbias.fits'

# ----- Importing IRAF from the root directory ----- #
current_dir = os.getcwd()
os.chdir(dir_iraf)

from pyraf import iraf
from pyraf.iraf import gemini, gmos

iraf.unlearn()
os.chdir(current_dir)
iraf.chdir(current_dir)

# ----- Making a processed bias ----- #
iraf.imdelete(procbias)
iraf.imdelete('g@'+lst_bias)
iraf.gbias('@'+lst_bias, procbias, rawpath=rawdir, fl_vardq='yes')
iraf.copy(procbias, caldir)

# ----- Inspecting the processed bias ----- #
os.system('ds9 &')
iraf.sleep(5.0)
iraf.gdisplay(procbias, 1, fl_paste='no')

# Printing the running time
print('--- %s seconds ---' %(time.time()-start_time))


# 11437(74), 11438(75)

# 최소 공통 조상(LCA)

import sys
input = sys.stdin.readline
sys.setrecursionlimit(int(1e5))
LOG = 21

n = int(input())
parent = [[0] * LOG for _ in range(n + 1)]    # 부모 노드 정보
d = [0] * (n + 1)   # 각 노드까지의 깊이
c = [False] * (n + 1)   # 각 노드의 깊이가 계산 되었는지 여부
graph = [[] for _ in range(n + 1)]

for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

# 루트 노드부터 시작하여 깊이(depth)를 구하는 함수
def dfs(x, depth):
    c[x] = True
    d[x] = depth    # 매번 노드에 대해 깊이 기록
    for y in graph[x]:  # 인접노드 확인
        if c[y]:    # 이미 깊이를 구했으면
            continue
        parent[y][0] = x    # 한 칸위에 있는 부모에 대한 정보만 먼저 구함
        dfs(y, depth + 1)

# 전체 부모관계를 설정하는 함수
def set_parent():
    dfs(1, 0)   # 루트노드는 1번 노드
    for i in range(1, LOG):
        for j in range(1, n +1):
            parent[j][i] = parent[parent[j][i - 1]][i - 1]

# A와 B의 최소 공통 조상 구하는 함수
def lca(a, b):
    # b가 더 작도록 설정
    if d[a] > d[b]:
        a, b = b, a
    # 먼저 깊이가(depth) 동일하므로
    # 큰 크기부터 작은 크기까지 차례대로 확인하면서 값 줄이기 (ex) 15: 8 -> 4 -> 2 -> 1
    for i in range(LOG - 1, -1, -1):
        if d[b] - d[a] >= (1 << i):
            b = parent[b][i]    # 더 깊은 쪽의 길이가 줄어들도록
    # 부모가 같아지도록
    if a == b:
        return a
    for i in range(LOG - 1, -1, -1):
        # 조상을 향해 거슬러 올라가기
        if parent[a][i] != parent[b][i]:
            a = parent[a][i]
            b = parent[b][i]
    # 이후에 부모가 찾고자 하는 조상
    return parent[a][0]

set_parent()

m = int(input())

for i in range(m):
    a, b = map(int, input().split())
    print(lca(a, b))


# vim: ts=4:sw=4:expandtabs

__author__ = 'zach.mott@gmail.com'

from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

class AssignmentStartForm(forms.Form):
    name = forms.CharField(max_length=256)
    confirm_name = forms.CharField(
        max_length=256,
        help_text=_('Please confirm your name.')
    )
    email = forms.EmailField(
        required=False,
        help_text=_('Optional: Enter your email address to '
                    'receive a copy of your assignment results.')
    )

    def clean(self):
        cleaned_data = super(AssignmentStartForm, self).clean()

        if cleaned_data['name'] != cleaned_data['confirm_name']:
            raise ValidationError(_("'Name' and 'Confirm name' fields "
                                    "must contain the same value."))

        return cleaned_data


class Utils:
    @staticmethod
    def reversed(number):
        if not isinstance(number, int):
            raise ValueError("Input must be an integer")
        reversed_num = int(str(number)[::-1])
        return reversed_num

    @staticmethod
    def formatter(number):
        if not isinstance(number, int):
            raise ValueError("Input must be an integer")
        binary_format = bin(number)
        octal_format = oct(number)
        return binary_format, octal_format


#253. Meeting Rooms II
class Solution:
	def minMeetingRooms(self, intervals):
		start=sorted([i[0] for i in intervals])
		end=sorted([i[1] for i in intervals])

		result = 0 # in the whole, what is max room
		count = 0 # at a time, how many rooms are booked.

		s=0 # start index
		e=0 # end index
		while s<len(intervals):
			if start[s] < end[e]:
				s+=1
				count+=1
			else:
				e+=1
				count-=1
			result = max(result, count)

		return result

"""
[[9,10],[4,9],[4,17]]
[[4,9],[4,17],[9,10]]

[[4,9],[4,17],[9,10]]

"""


class Solution(object):
    def stoneGame(self, piles):
        """
        :type piles: List[int]
        :rtype: bool
        """
        alex = 0
        lee = 0
        while(len(piles)>0):
            if(piles[0] > piles[-1]):
                alex += piles.pop(0)
                lee += piles.pop()
            else:
                alex += piles.pop()
                lee += piles.pop(0)
        if(alex > lee):
            return True
        else:
            return False


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

import config

def sql_shell(coon):
    while True:
        xuanze = input("[+]请输入要执行SQL语句：")
        if xuanze.upper() != "Q" :
            payloads = xuanze + ";"
            print(payloads)
            result = config.mssql_exec(coon=coon, payload=payloads).sql_shell()
            print(result)
        else:
            quit()


"""Part of Speech

This module provides parser of BIO tagged text.

    Typical usage example:

        my_list = BioParser.parse("input.txt")
"""
from pos import PoS

class BioParser(object):
    """BIO tagged text parser
    """
    @staticmethod
    def parse(ipath: str) -> list[tuple[str, PoS]]:
        """parse the BIO tagged text

        Memory overflow ignored.
        "O" tokens are not compressed.

        Args:
            ipath: path of BIO tagged text

        Returns:
            A list of tuples. Each contain a token string and its PoS.
            example:

            [("token0", PoS.ad),
             ("token1", PoS.UKN)]
        """
        token_list: list = []
        token: list[str]
        index = 0
        tag: list[str]
        bio: str
        prev_token: str

        with open(ipath, mode="r", encoding="utf-8") as ifile:
            token_list = ifile.read().strip().split(' ')
            while index < len(token_list):
                token = token_list[index].split('_')
                tag = token[1].split('-')
                bio = token[1][0]
                if bio == 'O':
                    token_list[index] = (token[0],
                                         PoS.UKN)
                    index += 1
                elif bio == 'I':
                    prev_token = token_list[index - 1]
                    token_list[index - 1] = (str().join([prev_token[0],
                                                         token[0]]),
                                             prev_token[1])
                    del token_list[index]
                elif bio == 'B':
                    token_list[index] = (token[0],
                                         PoS[tag[1]])
                    index += 1
        return token_list


#functional programming
#Tuple 只能新增，不能更改
#
#
t1 = ('A','B','C','D','E','F')
#t1[1]='b'
for t in t1:
    print(t)
l1 = ['A','B','C','D','E','F']
for l in l1:
    print(l)
sales = {'iphone11':500,'ipad':300}
for (k,v) in sales.items():
    print(f'key={k},value={v}')


from typing import Union

from pytorch_lightning.loggers.base import DummyLogger
from pytorch_lightning.utilities import rank_zero_info, rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException

class DebuggingConnector:

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

    def on_init_start(
            self,
            limit_train_batches,
            limit_val_batches,
            limit_test_batches,
            val_check_interval,
            overfit_batches,
            fast_dev_run
    ):
        if not isinstance(fast_dev_run, (bool, int)):
            raise MisconfigurationException(
                f'fast_dev_run={fast_dev_run} is not a valid configuration.'
                ' It should be either a bool or an int >= 0'
            )

        if isinstance(fast_dev_run, int) and (fast_dev_run < 0):
            raise MisconfigurationException(
                f'fast_dev_run={fast_dev_run} is not a'
                ' valid configuration. It should be >= 0.'
            )

        self.trainer.fast_dev_run = fast_dev_run
        fast_dev_run = int(fast_dev_run)

        # set fast_dev_run=True when it is 1, used while logging
        if fast_dev_run == 1:
            self.trainer.fast_dev_run = True

        if fast_dev_run:
            limit_train_batches = fast_dev_run
            limit_val_batches = fast_dev_run
            limit_test_batches = fast_dev_run
            self.trainer.max_steps = fast_dev_run
            self.trainer.num_sanity_val_steps = 0
            self.trainer.max_epochs = 1
            self.trainer.val_check_interval = 1.0
            self.trainer.check_val_every_n_epoch = 1
            self.trainer.logger = DummyLogger()

            rank_zero_info(
                'Running in fast_dev_run mode: will run a full train,'
                f' val and test loop using {fast_dev_run} batch(es).'
            )

        self.trainer.limit_train_batches = _determine_batch_limits(limit_train_batches, 'limit_train_batches')
        self.trainer.limit_val_batches = _determine_batch_limits(limit_val_batches, 'limit_val_batches')
        self.trainer.limit_test_batches = _determine_batch_limits(limit_test_batches, 'limit_test_batches')
        self.trainer.val_check_interval = _determine_batch_limits(val_check_interval, 'val_check_interval')
        self.trainer.overfit_batches = _determine_batch_limits(overfit_batches, 'overfit_batches')
        self.determine_data_use_amount(self.trainer.overfit_batches)

    def determine_data_use_amount(self, overfit_batches: float) -> None:
        """Use less data for debugging purposes"""
        if overfit_batches > 0:
            self.trainer.limit_train_batches = overfit_batches
            self.trainer.limit_val_batches = overfit_batches
            self.trainer.limit_test_batches = overfit_batches

def _determine_batch_limits(batches: Union[int, float], name: str) -> Union[int, float]:
    if 0 <= batches <= 1:
        return batches
    elif batches > 1 and batches % 1.0 == 0:
        return int(batches)
    else:
        raise MisconfigurationException(
            f'You have passed invalid value {batches} for {name}, it has to be in [0.0, 1.0] or an int.'
        )


#!/usr/bin/python3
# encoding: utf-8

import threading
from tile.sqllitedb import TileSqlLiteDB
from random import *
from tile.Info import TileInfo
import hashlib
import shutil
import os
from PIL import Image

Image.MAX_IMAGE_PIXELS = None

OpenSeaMapMerged = 'OpenSeaMapMerged'

def _MergePictures(background_file, overlay_file, result_file):

    background = Image.open(background_file)
    overlay = Image.open(overlay_file)

    background = background.convert("RGBA")
    overlay = overlay.convert("RGBA")

    new_img = Image.alpha_composite(background, overlay)
    new_img.save(result_file, "PNG")

class MergeThread(threading.Thread):

    def __init__(self, tileman, lock, DBDIR):
        threading.Thread.__init__(self)
        self.tileman = tileman
        self.DBDIR = DBDIR
        self.stop = False
        self.lock = lock
        self._WorkingDirMerge = self.tileman._WorkingDirectory + "{}".format(randint(1, 0xffffffff))

    def SetTileSrv(self, TsOpenSeaMap, TSOpenStreetMap):
        self.TSOpenStreetMap = TSOpenStreetMap
        self.TsOpenSeaMap = TsOpenSeaMap

    def CleanupWorkingDir(self):
        if os.path.exists(self._WorkingDirMerge):
            shutil.rmtree(self._WorkingDirMerge)

    def run(self):
        # log = getlog()
        self.db = TileSqlLiteDB(self.DBDIR)
        cnt = 0

        while(self.stop is False):
            if len(self.tileman.joblist) is 0:
                self.stop = True
                break
            job = self.tileman.joblist[0]
            self.tileman.joblist.pop(0)
            # print("{} is working on job {} {} {} {}".format(self.name, job[0], job[1], job[2], job[3]))

            x = job[1]
            y = job[2]
            z = job[3]

            self.lock.acquire()
            tile_osm_street = self.db.GetTile(self.TSOpenStreetMap.name, z, x, y)
            tile_osm_sea = self.db.GetTile(self.TsOpenSeaMap.name, z, x, y)
            tile_osm3 = self.db.GetTile(OpenSeaMapMerged, z, x, y)
            self.lock.release()

            # stop processing if tile is not available
            if tile_osm_street is None or tile_osm_sea is None:
                assert(False)
                break

            if (tile_osm_street.updated is not 0) or (tile_osm_sea.updated is not 0) or (tile_osm3 is None):
                tile_merged = self.MergeTile(tile_osm_street, tile_osm_sea)
                tile_osm_street.updated = False
                tile_osm_sea.updated = False

                self.lock.acquire()
                self.db.StoreTile(OpenSeaMapMerged, tile_merged, z, x, y)
                self.db.StoreTile(self.TSOpenStreetMap.name, tile_osm_street, z, x, y)
                self.db.StoreTile(self.TsOpenSeaMap.name, tile_osm_sea, z, x, y)
                self.lock.release()

                self.tileman.tilemerged += 1
            else:
                self.tileman.tilemergedskipped += 1
            cnt += 1

        self.db.CloseDB()

    def MergeTile(self, tile_osm_street, tile_osm_sea):
        # store tile  in file
        filename_in1 = self._WorkingDirMerge + "/" + 'file_openstreetmap.png'
        filename_in2 = self._WorkingDirMerge + "/" + 'file_openseamap.png'
        filename_result1 = self._WorkingDirMerge + "/" + 'file_merged.png'

        # print(hashlib.md5(tile_osm_sea.data).hexdigest())

        # check if tile is blank (or in other words empty)
        if "df9310416043ee37c3b3d4896549d823" == hashlib.md5(tile_osm_sea.data).hexdigest():
            filename_result1 = filename_in1
            tile_osm_street.StoreFile(filename_result1)
        else:
            tile_osm_street.StoreFile(filename_in1)
            tile_osm_sea.StoreFile(filename_in2)

            background = Image.open(filename_in1)
            overlay = Image.open(filename_in2)

            background = background.convert("RGBA")
            overlay = overlay.convert("RGBA")

            new_img = Image.alpha_composite(background, overlay)
            new_img.save(filename_result1, "PNG")

        ret = TileInfo()
        ret.SetData(filename_result1)
        return ret


from openpyxl import load_workbook
import requests
import json

wb = load_workbook(filename="saved.xlsx")
ws3 = wb["holders"]
qua = 0
ws3.cell(row=1, column=4).value = "USDC"
usd_user = requests.get(
    "https://proton.cryptolions.io/v2/state/get_tokens?limit=1000&account=pimlrp"
).text
usd_user = json.loads(usd_user)
for x in usd_user["tokens"]:
    if x["symbol"] == "XUSDC":
        USDC = x["amount"]
print(USDC)
upper5 = 0.035 * USDC
num6_20 = 0.025 * USDC
num21_40 = 0.0125 * USDC

for r in range(2, ws3.max_row):
    if r > 40 and int(ws3.cell(row=r, column=3).value) >= 100:
        qua += 1
others = USDC * (0.1 / qua)
print(qua)
for r in range(2, ws3.max_row):
    if r < 7:
        ws3.cell(row=r, column=4).value = upper5
    if r < 21 and r > 6:
        ws3.cell(row=r, column=4).value = num6_20
    if r > 20 and r < 42:
        ws3.cell(row=r, column=4).value = num21_40
    if r > 41 and r <= 40 + qua:
        ws3.cell(row=r, column=4).value = others
wb.save("temp.xlsx")


__author__ = 'Lesko'
# Documentation is like sex.
# When it's good, it's very good.
# When it's bad, it's better than nothing.
# When it lies to you, it may be a while before you realize something's wrong.
import os
BASEDIR = os.path.abspath(os.path.dirname(__file__))

# WTforms
WTF_CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'

# SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASEDIR, 'app.db')
SQLALCHEMY_DATABASE_URI = 'postgresql://lesko:*****@192.168.1.52/homeNET'

# BASIC_AUTH_FORCE = True
# BASIC_AUTH_USERNAME = "lesko"
# BASIC_AUTH_PASSWORD = "*****"


from collections.abc import Sequence
from typing import Union, cast

import awkward as ak
import numpy as np
from scanpy import logging

from scirpy import get
from scirpy.util import DataHandler, _is_na

@DataHandler.inject_param_docs()
def chain_qc(
    adata: DataHandler.TYPE,
    *,
    airr_mod="airr",
    airr_key="airr",
    chain_idx_key="chain_indices",
    inplace: bool = True,
    key_added: Sequence[str] = ("receptor_type", "receptor_subtype", "chain_pairing"),
) -> Union[None, tuple[np.ndarray, np.ndarray, np.ndarray]]:
    """\
    Perform quality control based on the receptor-chain pairing configuration.

    Categorizes cells into their receptor types and according to their chain pairing
    status. The function adds three columns to `adata.obs`, two containing a coarse
    and fine annotation of receptor types, a third classifying cells according
    to the number of matched receptor types.

    `receptor_type` can be one of the following
        * `TCR` (all cells that contain any combination of TRA/TRB/TRG/TRD chains,
          but no IGH/IGK/IGL chains)
        * `BCR` (all cells that contain any combination of IGH/IGK/IGL chains,
          but no TCR chains)
        * `ambiguous` (all cells that contain both BCR and TCR chains)
        * `multichain` (all cells with more than two VJ or more than two VDJ chains)
        * `no IR` (all cells without any detected immune receptor)

    `receptor_subtype` can be one of the following
        * `TRA+TRB` (all cells that have only TRA and/or TRB chains)
        * `TRG+TRD` (all cells that have only TRG and/or TRD chains)
        * `IGH` (all cells that have only IGH chains, but no IGL or IGK)
        * `IGH+IGL` (all cells that have only IGH and IGL chains)
        * `IGH+IGK` (all cells that have only IGH and IGK chains)
        * `multichain` (all cells with more than two VJ or more than two VDJ chains)
        * `ambiguous` (all cells that are none of the above, e.g. TRA+TRD, TRA+IGH or,
          IGH+IGK as the primary and IGH+IGL as the secondary receptor)
        * `no IR` (all cells without any detected immune receptor)

    `chain_pairing` can be one of the following
        * `single pair` (all cells that have exactely one matched VJ and VDJ chain)
        * `orphan VJ` (all cells that have only one VJ chain)
        * `orphan VDJ` (all cells that have only one VDJ chain)
        * `extra VJ` (all cells that have a matched pair of VJ and VDJ chains plus an
          additional VJ-chain)
        * `extra VDJ` (analogous)
        * `two full chains` (all cells that have two matched pairs of VJ and VDJ chains)
        * `ambiguous` (all cells that have unmatched chains, i.e. that have been
          classified as an `ambiguous` receptor_subtype)
        * `multichain` (all cells with more than two VJ or more than two VDJ chains)
        * `no IR` (all chains with not immune receptor chains)

    Parameters
    ----------
    {adata}
    {inplace}
    {key_added}
    {airr_mod}
    {airr_key}
    {chain_idx_key}

    Returns
    -------
    Depending on the value of `inplace` either adds three columns to
    `adata.obs` or returns a tuple with three numpy arrays containing
    the annotations.
    """
    params = DataHandler(adata, airr_mod, airr_key, chain_idx_key)

    # initalize result arrays
    string_length = len("multichain")
    res_receptor_type = np.empty(dtype=f"<U{string_length}", shape=(params.adata.shape[0],))
    res_receptor_subtype = np.empty(dtype=f"<U{string_length}", shape=(params.adata.shape[0],))

    mask_has_ir = get._has_ir(params)
    mask_multichain = mask_has_ir & ak.to_numpy(params.chain_indices["multichain"])

    vj_loci = cast(np.ndarray, get.airr(params, "locus", ["VJ_1", "VJ_2"]).values)
    vdj_loci = cast(np.ndarray, get.airr(params, "locus", ["VDJ_1", "VDJ_2"]).values)

    # Build masks for receptor chains
    has_tra = (vj_loci == "TRA").any(axis=1)
    has_trg = (vj_loci == "TRG").any(axis=1)
    has_igk = (vj_loci == "IGK").any(axis=1)
    has_igl = (vj_loci == "IGL").any(axis=1)

    has_trb = (vdj_loci == "TRB").any(axis=1)
    has_trd = (vdj_loci == "TRD").any(axis=1)
    has_igh = (vdj_loci == "IGH").any(axis=1)

    has_tr = has_tra | has_trg | has_trb | has_trd
    has_ig = has_igk | has_igl | has_igh

    # Combine masks into receptor types and subtypes
    type_is_t = has_tr & ~has_ig
    type_is_b = ~has_tr & has_ig

    subtype_is_tab = (has_tra | has_trb) & ~(has_trg | has_trd | has_ig)
    subtype_is_tgd = (has_trg | has_trd) & ~(has_tra | has_trb | has_ig)
    subtype_is_ighk = (has_igk) & ~(has_tr | has_igl)
    subtype_is_ighl = (has_igl) & ~(has_tr | has_igk)
    # orphan IGH
    subtype_is_igh = (has_igh) & ~(has_igk | has_igl | has_tr)

    # Apply masks for receptor type
    res_receptor_type[:] = "ambiguous"
    res_receptor_type[~mask_has_ir] = "no IR"
    res_receptor_type[type_is_t] = "TCR"
    res_receptor_type[type_is_b] = "BCR"
    res_receptor_type[mask_multichain] = "multichain"

    # Apply masks for receptor subtypes
    res_receptor_subtype[:] = "ambiguous"
    res_receptor_subtype[~mask_has_ir] = "no IR"
    res_receptor_subtype[subtype_is_tab] = "TRA+TRB"
    res_receptor_subtype[subtype_is_tgd] = "TRG+TRD"
    res_receptor_subtype[subtype_is_igh] = "IGH"
    res_receptor_subtype[subtype_is_ighl] = "IGH+IGL"
    res_receptor_subtype[subtype_is_ighk] = "IGH+IGK"
    res_receptor_subtype[mask_multichain] = "multichain"

    res_chain_pairing = _chain_pairing(params, res_receptor_subtype == "ambiguous", mask_has_ir, mask_multichain)

    if inplace:
        col_receptor_type, col_receptor_subtype, col_chain_pairing = key_added
        params.set_obs(col_receptor_type, res_receptor_type)
        params.set_obs(col_receptor_subtype, res_receptor_subtype)
        params.set_obs(col_chain_pairing, res_chain_pairing)
    else:
        return (res_receptor_type, res_receptor_subtype, res_chain_pairing)

def _chain_pairing(
    params: DataHandler,
    mask_ambiguous: np.ndarray,
    mask_has_ir: np.ndarray,
    mask_multichain: np.ndarray,
) -> np.ndarray:
    """Annotate chain pairing categories.

    Parameters
    ----------
    mask_ambiguous
        boolean array of the same length as `adata.obs`, marking
        which cells have an ambiguous receptor configuration.
    """
    string_length = len("two full chains")
    results = np.empty(dtype=f"<U{string_length}", shape=(params.adata.shape[0],))

    logging.debug("Done initalizing")

    mask_has_vj1 = ~_is_na(get.airr(params, "junction_aa", "VJ_1").values)
    mask_has_vdj1 = ~_is_na(get.airr(params, "junction_aa", "VDJ_1").values)
    mask_has_vj2 = ~_is_na(get.airr(params, "junction_aa", "VJ_2").values)
    mask_has_vdj2 = ~_is_na(get.airr(params, "junction_aa", "VDJ_2").values)

    logging.debug("Done with masks")

    for m in [mask_has_vj1, mask_has_vdj1, mask_has_vj2, mask_has_vdj2]:
        # no cell can have a junction_aa sequence but no TCR
        assert np.setdiff1d(np.where(m)[0], np.where(mask_has_ir)[0]).size == 0

    results[~mask_has_ir] = "no IR"
    results[mask_has_vj1] = "orphan VJ"
    results[mask_has_vdj1] = "orphan VDJ"
    results[mask_has_vj1 & mask_has_vdj1] = "single pair"
    results[mask_has_vj1 & mask_has_vdj1 & mask_has_vj2] = "extra VJ"
    results[mask_has_vj1 & mask_has_vdj1 & mask_has_vdj2] = "extra VDJ"
    results[mask_has_vj1 & mask_has_vdj1 & mask_has_vj2 & mask_has_vdj2] = "two full chains"
    results[mask_ambiguous] = "ambiguous"
    results[mask_multichain] = "multichain"

    assert "" not in results, "One or more chains are not characterized"

    return results


from django.db.models import Q
from django.conf import settings
from django.http import JsonResponse
from django.contrib.postgres.aggregates import ArrayAgg
from django.views.generic.list import BaseListView
from django.views.generic.detail import BaseDetailView

from movies.models import FilmWork, PersonFilmWork

class MoviesApiMixin:
    model = FilmWork
    http_method_names = ["get"]
    movies_simple_fields_in_result = [
        "id",
        "title",
        "description",
        "creation_date",
        "rating",
        "type",
    ]
    movies_roles_in_result = {
        "actors": PersonFilmWork.Roles.ACTOR,
        "directors": PersonFilmWork.Roles.DIRECTOR,
        "writes": PersonFilmWork.Roles.WRITER,
    }

    def get_queryset(self):
        queryset = super().get_queryset().values(*self.movies_simple_fields_in_result)
        return queryset.annotate(genres=ArrayAgg("genres__name", distinct=True), **{
            field_name: ArrayAgg("persons__full_name", distinct=True, filter=Q(personfilmwork__role=role))
            for field_name, role in self.movies_roles_in_result.items()
        })

    def render_to_response(self, context, **response_kwargs):
        return JsonResponse(context)

class MoviesListApi(MoviesApiMixin, BaseListView):
    paginate_by = settings.MOVIES_PAGE_SIZE

    def get_context_data(self, *, object_list=None, **kwargs):
        paginator, page, queryset, is_paginated = self.paginate_queryset(self.get_queryset(), self.paginate_by)

        context = {
            "count": paginator.count,
            "total_pages": paginator.num_pages,
            "prev": page.previous_page_number() if page.has_previous() else None,
            "next": page.next_page_number() if page.has_next() else None,
            "results": list(queryset),
        }
        return context

class MoviesDetailApi(MoviesApiMixin, BaseDetailView):
    def get_context_data(self, **kwargs):
        return self.object


import socket
import sys

#GLOBAL redirect counter
redirect_num = 0

def http_client(url):
    #check for redirects
    global redirect_num
    redirect_num += 1
    if redirect_num == 10:
        print('Error: Too Many Redirects', file = sys.stderr)
        sys.exit(1)

    #get host and port
    #check if url given is valid
    if url[:5] == 'https':
        print('Error: HTTPS is not accepted in the url', file = sys.stderr)
        sys.exit(1)
    if url[0:7] != 'http://':
        print('Error: URL must start with http://', file = sys.stderr)
        sys.exit(1)

    #Get rid of http:// to get host
    port = 80
    host = url[7:]

    #if port is specified in url
    if host.find(":")!=-1:
        port_str = host[host.find(':')+1:]
        if port_str.find('/') != -1:
            port_str = port_str.replace('/', ' ')
        port = int(port_str)
        host = host[:host.find(":")]

    if host.find('/') != -1:
        index = host.find('/')
        if len(host[index:]) == 1:
            path = '//'
        else:
            path = "//" + host[index+1:]
        host = host[:index]
    else:
        path = '//'

    #code based on https://realpython.com/python-sockets/#application-client-and-server and https://www.geeks3d.com/hacklab/20190114/python-3-stoppable-tcp-server-and-tcp-client-with-geexlab/
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect( (host, port) )
    req_header = 'GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host)
    req_header = req_header.encode()
    client.sendall(req_header)

    #receiving data, code based on https://iximiuz.com/en/posts/writing-web-server-in-python-sockets/ and https://stackoverflow.com/questions/47658584/implementing-http-client-with-sockets-without-http-libraries-with-python
    data = []
    response = client.recv(4096)
    data.append(response)
    while True:
        data_part = client.recv(4096)
        if not data_part:
            break
        data.append(data_part)

    for i in range(len(data)):
        data[i] = data[i].decode('latin-1')
    response = ''.join(data)
    response = response[9:]
    response = response.split('\r\n')

    #get status from response -- alwasy the first 0-3 index
    status = response[0][:3]
    response_len = len(response)

    #check statuses and execute rules
    if status == "200":
        for i in range(response_len):
            if response[i].find('Content-Type: text/html') != -1:
                print(response[response_len - 1])
                sys.exit(0)
        sys.exit(1)

    elif status == "301" or status == "302":
        for i in range(response_len):
            if response[i].find('Location') != -1:
                redir_location = response[i][10:]
        print("Redirected to:  " + redir_location, file = sys.stderr)
        http_client(redir_location)

    elif status >= "400":
        for i in range(response_len):
            if response[i].find('Content-Type: text/html') != -1:
                print(response[response_len-1])
        sys.exit(1)

if __name__ == '__main__':
    http_client(sys.argv[1])


from django.urls import path, include
from .views import Home, CreatToDo, MyToDo, MyToDoDetail, MyToDoUpdate, MyToDoDelete

urlpatterns = [
    path("", Home.as_view(), name="home"),
    path("my-todos", MyToDo.as_view(), name="my_todos"),
    path("my-todos/details/<int:pk>", MyToDoDetail.as_view(), name="my_todo_details"),
    path("my-todos/update/<int:pk>", MyToDoUpdate.as_view(), name="my_todo_update"),
    path("my-todos/delete/<int:pk>", MyToDoDelete.as_view(), name="my_todo_delete"),
    path("create-todo", CreatToDo.as_view(), name="create_todo"),
]


from typing import Any

from bleak.backends.scanner import AdvertisementData

ADVERTISEMENT_DATA_DEFAULTS = {
    "manufacturer_data": {},
    "service_data": {},
    "service_uuids": [],
    "rssi": -127,
    "platform_data": ((),),
    "tx_power": -127,
}

def generate_advertisement_data(**kwargs: Any) -> AdvertisementData:
    """Generate advertisement data with defaults."""
    new = kwargs.copy()
    for key, value in ADVERTISEMENT_DATA_DEFAULTS.items():
        new.setdefault(key, value)
    return AdvertisementData(**new)