from utils.transformers_utils import SiameseRobertaModel,TrainerLogger,get_preds,compute_metrics,update_metrics
import numpy as np
import pandas as pd
from transformers import RobertaTokenizerFast,RobertaConfig,TrainingArguments
from datasets import Dataset,DatasetDict #!pip install datasets
import evaluate                          #!pip install evaluate
from sklearn.metrics import roc_auc_score,recall_score,f1_score,precision_score,accuracy_score

DATASETS_PATH = '../data/datasets'
TOKENIZER_PATH = f'../data/tokenizers/tokenizer_opcode_bpe'
MODEL_PATH = f'../data/models/roberta_lm'
RESULTADOS_PATH = f'../data/resultados'

def get_dataset(target,tokenizer=None,remove_columns=None):
    dataset = pd.read_csv(f'{DATASETS_PATH}/{target}_balanced.csv')[['opcode_nocds',target,'is_valid']]#.sample(300)
    #dataset['arithmetic'] = np.where(dataset['arithmetic']==1,'Danger','Safe')
    dataset[target] = np.where(dataset[target]==1,0,1)
    dataset.columns = ['text','labels','is_valid']
    ds = DatasetDict()
    ds['train'] = Dataset.from_pandas(dataset[~dataset.is_valid].drop('is_valid',axis=1), preserve_index=False)
    ds['valid'] = Dataset.from_pandas(dataset[dataset.is_valid].drop('is_valid',axis=1), preserve_index=False)
    if tokenizer!= None:
        ds = ds.map(lambda x:tokenizer(x["text"],truncation=True,padding='max_length'), batched=True,remove_columns=remove_columns)
    return ds

def get_trainer(ds,model):
    training_args = TrainingArguments("test_trainer",
                                      num_train_epochs=6,
                                      no_cuda=False,
                                      evaluation_strategy="epoch",#steps
                                      #logging_strategy
                                      learning_rate= 5e-05,
                                      lr_scheduler_type= 'linear',
                                      #'linear',#cosine_with_restarts
                                      fp16=True
                                     )

    train_dataset = ds['train']
    eval_dataset = ds['valid']

    return TrainerLogger(
        model=model, args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        compute_metrics=compute_metrics,
        )

max_size_sentence = 4048
metric = evaluate.load("accuracy")
tokenizer = RobertaTokenizerFast.from_pretrained(TOKENIZER_PATH, max_len=max_size_sentence)

config = RobertaConfig(
    vocab_size=1000,
    max_position_embeddings=512 + 2,  # 514
    hidden_size=216,
    num_attention_heads=6,
    num_hidden_layers=4,
    type_vocab_size=1
    #  id2label={0: 'LABEL_0', 1: 'LABEL_1', 2: 'LABEL_2'}
)

resultados = {
    'fold': ['f1', 'auc-roc', 'auc-roc-th', 'precision', 'recall', 'accuracy', 'size'],
}

vulnerabilities = ['access_control', 'arithmetic', 'denial_service',
                   'front_running', 'reentrancy', 'time_manipulation',
                   'unchecked_low_calls']

for target in vulnerabilities:
    print(target)
    ds = get_dataset(target, tokenizer, ['text'])
    model = SiameseRobertaModel(config, n_childs=8, pretrained='')
    trainer = get_trainer(ds, model)
    trainer.train()
    y_real_train, y_pred_train = get_preds(model, ds['train'])
    y_real, y_pred = get_preds(model, ds['valid'])
    update_metrics(target, resultados, (y_real_train, y_pred_train, y_real, y_pred))

pd.DataFrame(resultados).to_csv(f'{RESULTADOS_PATH}/transfomers_no_lm.csv', index=False)

max_size_sentence = 4048
metric = evaluate.load("accuracy")
tokenizer = RobertaTokenizerFast.from_pretrained(TOKENIZER_PATH, max_len=max_size_sentence)

config = RobertaConfig(
    vocab_size=1000,
    max_position_embeddings=512 + 2,  # 514
    hidden_size=216,
    num_attention_heads=6,
    num_hidden_layers=4,
    type_vocab_size=1
    #  id2label={0: 'LABEL_0', 1: 'LABEL_1', 2: 'LABEL_2'}
)

resultados = {
    'fold': ['f1', 'auc-roc', 'auc-roc-th', 'precision', 'recall', 'accuracy', 'size'],
}

vulnerabilities = ['access_control', 'arithmetic', 'denial_service',
                   'front_running', 'reentrancy', 'time_manipulation',
                   'unchecked_low_calls']

for target in vulnerabilities:
    print(target)
    ds = get_dataset(target, tokenizer, ['text'])
    model = SiameseRobertaModel(config, n_childs=8, pretrained=MODEL_PATH)
    trainer = get_trainer(ds, model)
    trainer.train()
    y_real_train, y_pred_train = get_preds(model, ds['train'])
    y_real, y_pred = get_preds(model, ds['valid'])
    update_metrics(target, resultados, (y_real_train, y_pred_train, y_real, y_pred))

pd.DataFrame(resultados).to_csv(f'{RESULTADOS_PATH}/transfomers_yes_lm.csv', index=False)


from fastapi import APIRouter, Depends
from fastapi_pagination import Page, Params
from src.admins.dependencies import get_groups_service, get_valid_group
from src.admins.models import Group
from src.admins.schemas import CreateGroupSchema, GroupOut

from src.admins.services import GroupService

router = APIRouter()

@router.get("/")
async def get_groups(
    params: Params = Depends(),
    groups_service: GroupService = Depends(get_groups_service)
) -> Page[GroupOut]:
    return await groups_service.get_all(params=params)

@router.post("/")
async def create_group(
    data: CreateGroupSchema,
    groups_service: GroupService = Depends(get_groups_service)
) -> GroupOut:
    return await groups_service.create(**data.dict())

@router.get("/{group_id}")
async def get_group(
    group: Group = Depends(get_valid_group)
) -> GroupOut:
    return group

@router.delete("/{group_id}")
async def delete_group(
    group: Group = Depends(get_valid_group)
) -> GroupOut:
    await group.delete()
    return group


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

from __future__ import print_function
from __future__ import division
import tensorflow as tf
import numpy as np
from tqdm import tqdm
import os
import sys
import shutil
import time
from utils import get_logger
import network

os.environ["CUDA_VISIBLE_DEVICES"] = "1"

sys.path.append('../..')
from data_helper import to_categorical
from evaluator import cail_evaluator

flags = tf.flags
flags.DEFINE_bool('is_retrain', False, 'if is_retrain is true, not rebuild the summary')
flags.DEFINE_integer('max_epoch', 1, 'update the embedding after max_epoch, default: 1')
flags.DEFINE_integer('max_max_epoch', 1000, 'all training epoches, default: 1000')
flags.DEFINE_float('lr', 1e-3, 'initial learning rate, default: 1e-3')
flags.DEFINE_float('decay_rate', 0.6, 'decay rate, default: 0.65')
flags.DEFINE_float('keep_prob', 0.5, 'keep_prob for training, default: 0.5')
flags.DEFINE_string("log_file_train",     "train.log",    "File for log")
flags.DEFINE_integer('decay_step', 5000, 'decay_step, default: 5000')
flags.DEFINE_integer('valid_step', 2500, 'valid_step, default: 2500')
flags.DEFINE_float('last_score12', 0.0, 'if valid_score12 > last_score12, save new model. default: 0.0')

FLAGS = flags.FLAGS

lr = FLAGS.lr
last_score12 = FLAGS.last_score12
settings = network.Settings()
summary_path = settings.summary_path
ckpt_path = settings.ckpt_path
model_path = ckpt_path + 'model.ckpt'
log_path = settings.log_path

embedding_path = '../../data/word_embedding_256.npy'

data_train_path = '../../data/wd_pdQS200/train/'
data_valid_path = '../../data/wd_pdQS200/valid/'

tr_batches = os.listdir(data_train_path)
va_batches = os.listdir(data_valid_path)

n_tr_batches = len(tr_batches)
n_va_batches = len(va_batches)

def get_batch(data_path, batch_id):
    new_batch = np.load(data_path + str(batch_id) + '.npz')
    X_batch = new_batch['X']
    y_batch = new_batch['y']
    return [X_batch, y_batch]

def valid_epoch(data_path, sess, model):
    va_batches = os.listdir(data_path)
    n_va_batches = len(va_batches)
    _costs = 0.0
    predict_labels_list = list()
    marked_labels_list = list()
    for i in range(n_va_batches):
        [X_batch, y_batch] = get_batch(data_path, i)
        marked_labels_list.extend(y_batch)
        y_batch = to_categorical(y_batch)
        _batch_size = len(y_batch)
        fetches = [model.loss, model.y_pred]
        feed_dict = {model.X_inputs: X_batch,
                     model.y_inputs: y_batch, model.batch_size: _batch_size,
                     model.tst: True, model.keep_prob: 1.0}
        _cost, predict_labels = sess.run(fetches, feed_dict)
        _costs += _cost
        predict_labels_list.extend(predict_labels)
    f1_micro, f1_macro, score12 = cail_evaluator(predict_labels_list, marked_labels_list)
    return f1_micro, f1_macro, score12

def train_epoch(data_path, sess, model, train_fetches,
                valid_fetches, train_writer, test_writer, logger):
    global last_score12
    global lr
    time0 = time.time()
    batch_indexs = np.random.permutation(n_tr_batches)
    for batch in tqdm(range(n_tr_batches)):
        global_step = sess.run(model.global_step)
        if 0 == (global_step + 1) % FLAGS.valid_step:
            f1_micro, f1_macro, score12 = valid_epoch(data_valid_path, sess, model)
            print('Global_step=%d: f1_micro=%g, f1_macro=%g, score12=%g, time=%g s' % (
                global_step, f1_micro, f1_macro, score12, time.time() - time0))
            logger.info('END:Global_step={}: f1_micro={}, f1_macro={}, score12={}'.
                        format(sess.run(model.global_step), f1_micro, f1_macro, score12))
            time0 = time.time()
            if score12 > last_score12:
                last_score12 = score12
                saving_path = model.saver.save(sess, model_path, global_step+1)
                print('saved new model to %s ' % saving_path)
        # training
        batch_id = batch_indexs[batch]
        [X_batch, y_batch] = get_batch(data_path, batch_id)
        y_batch = to_categorical(y_batch)
        _batch_size = len(y_batch)
        feed_dict = {model.X_inputs: X_batch,
                     model.y_inputs: y_batch, model.batch_size: _batch_size,
                     model.tst: False, model.keep_prob: FLAGS.keep_prob}
        summary, _cost, _, _ = sess.run(train_fetches, feed_dict)  # the cost is the mean cost of one batch
        # valid per 500 steps
        if 0 == (global_step + 1) % 500:
            train_writer.add_summary(summary, global_step)
            batch_id = np.random.randint(0, n_va_batches)  # 随机选一个验证batch
            [X_batch, y_batch] = get_batch(data_valid_path, batch_id)
            y_batch = to_categorical(y_batch)
            _batch_size = len(y_batch)
            feed_dict = {model.X_inputs: X_batch,
                         model.y_inputs: y_batch, model.batch_size: _batch_size,
                         model.tst: True, model.keep_prob: 1.0}
            summary, _cost = sess.run(valid_fetches, feed_dict)
            test_writer.add_summary(summary, global_step)

def main(_):
    global ckpt_path
    global last_score12
    if not os.path.exists(ckpt_path):
        os.makedirs(ckpt_path)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)
    elif not FLAGS.is_retrain:
        shutil.rmtree(summary_path)
        os.makedirs(summary_path)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)
    if not os.path.exists(log_path):
        os.makedirs(log_path)

    print('1.Loading data...')
    W_embedding = np.load(embedding_path)
    print('training sample_num = %d' % n_tr_batches)
    print('valid sample_num = %d' % n_va_batches)
    logger = get_logger(log_path + FLAGS.log_file_train)

    print('2.Building model...')
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        model = network.Atten_TextCNN(W_embedding, settings)
        with tf.variable_scope('training_ops') as vs:
            learning_rate = tf.train.exponential_decay(FLAGS.lr, model.global_step,
                                                       FLAGS.decay_step,
                                                       FLAGS.decay_rate, staircase=True)
            with tf.variable_scope('Optimizer1'):
                tvars1 = tf.trainable_variables()
                grads1 = tf.gradients(model.loss, tvars1)
                optimizer1 = tf.train.AdamOptimizer(learning_rate=learning_rate)
                train_op1 = optimizer1.apply_gradients(zip(grads1, tvars1),
                                                   global_step=model.global_step)
            with tf.variable_scope('Optimizer2'):
                tvars2 = [tvar for tvar in tvars1 if 'embedding' not in tvar.name]
                grads2 = tf.gradients(model.loss, tvars2)
                optimizer2 = tf.train.AdamOptimizer(learning_rate=learning_rate)
                train_op2 = optimizer2.apply_gradients(zip(grads2, tvars2),
                                                   global_step=model.global_step)
            update_op = tf.group(*model.update_emas)
            merged = tf.summary.merge_all()  # summary
            train_writer = tf.summary.FileWriter(summary_path + 'train', sess.graph)
            test_writer = tf.summary.FileWriter(summary_path + 'test')
            training_ops = [v for v in tf.global_variables() if v.name.startswith(vs.name+'/')]

        if os.path.exists(ckpt_path + "checkpoint"):
            print("Restoring Variables from Checkpoint...")
            model.saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))
            f1_micro, f1_macro, score12 = valid_epoch(data_valid_path, sess, model)
            print('f1_micro=%g, f1_macro=%g, score12=%g' % (f1_micro, f1_macro, score12))
            sess.run(tf.variables_initializer(training_ops))
            train_op2 = train_op1
        else:
            print('Initializing Variables...')
            sess.run(tf.global_variables_initializer())

        print('3.Begin training...')
        print('max_epoch=%d, max_max_epoch=%d' % (FLAGS.max_epoch, FLAGS.max_max_epoch))
        logger.info('max_epoch={}, max_max_epoch={}'.format(FLAGS.max_epoch, FLAGS.max_max_epoch))
        train_op = train_op2
        for epoch in range(FLAGS.max_max_epoch):
            print('\nepoch: ', epoch)
            logger.info('epoch:{}'.format(epoch))
            global_step = sess.run(model.global_step)
            print('Global step %d, lr=%g' % (global_step, sess.run(learning_rate)))
            if epoch == FLAGS.max_epoch:
                train_op = train_op1

            train_fetches = [merged, model.loss, train_op, update_op]
            valid_fetches = [merged, model.loss]
            train_epoch(data_train_path, sess, model, train_fetches,
                        valid_fetches, train_writer, test_writer, logger)
        # 最后再做一次验证
        f1_micro, f1_macro, score12 = valid_epoch(data_valid_path, sess, model)
        print('END:Global_step=%d: f1_micro=%g, f1_macro=%g, score12=%g' % (
            sess.run(model.global_step), f1_micro, f1_macro, score12))
        logger.info('END:Global_step={}: f1_micro={}, f1_macro={}, score12={}'.
                    format(sess.run(model.global_step), f1_micro, f1_macro, score12))
        if score12 > last_score12:
            saving_path = model.saver.save(sess, model_path, sess.run(model.global_step)+1)
            print('saved new model to %s ' % saving_path)
            logger.info('saved new model to {}'.format(saving_path))

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


from __future__ import absolute_import, print_function
import unittest
import poker

testdata = ["5H 5C 6S 7S KD 2C 3S 8S 8D TD",
            "5D 8C 9S JS AC 2C 5C 7D 8S QH",
            "2D 9C AS AH AC 3D 6D 7D TD QD",
            "4D 6S 9H QH QC 3D 6D 7H QD QS",
            "2H 2D 4C 4D 4S 3C 3D 3S 9S 9D"]

testwinner = [1, 0, 1, 0, 0]

class TestPoker(unittest.TestCase):
    def testpoker(self):
        games = []
        for line in testdata:
            hand0, hand1 = line[:14], line[15:]
            player0 = poker.player(hand0)
            player1 = poker.player(hand1)
            game = poker.game()
            game.set_player(player0, player1)
            games.append(game)
        for i in range(5):
            game = games[i]
            winner = testwinner[i]
            self.assertEqual(game.winner(), winner)

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


import numpy as np

# Color palette
palette = {
           0: (0, 0, 0),        # Undefined (black)
           1: (255, 255, 255),  # Impervious surfaces (white)
           2: (0, 0, 255),      # Buildings (dark blue)
           3: (0, 128, 0),      # Vegetation (light green)
           4: (255, 0, 0),      # Water (red)
          }

invert_palette = {v: k for k, v in palette.items()}

def convert_from_color(arr_3d, palette=invert_palette):
    """ RGB-color encoding to grayscale labels """
    arr_2d = np.zeros((arr_3d.shape[0], arr_3d.shape[1]), dtype=np.uint8)

    for c, i in palette.items():
        m = np.all(arr_3d == np.array(c).reshape(1, 1, 3), axis=2)
        arr_2d[m] = i

    return arr_2d

def convert_to_color(arr_2d, palette=palette):
    """ grayscale labels to RGB-color encoding """
    arr_3d = np.zeros((arr_2d.shape[0], arr_2d.shape[1], 3), dtype=np.uint8)

    for c, i in palette.items():
        m = arr_2d == c
        arr_3d[m] = i

    return arr_3d


import logging

import feedparser
import requests

from .. import read_list

log = logging.getLogger(__name__)

class VideoFeed:
    def __init__(self, known_path, url):
        self.url = url
        self.read_list = read_list.ReadList(known_path, url)

    def is_new(self, item):
        return self.read_list.is_new(item.id)

    def fetch(self):
        try:
            response = requests.get(self.url)
            response.raise_for_status()
            feed = feedparser.parse(response.content)
            items = feed.entries
            if len(items) == 0:
                raise Exception("Empty feed. Is site down?")
            new_items = list(filter(self.is_new, items))
            log.info("Feed contains %d items, %d are new." % (len(items), len(new_items)))
            # iterate first, then save new read list
            for i in new_items:
                yield i
            self.read_list.save()
        except Exception:
            log.exception("Unexpected error with %s", self.url, exc_info=True)

    def fetch_video_codes(self):
        raise NotImplementedError

    def append_to_queue(self, queue_path):
        codes = self.fetch_video_codes()
        links = ["\nhttps://www.youtube.com/watch?v=" + v for v in codes]
        f = open(queue_path, "a")
        f.writelines(links)
        f.close()


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

"""DESCRIPTION
         This module is a list of coding exercises. The intention
         is to emulate some functions from other languages.
"""

#==================================================================
#                     Generate a list of divisors
#==================================================================

def divisors_of(number_to_divide):
    """Find the integer divisors of <number_to_divide>
    param: number_to_divide."""
    number_to_divide = number_to_divide + 1
    possible_divisors_list = list(range(number_to_divide))
    if 0 in possible_divisors_list:
        possible_divisors_list.remove(0)
    if 1 in possible_divisors_list:
        possible_divisors_list.remove(1)
    divisors_list = [1]
    number_to_divide = number_to_divide - 1
    for each_possible_divisor in list(possible_divisors_list):
        if number_to_divide % each_possible_divisor == 0:
            divisors_list.append(each_possible_divisor)
        elif number_to_divide % each_possible_divisor != 0:
            possible_divisors_list.remove(each_possible_divisor)
    return divisors_list

#==================================================================
#           Generate a range of prime numbers
#==================================================================

def prime_range(how_many_primes_you_need):
    """Return a range of prime numbers.
    param: how_many_primes_you_need"""
    primes = []
    for each_number in range(how_many_primes_you_need):
        if len(divisors_of(each_number)) == 2:
            primes
            primes.append(each_number)
    return primes

#====================================================
#           Partition a lists into sublists of size x
#====================================================

def partition_list(working_list, size_of_partition):
    """ Return a list of lists of the size of <size_of_partition>."""
    final_list = []
    for each_elem in range(0, len(working_list) + 1, size_of_partition):
        elem = working_list[each_elem: each_elem + size_of_partition]
        final_list.append(elem)
    for each_sublist in final_list:
        if len(each_sublist) != size_of_partition:
            final_list.remove(each_sublist)
    return list(final_list)

#==================================================================
#                       Factorial function
#==================================================================

def factorial_increment(limit_of_factorial_increment):
    """Return the factorial product of the sequence
    with limit <limit_of_factorial_increment> """
    limit_of_factorial_increment = limit_of_factorial_increment + 1
    limit_of_factorial_increment = int(limit_of_factorial_increment)
    terms_of_factorial_sequence_list = \
        list(range(1, limit_of_factorial_increment, 1))
    limit_of_factorial_increment = limit_of_factorial_increment - 1
    factorial_product = 1
    if limit_of_factorial_increment == 1:
        return 1
    elif limit_of_factorial_increment == 0:
        return 1
    while len(terms_of_factorial_sequence_list) != 0:
        for each_term in terms_of_factorial_sequence_list:
            if each_term == 1:
                term, *rest = terms_of_factorial_sequence_list
                next_term, *next_rest = rest
                factorial_product = term * next_term
                terms_of_factorial_sequence_list.remove(term)
                terms_of_factorial_sequence_list.remove(next_term)
            elif each_term != 1:
                term, *rest = terms_of_factorial_sequence_list
                factorial_product = factorial_product * term
                terms_of_factorial_sequence_list.remove(term)
    return factorial_product

#==================================================================
#                      Riffle elements of two lists
#==================================================================

def interleave_lists(first_list, second_list):
    """Returns a single list with elements of first_list interlaced
    with elements of second_list."""
    first_list = first_list[:]
    second_list = second_list[:]
    len_of_first_list = int(len(first_list))
    riffled_list = []
    if len_of_first_list == 0:
        return riffled_list
    else:
        while len_of_first_list != 0:
            len_of_first_list -= 1
            for each_element in first_list[:]:
                global riffled_list
                element_of_first_list, *rest_of_first_list = first_list
                element_of_second_list, *rest_of_second_list = second_list
                riffled_list.append(element_of_first_list)
                riffled_list.append(element_of_second_list)
                first_list.remove(element_of_first_list)
                second_list.remove(element_of_second_list)
                first_list = rest_of_first_list
                second_list = rest_of_second_list
    return riffled_list

#==================================================================
#                           Prime Factorization
#==================================================================

def prime_factors(input_number):
    """
    Return a list with prime factors of argument <int_number>.
    param: input_number
    Arguments:
                Integer type input. -> List
    """
    prime_factors_list = []
    first_prime = 2
    while first_prime**2 <= input_number:
        while (input_number % first_prime) == 0:
            prime_factors_list.append(first_prime)
            input_number //= first_prime
        first_prime += 1
    if input_number > 1:
        prime_factors_list.append(input_number)
    return prime_factors_list

#==================================================================
#                           Fibonacci Series
#==================================================================

def fibonacci_range(size_of_fibonacci_range):
    """Fibonacci series:
        Returns a list with a sequnce of numbers
        in which the next is the sum of the las two.
        Arguments:
            limit of the series.
     """
    fibonacci_number_list = [1]
    term, next_term = 0, 1
    while True:
        term, next_term = next_term, term + next_term
        fibonacci_number_list.append(next_term)
        if len(fibonacci_number_list) == int(size_of_fibonacci_range):
                break
    return fibonacci_number_list

#==================================================================
#                            Module runs as script
#==================================================================

if __name__ == '__main__':
    fibonacci_range()
    prime_factors()
    factorial_increment()


class Node:
    def __init__(self, item):
        self.data = item
        self.next = None

class LinkedList:
    def __init__(self):
        self.nodeCount = 0
        self.head = None
        self.tail = None

    def __repr__(self):
        if self.nodeCount == 0:
            return 'LinkedList: empty'

        s = ''
        curr = self.head
        while curr is not None:
            s += repr(curr.data)
            if curr.next is not None:
                s += ' -> '
            curr = curr.next
        return s

    def getAt(self, pos):
        if pos < 1 or pos > self.nodeCount:
            return None
        i = 1
        curr = self.head
        while i < pos:
            curr = curr.next
            i += 1
        return curr

    def traverse(self):
        if not self.head:
            return []

        answer = []
        curr = self.head

        while curr is not None:
            answer.append(curr.data)
            curr = curr.next

        return answer

    def insertAt(self, pos, newNode): # 요소 삽입
        if pos < 1 or pos > self.nodeCount +1:
            return False

        if pos == 1:
            newNode.next = self.head
            self.head = newNode
        else:
            if pos == self.nodeCount + 1:
                prev = self.tail
            else:
                prev = self.getAt(pos-1)
            newNode.next = prev.next
            prev.next = newNode

        if pos == self.nodeCount + 1: # 맨 처음, 맨 마지막
            self.tail = newNode

        self.nodeCount +=1
        return True

    def popAt(self, pos):
        """
        pos가 1일때 self.head = self.head.next
        pos가 마지막일때  self.tail = prev
        유일한 노드를 삭제할 때  유일한 노드 = self.head = 1 , self.tail = 1
        :param pos: 위치
        :return:
        """
        if pos < 1 or pos > self.nodeCount:
            raise IndexError
        result = 0
        if pos == 1:
            if pos == self.nodeCount: # 유일한 노드
                self.tail = None
            result = self.head.data
            self.head = self.head.next

        else:
            prev = self.getAt(pos-1)
            curr = prev.next
            prev.next = curr.next
            result = curr.data
            if pos == self.nodeCount:
                self.tail = prev

        self.nodeCount -= 1;
        return result

a = Node(10)
b = Node(20)
c = Node(30)

L = LinkedList()
L2 = LinkedList()

L.insertAt(1,a)
L.insertAt(2,b)

L2.insertAt(1,c)
L2.insertAt(2,b)

print(L)
print(L2)


'''
2D Multiple Circles Problem
'''
from lsdo_genie import Genie2D
from lsdo_genie.utils.geometric_shapes import Multi_circle
import numpy as np

num_surface_pts = 25

centers = [[-13.,-0.5],[-7.,2.],[2.,0.],[10.,-4.]]
radii = [2.,2.,4.,3.]
geom_shape = Multi_circle(centers,radii)

surface_points = geom_shape.surface_points(num_surface_pts)
surface_normals = geom_shape.unit_normals(num_surface_pts)

custom_domain = np.array([
    [-18.,18.],
    [-8.,8.]
])

genie = Genie2D(verbose=True)
genie.input_point_cloud(
    surface_points=surface_points,
    surface_normals=surface_normals,
)
genie.config(
    domain=custom_domain,
    max_control_points=30,
    min_ratio=0.75,
)
genie.solve_energy_minimization(
    Ln=1e0,
    Lr=1e-4,
)
genie.visualize()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Few more things we might wanna work out here.

Our lack of the module :mod:`inspect` is pretty surprising.

Refer to either `IPython.core.oinspect` or `xonsh.inspectors`
for some good uses of the std lib module.

"""
from pprint import pformat

from pygments import highlight
from pygments.formatters.terminal256 import TerminalTrueColorFormatter

try:
    from gruvbox.gruvbox import GruvboxStyle
except ImportError:
    from pygments.styles.inkpot import InkPotStyle

    style = InkPotStyle()
else:
    style = GruvboxStyle()

from IPython.core.getipython import get_ipython
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.lib.lexers import IPyLexer
from IPython.lib.pretty import pprint

@magics_class
class PrettyColorfulInspector(Magics):
    """Implementation for a magic function that inspects a given python object.

    The extension then prints a syntax-highlighted and pretty-printed
    version of the provided object.
    """

    # Use Pygments to do syntax highlighting
    lexer = IPyLexer()
    if GruvboxStyle is not None:
        style = GruvboxStyle
    else:
        style = InkPotStyle
    formatter = TerminalTrueColorFormatter(style=style)

    def shell(self):
        # why the fuck is this returning none
        return get_ipython()

    def __repr__(self):
        return f"<{self.__class__.__name__}>:"

    @line_magic
    def ins(self, line=None):
        """Alias for the `%inspect_obj magic defined here."""
        self.inspect(line=line)

    @line_magic
    def inspect_obj(self, line=None):
        """Deviate from the original implementation a bit.

        In this version, we'll use the IPython lexer used at IPython.lib
        instead of pygments.

        Parameters
        ----------
        line : str
            Line to be evaluated by the IPython shell.
            Note that this invokes the get_ipython().ev() method.
            So we might wanna wrap this in a try/except but idk what it'll raise.

        """
        if not line:
            return

        # evaluate the line to get a python object
        python_object = self.shell.ev(line)

        # Pretty Print/Format the object
        # Print the output, but don't return anything (otherwise, we'd
        # potentially get a wall of color-coded text.
        formatted_dict = pformat(python_object.__dict__)
        print(highlight(formatted_dict, lexer, formatter).strip())
        pprint(python_object)

def load_ipython_extension(shell=None):
    """Add to the list of extensions used by IPython."""
    if shell is None:
        shell = get_ipython()
    if shell is None:
        return
    shell.register_magics(PrettyColorfulInspector)
    shell.register_magic_function(PrettyColorfulInspector.inspect_obj)
    shell.register_magic_function(PrettyColorfulInspector.ins)

if __name__ == "__main__":
    load_ipython_extension(get_ipython())


import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import plotly.express as px
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
import pickle
st.set_page_config(page_title="Air Quality", page_icon="ðŸ’¨", layout='wide', initial_sidebar_state='auto')

@st.cache_data
def load_data():
    df = pd.read_csv("data/model_input.csv")
    df.drop(['location'],axis=1,inplace=True)
    df = df[df['lceq_avg'] != 0]
    airquality = pd.read_csv("data/Air_Quality.csv", delimiter=",")
    with open('data/xgb_airquality.pkl', 'rb') as f:
        model = pickle.load(f)
    return df, airquality, model
df, airquality, model = load_data()

airquality['time_stamp'] = pd.to_datetime(airquality['time_stamp'])
airquality['month'] = airquality['time_stamp'].dt.month
airquality['day_month'] = airquality['time_stamp'].dt.day
airquality['day_week'] = airquality['time_stamp'].dt.dayofweek.apply(lambda x: 7 if x == 6 else x + 1)
airquality['hour'] = airquality['time_stamp'].dt.hour
airquality['minute'] = airquality['time_stamp'].dt.minute

merged_df = pd.merge(df, airquality, how='left', on=['month', 'day_month', 'day_week', 'hour', 'minute'])

new_df = merged_df.drop(['lcpeak_avg', 'lceq_avg', 'v85', 'Telraam data', 'avg_pedestrians', 'avg_bikes', 'avg_cars', 'avg_trucks' ], axis=1)

st.title("Air Quality analysis ðŸ’¨")
st.markdown("""In this section, we will analyse the air quality data found in the PurpleAir API.
We will start by looking at the data and then we will try to find some correlations between the different variables.""")

# Group the data by month and calculate the mean of '2.5_um_count'
grouped_df = new_df.groupby('month')['2.5_um_count'].mean().reset_index()

expander_corr = st.expander("Correlation heatmap explanation")
expander_corr.markdown("We will start by looking at the correlation heatmap of the different variables. This will give us a first idea of the variables that are somewhat correlated with the count of 2.5um particles.")
columns_of_interest = ['LC_TEMP', 'LC_DAILYRAIN', 'LC_RAD', 'LC_WINDDIR', 'month', '2.5_um_count']
corr_matrix = new_df[columns_of_interest].corr()

# Create the correlation heatmap using Plotly
fig = go.Figure(data=go.Heatmap(
    z=corr_matrix.values,
    x=corr_matrix.columns,
    y=corr_matrix.columns,
    colorscale='RdBu',
    zmin=-1,
    zmax=1,
    colorbar=dict(title="Correlation")
))

# Add custom annotations for the correlation values inside each square
annotations = []
for i, row in enumerate(corr_matrix.values):
    for j, value in enumerate(row):
        annotations.append(
            dict(
                x=corr_matrix.columns[j],
                y=corr_matrix.columns[i],
                text=str(round(value, 2)),
                font=dict(color='white' if abs(value) > 0.5 else 'black'),
                showarrow=False
            )
        )

fig.update_layout(
    title='Correlation Heatmap',
    xaxis_title='Variables',
    yaxis_title='Variables',
    width=800,
    height=600,
    annotations=annotations
)
expander_corr.plotly_chart(fig)

monthly_avg = new_df.groupby('month')['2.5_um_count'].mean().reset_index()
expander_mon = st.expander("Average PM2.5 particles count per Month")
expander_mon.markdown("We will now look at the average PM2.5 particles count per Month. We can see that there is a negative correlation between the 2.5_um_count and the month. This shows that the air quality is better during the summer months.")
fig = px.line(monthly_avg, x='month', y='2.5_um_count', color_discrete_sequence=['#3366cc'])
fig.update_layout(title='Average 2.5_um_count per Month',
                  xaxis_title='Month', yaxis_title='Average 2.5_um_count')
expander_mon.plotly_chart(fig)

expander_temp = st.expander("Average PM2.5 particles count per Temperature")
expander_temp.markdown("We will now look at the average PM2.5 particles count per Temperature. We can see that there is a negative correlation between the 2.5_um_count and the LC_TEMP. This means that when the temperature is higher, the air quality is better.")
fig = px.scatter(new_df, x="LC_TEMP", y="2.5_um_count", trendline="ols",
                 animation_frame="month", animation_group="day_month", color="day_month",
                 hover_name="day_month", range_x=[-5, 25], range_y=[0, 40])
fig.update_layout(title='2.5_um_count by LC_TEMP', xaxis_title='LC_TEMP', yaxis_title='2.5_um_count')
expander_temp.plotly_chart(fig)

merged_df['2.5_um_count'] = merged_df['2.5_um_count'].fillna(method='ffill').rolling(window=10, min_periods=1).mean()
merged_df = merged_df.drop(['time_stamp'], axis=1)
x = merged_df.drop(['2.5_um_count'], axis=1)
y = merged_df['2.5_um_count']
xgb = model

expander_imp = st.expander("Feature importance")
expander_imp.markdown("We will now look at the feature importance of the different variables. The used model is a XGBoost model, with the target variable being the 2.5_um_count. By looking at the feature importance, we can see which variables are the most important in predicting the 2.5_um_count. We can see that the most important variables are the temporal data and weather conditions.")
importance_sorted = sorted(zip(xgb.feature_importances_, x.columns), reverse=True)
importance_values_sorted = [imp for imp, _ in importance_sorted]
variable_names_sorted = [var for _, var in importance_sorted]

fig = px.bar(x=importance_values_sorted, y=variable_names_sorted, orientation='h')

fig.update_layout(
    title='Feature importance',
    xaxis_title='Importance',
    yaxis_title='Variables',
    yaxis=dict(
        tickmode='array',
        ticktext=variable_names_sorted,
        tickvals=variable_names_sorted,
        showticklabels=True,
        automargin=True
    )
)

expander_imp.plotly_chart(fig)


import platform
from tkinter import filedialog
from web3 import Web3
from web3.contract import Contract
from web3.providers.rpc import HTTPProvider
from solcx import install_solc
install_solc(version='latest')
from solcx import compile_source
import subprocess
import os
import tkinter as tk
from PIL import Image, ImageTk
import threading
import json
from dotenv import load_dotenv

load_dotenv()

PRIVATE_KEY = os.getenv('PRIVATE_KEY')

onChainSmartContract = None
web3_1 = None
web3_2 = None
web3_3 = None
web3_4 = None

def get_sc(filename):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    with open(os.path.join(current_dir, filename), 'r') as file:
        text = file.read()
    return text

def update_storage(map):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    filename = os.path.join(current_dir, 'onchaindata.json')
    try:
        with open(filename, 'r') as file:
            maps = json.load(file)
    except FileNotFoundError:
        maps = []
    maps.append(map)
    with open(filename, 'w') as file:
        json.dump(maps, file)

def read_storage(name: str):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    filename = os.path.join(current_dir, 'onchaindata.json')
    with open(filename, 'r') as file:
        maps = json.load(file)
    for map_data in maps:
        if map_data['name'] == name:
            return map_data
    return None

def deploy(w3: Web3, contract: Contract, name: str):
    sm_transaction = {
        "from": w3.eth.accounts[0],
        "maxFeePerGas": w3.to_hex(1000000000000),
        "maxPriorityFeePerGas": w3.to_hex(0),
        "gas": w3.to_hex(1000000000000),
        "nonce": w3.eth.get_transaction_count(w3.eth.accounts[0]),
        "data": contract.bytecode,
        'chainId': w3.eth.chain_id
    }
    signedTransaction = w3.eth.account.sign_transaction(sm_transaction, PRIVATE_KEY)
    transaction_hash = w3.eth.send_raw_transaction(signedTransaction.rawTransaction)
    tx_receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)
    contract = w3.eth.contract(address=tx_receipt.contractAddress, abi=contract.abi, bytecode=contract.bytecode)
    new_map = {
        'name': name,
        'address': str(contract.address),
        'abi': str(contract.abi)
    }
    update_storage(new_map)
    return contract

def read(contract: Contract, function_name: str, args: list):
    if len(args) == 0:
        # result = contract.functions.askForDeploySmartContract().call()
        result = contract.functions[function_name]().call()
    elif len(args) == 1:
        result = contract.functions[function_name](args[0]).call()
    elif len(args) == 2:
        result = contract.functions[function_name](args[0], args[1]).call()
    else:
        result = contract.functions[function_name](args[0], args[1], args[2]).call()
    return result

def write(w3: Web3, contract: Contract, function_name: str, args: any):
    new_transaction = {
        "from": w3.eth.accounts[0],
        "to": contract.address,
        "data": contract.encodeABI(fn_name=function_name, args=args),
        "gas": w3.to_hex(1000000000000),
        # "gasPrice": w3.to_wei('0', 'gwei'),
        "maxFeePerGas": w3.to_hex(1000000000000),
        "maxPriorityFeePerGas": w3.to_wei(0, 'gwei'),
        "nonce": w3.eth.get_transaction_count(w3.eth.accounts[0]),
        'chainId': w3.eth.chain_id
    }
    #"gas": w3.to_hex(6721975),
    signedTransaction = w3.eth.account.sign_transaction(new_transaction, PRIVATE_KEY)
    transaction_hash = w3.eth.send_raw_transaction(signedTransaction.rawTransaction)
    receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)
    return receipt

def init_web3():
    global web3_1
    global web3_2
    global web3_3
    global web3_4
    web3_1 = Web3(HTTPProvider("http://127.0.0.1:8545"))
    if web3_1.is_connected():
        print("Connected to http://127.0.0.1:8545")
    else:
        print("Not connected to http://127.0.0.1:8545")
    web3_2 = Web3(HTTPProvider("http://127.0.0.1:8546"))
    if web3_2.is_connected():
        print("Connected to http://127.0.0.1:8546")
    else:
        print("Not connected to http://127.0.0.1:8546")
    web3_3 = Web3(HTTPProvider("http://127.0.0.1:8547"))
    if web3_3.is_connected():
        print("Connected to http://127.0.0.1:8547")
    else:
        print("Not connected to http://127.0.0.1:8547")
    web3_4 = Web3(HTTPProvider("http://127.0.0.1:8548"))
    if web3_4.is_connected():
        print("Connected to http://127.0.0.1:8548")
    else:
        print("Not connected to http://127.0.0.1:8548")

def loadOnChainManager():
    compiledSmartContract = compile_source(get_sc("onchainmanager.sol"), output_values=['abi', 'bin'])
    _, smartContractInterface = compiledSmartContract.popitem()
    smartContractBytecode = smartContractInterface['bin']
    smartContractAbi = smartContractInterface['abi']
    global onChainSmartContract
    onChainSmartContract = web3_1.eth.contract(abi=smartContractAbi, bytecode=smartContractBytecode)
    count = web3_1.eth.get_transaction_count(web3_1.eth.accounts[0])
    sc = read_storage("onchainsc")
    if sc is None:
        onChainSmartContract = deploy(web3_1, onChainSmartContract, 'onchainsc')
        # my_contract = web3_1.eth.contract(address=onChainSmartContract.address, abi=onChainSmartContract.abi)
    else:
        onChainSmartContract = web3_1.eth.contract(address=sc["address"], abi=smartContractAbi, bytecode=smartContractBytecode)

    write(web3_1, onChainSmartContract, 'setAddress1', ["http://127.0.0.1:8546"])
    write(web3_1, onChainSmartContract, 'setAddress2', ["http://127.0.0.1:8547"])
    write(web3_1, onChainSmartContract, 'setAddress3', ["http://127.0.0.1:8548"])

class Loader(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        parent.title("Progetto Software Security & Blockchain")
        self.result_text = tk.Text(self)
        tk.Button(self, text="Avvia le Blockchain", command=self.run_script_threaded).grid(row=1, column=0)
        tk.Button(self, text="Avvia il Programma", command=self.start_app).grid(row=1, column=1)
        self.result_text.grid(row=2, column=0, columnspan=2)

    def run_script_threaded(self):
        threading.Thread(target=self.run_script).start()
        # threading.Thread(target=self.init_web3_thread).start()

    def start_app(self):
        # self.result_text.insert(tk.END, "Tutte le Blockchain sono state inizializzate correttamente!")
        # self.result_text.insert(tk.END, "Caricamento del programma... (impiega circa 15 secondi)")
        homepage = HomePage(self.master)
        init_web3()
        loadOnChainManager()

    def run_script(self):
        current_dir = os.path.dirname(os.path.abspath(__file__))
        system = platform.system()
        try:
            if system == 'Windows':
                dir = os.path.join(current_dir, "init.sh")
                process = subprocess.Popen([dir], stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE, universal_newlines=True)
            elif system == 'Linux':
                command = ['bash', os.path.join(current_dir, "init.sh")]
                process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
            elif system == 'Darwin':
                command = ['bash', os.path.join(current_dir, "init.sh")]
                process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
            else:
                print(f"Error: Unsupported system '{system}'")
        except Exception as e:
            print("Errore", "File non trovato al percorso " + e)

        while True:
            output = process.stdout.readline()
            if not output and process.poll() is not None:
                break
            self.result_text.insert(tk.END, output)
            self.result_text.see(tk.END)

class HomePage(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        # screen_width = root.winfo_screenwidth()
        # screen_height = root.winfo_screenheight()
        # self.geometry(f"{screen_width//2}x{screen_height//2}+{screen_width//4}+{screen_height//4}")
        self.title("Home Page")

        current_dir = os.path.dirname(os.path.abspath(__file__))
        button1_image = Image.open(os.path.join(current_dir, "receipt_long.png"))
        button2_image = Image.open(os.path.join(current_dir, "assignment.png"))
        button3_image = Image.open(os.path.join(current_dir, "tty.png"))
        button4_image = Image.open(os.path.join(current_dir, "delete.png"))
        button1_photo = ImageTk.PhotoImage(button1_image)
        button2_photo = ImageTk.PhotoImage(button2_image)
        button3_photo = ImageTk.PhotoImage(button3_image)
        button4_photo = ImageTk.PhotoImage(button4_image)

        title_label = tk.Label(self, text="Scegli come deployare il tuo smart contract, oppure se richiamare un metodo di uno smart contract esistente.", font=("Arial", 13))
        title_label.grid(row=0, column=0, columnspan=3, pady=20)

        button1 = tk.Button(self, image=button1_photo, text="Deploy il file .sol", compound=tk.TOP, font=("Arial", 12), command=self.button1_clicked)
        button1.image = button1_photo
        button1.grid(row=1, column=0, padx=0, pady=10)
        frame1 = tk.Frame(self, height=100, width=100)
        frame1.pack_propagate(0)
        frame1.grid(row=2, column=0, padx=0, pady=10)
        label1 = tk.Label(frame1, text="Carica il tuo\nfile in solidity\nin cui è scritto\nlo Smart Contract", font=("Arial", 13))
        label1.pack(fill=tk.BOTH, expand=1)

        button2 = tk.Button(self, image=button2_photo, text="Deploy da ABI e Bytecode", compound=tk.TOP, font=("Arial", 12), command=self.button2_clicked)
        button2.image = button2_photo
        button2.grid(row=1, column=1, padx=0, pady=10)
        frame2 = tk.Frame(self, height=100, width=100)
        frame2.pack_propagate(0)
        frame2.grid(row=2, column=1, padx=0, pady=10)
        label2 = tk.Label(frame2, text="Carica il tuo\nSmart Contract\nscrivendo l'ABI\ne il Bytecode", font=("Arial", 13))
        label2.pack(fill=tk.BOTH, expand=1)

        button3 = tk.Button(self, image=button3_photo, text="Chiama metodo", compound=tk.TOP, font=("Arial", 12), command=self.button3_clicked)
        button3.image = button3_photo
        button3.grid(row=1, column=2, padx=0, pady=10)
        frame3 = tk.Frame(self, height=100, width=100)
        frame3.pack_propagate(0)
        frame3.grid(row=2, column=2, padx=0, pady=10)
        label3 = tk.Label(frame3, text="Chiama un\nmetodo di uno\nSmart Contract\nesistente", font=("Arial", 13))
        label3.pack(fill=tk.BOTH, expand=1)

        button4 = tk.Button(self, image=button4_photo, text="Elimina Smart Contract", compound=tk.TOP, font=("Arial", 12), command=self.button4_clicked)
        button4.image = button4_photo
        button4.grid(row=1, column=3, padx=0, pady=10)
        frame4 = tk.Frame(self, height=100, width=100)
        frame4.pack_propagate(0)
        frame4.grid(row=2, column=3, padx=0, pady=10)
        label4 = tk.Label(frame4, text="Elimina uno\nSmart Contract", font=("Arial", 13))
        label4.pack(fill=tk.BOTH, expand=1)

    def get_folder_path():
        while True:
            folder_path = input("Please enter the path of the folder: ")
            if os.path.isdir(folder_path):
                return folder_path
            else:
                print("Invalid folder path. Please try again.")

    def button1_clicked(self):
        soliditypage = SolidityPage(self.master)

    def button2_clicked(self):
        abibytecodepage = ABIBytecodePage(self.master)

    def button3_clicked(self):
        callmethodpage = MethodsPage(self.master)

    def button4_clicked(self):
        deletepage = DeletePage(self.master)

class SolidityPage(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        # screen_width = root.winfo_screenwidth()
        # screen_height = root.winfo_screenheight()
        # self.geometry(f"{screen_width//2}x{screen_height//2}+{screen_width//4}+{screen_height//4}")
        self.title("Deploy file .sol")
        self.name_label = tk.Label(self, text="Nome:")
        self.name_label.pack()

        self.name_entry = tk.Entry(self)
        self.name_entry.pack()

        self.file_label = tk.Label(self, text="File:")
        self.file_label.pack()

        self.file_entry = tk.Entry(self, state="readonly")
        self.file_entry.pack()

        self.browse_button = tk.Button(self, text="Cerca", command=self.browse_file)
        self.browse_button.pack()

        frame = tk.Frame(self)

        self.ok_button = tk.Button(frame, text="Deploy", command=self.ok_button_click)
        self.ok_button.pack(side=tk.LEFT)

        self.cancel_button = tk.Button(frame, text="Annulla", command=self.cancel_button_click)
        self.cancel_button.pack(side=tk.LEFT)

        frame.pack()

    def browse_file(self):
        filetypes = (("Solidity files", "*.sol"), ("All files", "*.*"))
        filename = filedialog.askopenfilename(filetypes=filetypes)
        if filename:
            self.file_entry.config(state="normal")
            self.file_entry.delete(0, tk.END)
            self.file_entry.insert(0, filename)
            self.file_entry.config(state="readonly")

    def ok_button_click(self):
        name = self.name_entry.get()
        filename = self.file_entry.get()
        if not filename.endswith(".sol"):
            tk.messagebox.showerror("Error", "Invalid file format. Please select a .sol file.")
        else:
            compiledSmartContract = compile_source(get_sc(os.path.basename(filename).split('/')[-1]), output_values=['abi', 'bin'])
            _, smartContractInterface = compiledSmartContract.popitem()
            smartContractBytecode = smartContractInterface['bin']
            smartContractAbi = smartContractInterface['abi']
            receipt = write(web3_1, onChainSmartContract, "getNextAddress", [])
            logs = onChainSmartContract.events.NextAddressReturned().process_receipt(receipt)
            nextAddress = logs[0]['args']['nextAddress']
            web3_c = Web3(HTTPProvider(nextAddress))
            if web3_c.is_connected():
                print("Connected to " + nextAddress)
                customSmartContract = web3_c.eth.contract(abi=smartContractAbi, bytecode=smartContractBytecode)
                customSmartContract = deploy(web3_c, customSmartContract, name)
                write(web3_1, onChainSmartContract, 'addContract', [name, str(nextAddress), str(customSmartContract.address), str(smartContractAbi)])
                result = read(onChainSmartContract, 'getContract', [name])
                print("Result: " + str(result))
                show_toast('Lo Smart Contract è stato deployato con successo', '')
            else:
                print("Not connected to " + nextAddress)

            self.destroy()

    def cancel_button_click(self):
        self.destroy()

class ABIBytecodePage(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        # screen_width = root.winfo_screenwidth()
        # screen_height = root.winfo_screenheight()
        # self.geometry(f"{screen_width//2}x{screen_height//2}+{screen_width//4}+{screen_height//4}")
        self.title("Deploy da ABI e Bytecode")
        self.name_label = tk.Label(self, text="Nome:")
        self.name_label.pack()

        self.name_entry = tk.Entry(self)
        self.name_entry.pack()

        self.abi_label = tk.Label(self, text="ABI:")
        self.abi_label.pack()

        self.abi_label = tk.Entry(self)
        self.abi_label.pack()

        self.bytecode_label = tk.Label(self, text="Bytecode:")
        self.bytecode_label.pack()

        self.bytecode_label = tk.Entry(self)
        self.bytecode_label.pack()

        frame = tk.Frame(self)

        self.ok_button = tk.Button(frame, text="Deploy", command=self.ok_button_click)
        self.ok_button.pack(side=tk.LEFT)

        self.cancel_button = tk.Button(frame, text="Annulla", command=self.cancel_button_click)
        self.cancel_button.pack(side=tk.LEFT)

        frame.pack()

    def ok_button_click(self):
        name = self.name_entry.get()
        smartContractAbi = self.abi_label.get()
        smartContractBytecode = self.bytecode_label.get()
        receipt = write(web3_1, onChainSmartContract, "getNextAddress", [])
        logs = onChainSmartContract.events.NextAddressReturned().process_receipt(receipt)
        nextAddress = logs[0]['args']['nextAddress']
        web3_c = Web3(HTTPProvider(nextAddress))
        if web3_c.is_connected():
            print("Connected to " + nextAddress)
            customSmartContract = web3_c.eth.contract(abi=smartContractAbi, bytecode=smartContractBytecode)
            customSmartContract = deploy(web3_c, customSmartContract, name)
            write(web3_1, onChainSmartContract, 'addContract', [name, str(nextAddress), str(customSmartContract.address), str(smartContractAbi)])
            result = read(onChainSmartContract, 'getContract', [name])
            print("Result: " + str(result))
            show_toast('Lo Smart Contract è stato deployato con successo', '')
        else:
            print("Not connected to " + nextAddress)

        self.destroy()

    def cancel_button_click(self):
        self.destroy()

class MethodsPage(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        # screen_width = root.winfo_screenwidth()
        # screen_height = root.winfo_screenheight()
        # self.geometry(f"{screen_width//2}x{screen_height//2}+{screen_width//4}+{screen_height//4}")
        self.title("Chiama metodo")
        self.name_label = tk.Label(self, text="Nome dello Smart Contract:")
        self.name_label.pack()

        self.name_entry = tk.Entry(self)
        self.name_entry.pack()

        self.function_label = tk.Label(self, text="Nome della Funzione:")
        self.function_label.pack()

        self.function_label = tk.Entry(self)
        self.function_label.pack()

        self.selected_option = tk.IntVar()
        self.selected_option.set(0)

        self.option1 = tk.Radiobutton(self, text='Lettura', variable=self.selected_option, value=0)
        self.option1.pack(padx=10)
        self.option2 = tk.Radiobutton(self, text='Scrittura', variable=self.selected_option, value=1)
        self.option2.pack(padx=10)

        self.arg1_label = tk.Label(self, text="Arg 1:")
        self.arg1_label.pack()

        self.arg1_label = tk.Entry(self)
        self.arg1_label.pack()

        self.arg2_label = tk.Label(self, text="Arg 2:")
        self.arg2_label.pack()

        self.arg2_label = tk.Entry(self)
        self.arg2_label.pack()

        self.arg3_label = tk.Label(self, text="Arg 3:")
        self.arg3_label.pack()

        self.arg3_label = tk.Entry(self)
        self.arg3_label.pack()

        frame = tk.Frame(self)

        self.ok_button = tk.Button(frame, text="OK", command=self.ok_button_click)
        self.ok_button.pack(side=tk.LEFT)

        self.cancel_button = tk.Button(frame, text="Annulla", command=self.cancel_button_click)
        self.cancel_button.pack(side=tk.LEFT)

        frame.pack()

    def ok_button_click(self):
        selected_option = self.selected_option.get()
        name = self.name_entry.get()
        function = self.function_label.get()
        arg1 = self.arg1_label.get()
        arg2 = self.arg2_label.get()
        arg3 = self.arg3_label.get()
        data = read(onChainSmartContract, "getContract", [name])
        blockChainAddress = data[0]
        address = data[1]
        abi = data[2].replace("'", '"').replace('False', 'false').replace('True', 'true')
        web3_c = Web3(HTTPProvider(blockChainAddress))
        if web3_c.is_connected():
            print("Connected to " + blockChainAddress)
            customSmartContract = web3_c.eth.contract(address=address, abi=abi)
            if arg1 and arg2 and arg3:
                if selected_option == 0:
                    result = read(customSmartContract, function, [arg1, arg2, arg3])
                else:
                    write(web3_c, customSmartContract, function, [arg1, arg2, arg3])
            elif arg1 and arg2:
                if selected_option == 0:
                    result = read(customSmartContract, function, [arg1, arg2])
                else:
                    write(web3_c, customSmartContract, function, [arg1, arg2])
            elif arg1:
                if selected_option == 0:
                    result = read(customSmartContract, function, [arg1])
                else:
                    write(web3_c, customSmartContract, function, [arg1])
            else:
                if selected_option == 0:
                    result = read(customSmartContract, function, [])
                else:
                    write(web3_c, customSmartContract, function, [])
            if selected_option == 0:
                show_toast('Dati ottenuti correttamente', str(result))
            else:
                show_toast('Dati scritti correttamente', '')
            self.destroy()
        else:
            show_toast('Lo Smart Contract non esiste', 'Deployalo prima di eseguire un suo metodo')
            print("Not connected to " + blockChainAddress)

    def cancel_button_click(self):
        self.destroy()

def show_toast(title, description):
    window = tk.Toplevel()
    window.overrideredirect(True)
    window.attributes("-topmost", True)
    # screen_width = root.winfo_screenwidth()
    # screen_height = root.winfo_screenheight()
    # window.geometry(f"{500}x{100}+{screen_width//2-50}+{screen_height//2-50}")

    # create a frame for the toast message
    frame = tk.Frame(window, bg='white', bd=1, relief=tk.RAISED)
    frame.pack(side=tk.BOTTOM, padx=10, pady=10)

    # create a label for the title and add it to the frame
    title_label = tk.Label(frame, text=title, font=('Arial', 14, 'bold'), fg='black', bg='white')
    title_label.pack(padx=10, pady=5)

    # create a label for the description and add it to the frame
    desc_label = tk.Label(frame, text=description, font=('Arial', 12), fg='gray', bg='white')
    desc_label.pack(padx=10, pady=5)

    # function to destroy the window after a short delay
    def destroy_window():
        window.after(3000, window.destroy)

    window.after(3000, destroy_window)

class DeletePage(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        # screen_width = root.winfo_screenwidth()
        # screen_height = root.winfo_screenheight()
        # self.geometry(f"{screen_width//2}x{screen_height//2}+{screen_width//4}+{screen_height//4}")
        self.title("Elimina Smart Contract")
        self.name_label = tk.Label(self, text="Nome:")
        self.name_label.pack()

        self.name_entry = tk.Entry(self)
        self.name_entry.pack()

        frame = tk.Frame(self)

        self.ok_button = tk.Button(frame, text="Elimina", command=self.ok_button_click)
        self.ok_button.pack(side=tk.LEFT)

        self.cancel_button = tk.Button(frame, text="Annulla", command=self.cancel_button_click)
        self.cancel_button.pack(side=tk.LEFT)

        frame.pack()

    def ok_button_click(self):
        name = self.name_entry.get()
        write(web3_1, onChainSmartContract, "deleteContract", [name])
        show_toast('Smart Contract eliminato correttamente', '')
        self.destroy()

    def cancel_button_click(self):
        self.destroy()

root = tk.Tk()
app = Loader(root)
app.pack()
root.mainloop()


# # # -*-  coding: utf-8 -*-
# # # @Time      :2021/3/22 20:56
# # # @Author    :huangzg28153
# # # @File      :test.py
# # # @Software  :PyCharm
# # import numpy as np
# # import pandas as pd
# # # type = [0,1,1,1,2,0,1,0,1,2,2,0]
# # # ser = [0,1,2,3,4,5,6,0,1,2,3,4]
# # # layer = [0,0,0,0,0,1,1,0,0,0,0,1]
# # # sample = [0,0,0,0,0,0,0,1,1,1,1,1]
# # #
# # # df = pd.DataFrame({"type":type,"ser":ser,"layer":layer,"sample":sample})
# # #
# # #
# # # df.sort_values(by=["ser",'type',"sample","layer"],axis=0)
# # # df.sort_values(by=["layer","sample","type","ser"],axis=0)
# # # df.sort_values(by=["type","layer","sample","ser"],axis=0)
# # # df['order'] = [0,2,4,5,6,9,11,1,3,7,8,10]
# # # df = df.sort_values(by=['order'],axis=0)
# # # df.sort_values(by=['layer','ser','type','sample'],axis=0)
# # # df.sort_values(by=["sample","type",'ser',"layer"],axis=0)
# # #
# # # ########################################################
# # # df.sort_values(by=['layer',"type","sample","ser"],axis=0).reset_index().index
# # # #######################################################
# # # from multiprocess import Process,Manager
# # # from pyHGT.data import Graph, renamed_load
# # # from pyHGT.data import renamed_load
# # # import os
# # # import ray
# # # Manager().register("Graph", Graph)
# # # dir(Manager())
# # # ABSULUTE_DIR = '/data1/huangzg/research/pyHGT_OAG'
# # # graph = renamed_load(open(os.path.join(ABSULUTE_DIR + '/data/oag_output', 'graph_CS.pk'), 'rb'))
# # # func = lambda graph,inp: print(graph.__dir__())
# # #
# # # # graph = Manager().Graph(graph)
# # # ray_graph = ray.put(graph)
# # ###########################
# #
# # import oaglog
# # from pyHGT.data import renamed_load
# # from pyHGT.model import *
# # from pyHGT.SubgraphToTorch import SubgraphToTorch
# # from warnings import filterwarnings
# # filterwarnings("ignore")
# # import ray
# # import os
# # import numpy as np
# # import dill
# # from collections import defaultdict
# # import sys
# # import argparse
# # oaglog.logger.info("流程开始。。。")
# # parser = argparse.ArgumentParser(description='Training GNN on Paper-Venue (Journal) classification task')
# #
# # '''
# #     Dataset arguments
# # '''
# # parser.add_argument('--data_dir', type=str, default='/data/oag_output/',
# #                     help='The address of preprocessed graph.')
# # parser.add_argument('--subgraphs_dir',type=str,default='/data/sampled_subgraphs/',
# #                     help='The adress of sampled subgraph.')
# # parser.add_argument('--model_dir', type=str, default='/model_save/',
# #                     help='The address for storing the models and optimization results.')
# # parser.add_argument('--task_name', type=str, default='PV',
# #                     help='The name of the stored models and optimization results.')
# # parser.add_argument('--cuda', type=int, default=0,
# #                     help='Avaiable GPU ID')
# # parser.add_argument('--domain', type=str, default='_CS',
# #                     help='CS, Medicion or All: _CS or _Med or (empty)')
# # '''
# #    Model arguments
# # '''
# # parser.add_argument('--conv_name', type=str, default='hgt',
# #                     choices=['hgt', 'gcn', 'gat', 'rgcn', 'han', 'hetgnn'],
# #                     help='The name of GNN filter. By default is Heterogeneous Graph Transformer (hgt)')
# # parser.add_argument('--n_hid', type=int, default=400,
# #                     help='Number of hidden dimension')
# # parser.add_argument('--n_heads', type=int, default=8,
# #                     help='Number of attention head')
# # parser.add_argument('--n_layers', type=int, default=4,
# #                     help='Number of GNN layers')
# # parser.add_argument('--dropout', type=float, default=0.2,
# #                     help='Dropout ratio')
# # parser.add_argument('--sample_depth', type=int, default=6,
# #                     help='How many numbers to sample the graph')
# # parser.add_argument('--sample_width', type=int, default=128,
# #                     help='How many nodes to be sampled per layer per type')
# # parser.add_argument('--feature_flags', type=tuple, default=('rw','sp'),
# #                     help='which kind of distance feature to use,"random walk","shortest path" or both')
# # parser.add_argument('--max_sprw', type=tuple, default=(4, 4),
# #                     help='parameters of distance feature')
# # parser.add_argument('--if_sample_mp',type=bool, default=True,
# #                     help="whether sample subgraph with multiprocessing or not")
# # parser.add_argument('--sample_n_pool',type=int,default=16,
# #                     help="how many pools to sample subgraph")
# # '''
# #     Optimization arguments
# # '''
# # parser.add_argument('--optimizer', type=str, default='adamw',
# #                     choices=['adamw', 'adam', 'sgd', 'adagrad'],
# #                     help='optimizer to use.')
# # parser.add_argument('--data_percentage', type=float, default=1.0,
# #                     help='Percentage of training and validation data to use')
# # parser.add_argument('--n_epoch', type=int, default=200,
# #                     help='Number of epoch to run')
# # parser.add_argument('--n_pool', type=int, default=4,
# #                     help='Number of process to sample subgraph')
# # parser.add_argument('--n_batch', type=int, default=32,
# #                     help='Number of batch (sampled graphs) for each epoch')
# # parser.add_argument('--repeat', type=int, default=2,
# #                     help='How many time to train over a singe batch (reuse data)')
# # parser.add_argument('--batch_size', type=int, default=256,
# #                     help='Number of output nodes for training')
# # parser.add_argument('--clip', type=float, default=0.25,
# #                     help='Gradient Norm Clipping')
# #
# # args = parser.parse_args()
# #
# # if args.cuda != -1:
# #     device = torch.device("cuda:" + str(args.cuda))
# # else:
# #     device = torch.device("cpu")
# #
# # ABSULUTE_DIR = '/data1/huangzg/research/pyHGT_OAG'
# #
# # ###############################################data_preparing#########################################################
# # # oaglog.logger.info("读取数据开始。。。")
# # # graph = renamed_load(open(os.path.join(ABSULUTE_DIR + args.data_dir, 'graph%s.pk' % args.domain), 'rb'))
# # # oaglog.logger.info("读取数据完毕。")
# # #
# # # from ReadData import read_data, graph_to_dict
# # #
# # # dict_graph = graph_to_dict(graph)
# #
# # from multiprocess import Manager, Pool, SharedMemoryManager
# # # manager = Manager()
# # # graph_temp = manager.dict(dict_graph)
# #
# # graph = [np.ones(10**8) for i in range(20)]
# #
# # def mp_test(graph):
# #     print(id(graph))
# #     return 1
# #
# # p = Pool(6)
# #
# # result = p.apply_async(mp_test,graph_temp)
# # # @ray.remote
# # # class Counter(object):
# # #     def __init__(self,a):
# # #         self.n = 0
# # #         self.a = a
# # #     def increment(self):
# # #         self.n += 1
# # #
# # #     def read(self,b,m_graph):
# # #         print("a")
# # #         self.increment()
# # #         print(id(m_graph))
# # #         del m_graph
# # #         return self.n * b
# # #
# # # counters = [Counter.remote(a=0) for i in range(8)]
# # # futures = [c.read.remote(2, ray_graph) for c in counters]
# # #
# # # print('******************************')
# # # print(ray.get(futures))
# #
# # ray.init()
# # @ray.remote
# # def func(array, param):
# #     # print(array.job_id)
# #     # print(array.task_id)
# #     # print(array.size)
# #     # print(type(array))
# #     print(id(array))
# #     return 1
# #
# # # array = np.ones(10**6)
# # # Store the array in the shared memory object store once
# # # so it is not copied multiple times.
# # # graph =  {i: np.ones(10**8) for i in range(20)}
# # graph = [np.ones(10**8) for i in range(20)]
# # array_id = ray.put(graph)
# #
# # result_ids = [func.remote(array_id, i) for i in range(40)]
# # output = ray.get(result_ids)
# # #################################################################
# # #
# # # ray.get(ray_graph)
# # # import ray
# # # import asyncio
# # # ray.init()
# # #
# # # import asyncio
# # #
# # # @ray.remote
# # # class AsyncActor:
# # #     async def run_task(self):
# # #         print("started")
# # #         await asyncio.sleep(1) # Network, I/O task here
# # #         print("ended")
# # #
# # # actor = AsyncActor.remote()
# # # # All 50 tasks should start at once. After 1 second they should all finish.
# # # # they should finish at the same time
# # # ray.get([actor.run_task.remote() for _ in range(50)])
# # ###################################################################
# # # import ray
# # # import asyncio
# # # ray.init()
# # #
# # # @ray.remote(num_cpus=40)
# # # class AsyncActor:
# # #     # multiple invocation of this method can be running in
# # #     # the event loop at the same time
# # #     async def run_concurrent(self):
# # #         print("started")
# # #         await asyncio.sleep(2) # concurrent workload here
# # #         print("finished")
# # #
# # # actor = AsyncActor.remote()
# # #
# # # # regular ray.get
# # # ray.get([actor.run_concurrent.remote() for _ in range(80)])
# #
# # # # async ray.get
# # # await actor.run_concurrent.remote()
# #
# # ########################################################################
# from multiprocessing import Pool,Manager,shared_memory
# from multiprocessing.managers import SharedMemoryManager
# import numpy as np
#
#
# a = np.array([np.ones(10**8) for i in range(20)])
#
# shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
# b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
# b[:] = a[:]
#
# def mp_test(graph):
#     print(id(graph))
#     return 1
#
# p = Pool(6)
# results = []
# for i in range(3):
#     result = p.apply_async(mp_test, args=(b,))
#     results.append(result)
#
# re = [job.get() for job in results]
# ############################################################################
from multiprocessing import Pool
import multiprocessing as mp
from collections import defaultdict
import pandas as pd
import os

class NewClass(object):
    def __init__(self,
                 a):
        self.a = a
        self.b = {"a":a}
        self.c = pd.DataFrame(self.b)
        self.d = {"c":self.c, "b":self.b, "a":a}

    def my_method(self,e):
        print(id(self.a))
        print(id(self.b))
        print(id(self.c))
        print(id(self.d))
        print(id(e))
        defaultdict(lambda :[])
        return 1

graph = NewClass([1,3,6])

global graph

def my_fun(param,graph):

    print(os.getpid(), id(graph))

    return 1

def my_mp(param):
    my_fun(param, graph)

if __name__ == '__main__':

    p = Pool(5)
    jobs = []
    for i in range(mp.cpu_count()-1):
        job = p.apply_async(my_mp, args=(['a','b'],))
        jobs.append(job)
    result = [job.get() for job in jobs]

    print(result)


hargaBarang =  int(input("Masukkan Harga Barang: "))
uang = int(input("Masukkan Uang Anda: "))

kembalian = uang - hargaBarang

pecahan_uang = [100000, 50000, 20000, 10000, 5000, 2000, 1000]

if hargaBarang > uang:
    print("uang anda tidak cukup, dilarang utang disini!")
    exit()
for pecahan in pecahan_uang:
    jumlah_uang = kembalian // pecahan
    kembalian %= pecahan
    print(f"{jumlah_uang} uang sejumlah Rp.{pecahan} ")

#nah jumlah_uang itu adalah lembar uang yang dikembalikan nantinya


import torch
import decord
from decord import cpu, gpu
from tqdm import tqdm
import json
import os
import random
import numpy as np
import pickle

def sample_frames(num_frames, vlen, sample='rand', fix_start=None):
    acc_samples = min(num_frames, vlen)
    intervals = np.linspace(start=0, stop=vlen, num=acc_samples + 1).astype(int)
    ranges = []
    for idx, interv in enumerate(intervals[:-1]):
        ranges.append((interv, intervals[idx + 1] - 1))
    if sample == 'rand':
        frame_idxs = [random.choice(range(x[0], x[1])) for x in ranges]
    elif fix_start is not None:
        frame_idxs = [x[0] + fix_start for x in ranges]
    elif sample == 'uniform':
        frame_idxs = [(x[0] + x[1]) // 2 for x in ranges]
    else:
        raise NotImplementedError

    return frame_idxs

video_list_train = json.load(open("video_train.json"))
video_list_val = json.load(open("video_val.json"))
video_list_test = json.load(open("video_test.json"))
for video_id in tqdm(video_list_train):
    if os.path.exists(f"videos/{video_id}.mp4"):
        video_path = f"videos/{video_id}.mp4"
    else:
        video_path = f"videos/{video_id}.mkv"
    video_reader = decord.VideoReader(video_path, width=512, height=512)
    decord.bridge.set_bridge('torch')
    vlen=len(video_reader)
    for i in range(1,10):
        frame_idxs = sample_frames(3, vlen, sample="rand")
        frames = video_reader.get_batch(frame_idxs).byte()
        frames = frames.permute(0, 3, 1, 2)
        frame_ids[f"{video_id}_{i}"]=frame_idxs
        pickle.dump(frames,open(f"allinone_data/images/rand_new/{video_id}_{i}","wb"))

for video_id in tqdm(video_list_val+video_list_test):
    if os.path.exists(f"videos/{video_id}.mp4"):
        video_path = f"videos/{video_id}.mp4"
    else:
        video_path = f"videos/{video_id}.mkv"
    video_reader = decord.VideoReader(video_path, width=512, height=512, num_threads=1)
    decord.bridge.set_bridge('torch')
    vlen=len(video_reader)
    frame_idxs = sample_frames(3, vlen, sample="uniform")
    frames = video_reader.get_batch(frame_idxs).byte()
    frames = frames.permute(0, 3, 1, 2)
    pickle.dump(frames,open(f"clipbert/images/uniform/{video_id}","wb"))


'''Refazendo Progressão Aritmética com While'''
print('=-='*20)
print('Progressão Aritmética')
print('=-='*20)
primeiro = int(input('Digite o primeiro termo da progressão: '))
razao = int(input('Digite a razão para a progressão: '))
termo = primeiro
cont = 1
total = 0
mais = 10
while mais != 0:
    total = total + mais
    while cont <= total:
        print('{} → '.format(termo),end='')
        termo += razao
        cont += 1
    print('Pausa')
    mais = int(input('Quantos termos você quer mostrar a mais? '))
print('Progressão finalizada com {} termos mostrados.'.format(total))


# The file is object for VICON. This is effective method for storing and
# accesing information about the vicon objects

import numpy as np
import os
from VICONMath import transformations as transferOp
from VICONFileOperations import rwOperations

class ObjectVicon:
    """The class is object is base class for VICONTrackingObjects and VICONCameraObjects"""
    def __init__(self, rotation = [0, 0, 0, 1], translation = [0,0,0], name = "temp"):
        """
        Initialize the class, the position orientation and name of object
        :param rotation: list of roation parameters
        :param translation: list of translation parameters
        :param name: name of the object
        """
        self.rotation = rotation # list of parameters
        self.translation = translation # list of parameters
        self.name = name
        self.featureMat = np.zeros((3,1))
        self.featurePoints = []
        self.featureList = []

        self.featureDict = {}

    def clearFeatures(self):
        """
        Clear all features
        :return: True
        """
        self.featureDict = {}

    def setTransformationParameters(self, rotation, translation ):

        self.rotation = rotation
        self.translation = translation

    def transferFeaturesToViconSpace(self) :
        """
        Transfer the features from Object space to target space
        :return: dict (3D features)
        """
        assert (len(self.featureDict) != 0), "Dicitonary empty!! No object features to transfer"
        featureList = list(self.featureDict.values())
        targetPoints = transferOp.transformPoint3D(featureList, self.rotation, self.translation)
        targetPoints = targetPoints.tolist()
        transferedFeatureDict = {}
        for feature,index in zip(self.featureDict,range(len(targetPoints)) ):
            transferedFeatureDict[feature] = targetPoints[index]

        return transferedFeatureDict

    def transferFeaturesToObjectSpace(self, featureDictViconSpace):
        """
        Transfer feature points from given coordinate system to object space
        :return: dictionary of features
        """
        assert (len(featureDictViconSpace) != 0), "Given featurelist is empty"

        featureList = list(featureDictViconSpace.values())
        targetPoints = transferOp.transformPoint3D(featureList, self.rotation, self.translation, True)
        targetPoints = targetPoints.tolist()
        transferedFeatureDict = {}
        for feature, index in zip(featureDictViconSpace, range(len(targetPoints)) ):
            transferedFeatureDict[feature] = targetPoints[index]

        return transferedFeatureDict

    def transferPointsToObjectSpace(self, pointList):
        """
        Transfers the list points from given space to Object space using given R and T
        :return:
        """
        invertedPoints = transferOp.transformPoint3D(pointList, self.rotation , self.translation, True) # R_inv . (P-T)
        return invertedPoints

    def transferPointsFromObjectSpace(self, pointList):
        """
        Transfers given list of points from object space to target space using given R and T
        :return:
        """
        targetPoints = transferOp.transformPoint3D(pointList,self.rotation,self.translation)
        return targetPoints

    def setFeatures(self, featuresDict):
        """
        Set the given features in the feature list
        :param  set the features in the dict using given dict
        :return: True/False
        """
        for feature in featuresDict:
            self.featureDict[feature] = featuresDict[feature]

        return True

    def removeSelectedFeatures(self, features):
        """
        Remove the given features
        :param featureDict: dict
        :return: bool
        """
        for feature in features:
            if feature in self.featureDict:
                del self.featureDict[feature]

        return True

    def filterFeaturesBasedOnObject(self, dict):
        """
        Filter the feature dict to only have features from a specific object
        :return: dict
        """
        modifiedDict = {}
        for feature in dict :
            if self.name in feature:
                modifiedDict[feature] = dict[feature]

        return modifiedDict

    def readFeaturePointsFromFile(self, path, objectFilter = True):
        """
        Reads given *.mp (VICON) file to create feature points for Object
        :param path: Storage path of the
        :return: True if success
        """
        if os.path.exists(path) and ".mp" in path:
            featureDict = rwOperations.readFeaturePointsFromViconFile(path)
            self.setFeatures(featureDict)
            return True

        elif os.path.exists(path) and ".txt" in path:
            featureDict = rwOperations.readFeaturePointsFromTextFile(path)
            if objectFilter:
                objectSpecificDict = self.filterFeaturesBasedOnObject(featureDict)

            self.setFeatures(objectSpecificDict)
            return True

        else:
            raise ValueError("Given file does not exist ", path)

if __name__ == '__main__':

    rotation = [0 , 0 , 0 , 1] # Identity
    translation = [0 , 0 , 0 ] # Translation

    # Test creating points from file
    filePath = "D:/BirdTrackingProject/VICON_DataRead/VICONDrawingOperations\\9mm_02.mp"
    objectName = "9mm_02"
    viconObject = ObjectVicon(rotation, translation, objectName)
    viconObject.readFeaturePointsFromFile(filePath)
    viconObject.name = "point"
    viconObject.readFeaturePointsFromFile("D:\BirdTrackingProject\VICON_DataRead\VICONFileOperations\\temp.txt")
    print("Print features: ", viconObject.featureDict)
    # Test creating points without file
    pointlist = list(viconObject.featureDict.values())

    rotationCam1 = [-0.79697172135980876, 0.009835241003934278, 0.056076936965809787, 0.60132746530298287]
    translationCam1 = [-701.064504485933, -6173.2248621199, 1830.24808693825]

    cam1Points = [[1415.76135949, - 570.12878565, 7724.00698054],
                           [1427.61481598, - 588.07764951, 7708.65018457],
                           [1433.03420332, - 579.43874105, 7728.08209005],
                           [1439.77318296, - 598.69765411, 7698.82965911]]

    viconObject.setTransformationParameters(rotationCam1,translationCam1)

    # transfer points to object space
    transferedPoints = viconObject.transferPointsFromObjectSpace(cam1Points)
    print("TF Points:", transferedPoints)
    reversedPoints = viconObject.transferPointsToObjectSpace(transferedPoints)
    print("Reversed points:", reversedPoints)

    transferedDict = viconObject.transferFeaturesToViconSpace()
    print("TF Points:", transferedDict)
    reversedDict = viconObject.transferFeaturesToObjectSpace(transferedDict)
    print("Reversed points:", reversedDict)

    # Test adding feature and clear the features
    p = {'1': [0, 0, 0]}
    viconObject.setFeatures(p)
    print("Print added features: ", viconObject.featureDict)

    viconObject.removeSelectedFeatures(p)
    print("Print removed features: ", viconObject.featureDict)

    viconObject.clearFeatures()
    print("Print features after clearance: ", viconObject.featureDict)


from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, Float, UniqueConstraint, Index
from sqlalchemy.orm import relationship

from src.models import Base

class GpsRecord(Base):

    __tablename__ = "gps_record"

    id = Column(Integer, primary_key=True, index=True)
    datetime = Column(DateTime(timezone=True))
    latitude = Column(Float())
    longitude = Column(Float())
    altitude = Column(Float(), nullable=True)
    accuracy = Column(Float(), nullable=True)
    vertical_accuracy = Column(Float(), nullable=True)
    description = Column(String, nullable=True)
    device = Column(String(length=128))
    app = Column(String(length=128))
    user = Column(String(length=32), default='castel')
    distance = Column(Float())

Index('device_records', GpsRecord.datetime, GpsRecord.device, GpsRecord.app, GpsRecord.user, unique=True)
Index('desc_date_per_app', GpsRecord.datetime.desc(), GpsRecord.device, GpsRecord.app)
Index('per_app', GpsRecord.device, GpsRecord.app)
Index('desc_date_per_device', GpsRecord.datetime.desc(), GpsRecord.device)
Index('desc_date', GpsRecord.datetime.desc())


"""
Class to deal with the pooling problem (differing amounts of tweets for various days)
"""

import torch
from torch import nn, tensor

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#device = torch.device("cpu")
# will we have a device setting here? to ensure that the data is being processed? (why is this bad practice)

class pooling(nn.Module):
    """A pooling class, so that the forward pass in this variation of the teanet model is lest complex
        Also, perhaps it can be trained separately?
    """
    def __init__(self, dim, lag):
        super(pooling, self).__init__()
        self.dim = dim
        self.lag = lag
        # multiple pooling layers? A feed forward neural network?
        self.adaptive_pooling = nn.AdaptiveMaxPool2d((1, dim))

    def forward(self, input):
        batch_of_tweets = None
        for x_val in input[0]:
            processed_tweets = None
            # iterate through the days in the x_value
            for day in x_val:
                processed = self.adaptive_pooling(day.view(1, day.shape[0], day.shape[1]))
                if(processed_tweets == None):
                    processed_tweets = processed
                else:
                    processed_tweets = torch.cat((processed_tweets, processed), 1)
            if(batch_of_tweets == None):
                batch_of_tweets = processed_tweets.view(1, self.lag, self.dim)
            else:
                batch_of_tweets = torch.cat((batch_of_tweets, processed_tweets.view(1, self.lag, self.dim)), 0)
        return batch_of_tweets.to(device)


from __future__ import print_function
import unittest
import numpy
import irbasis

from irbasis_util.two_point_basis import *
from irbasis_util.four_point import *
from irbasis_util.internal import *
from irbasis_util.regression import *

from atomic_limit import *

def G1_iw_pole_f(n, pole, beta):
    return 1/(1J * (2 * n + 1) * numpy.pi / beta - pole)

def G1_iw_pole_b(n, pole, beta):
    return 1/(1J * (2 * n) * numpy.pi / beta - pole)

def _compute_G4pt_iw(beta, pole, r, nvec):
    if r == 0:
        return G1_iw_pole_f(nvec[0], pole, beta) * G1_iw_pole_f(nvec[1], pole, beta) * G1_iw_pole_f(nvec[2], pole, beta)
    elif r == 1:
        return G1_iw_pole_f(nvec[0], pole, beta) * G1_iw_pole_f(nvec[1], pole, beta) * G1_iw_pole_f(nvec[3], pole, beta)
    elif r == 4:
        return G1_iw_pole_f(nvec[0], pole, beta) * G1_iw_pole_b(nvec[0] + nvec[1] + 1, pole, beta)\
               * G1_iw_pole_f(-nvec[3] - 1, pole, beta)
    elif r == 5:
        return G1_iw_pole_f(nvec[0], pole, beta) * G1_iw_pole_b(nvec[0] + nvec[1] + 1, pole, beta) \
               * G1_iw_pole_f(-nvec[2] - 1, pole, beta)
    else:
        raise RuntimeError("Not supported")

def _outer_product(Gl1, Gl2, Gl3):
    tensor12 =  Gl1[:, numpy.newaxis] * Gl2[numpy.newaxis, :]
    return tensor12[:, :, numpy.newaxis] * Gl3[numpy.newaxis, numpy.newaxis, :]

def _compute_G4pt_l(b4pt, pole, r):
    Nl = b4pt.Nl
    Gl = numpy.zeros((16, Nl, Nl, Nl))
    if r >= 0  and r <= 3:
        Gl[r, :, :, :] = _outer_product(
            Gl_pole(b4pt.basis_beta_f, pole)[:Nl],
            Gl_pole(b4pt.basis_beta_f, pole)[:Nl],
            Gl_pole(b4pt.basis_beta_f, pole)[:Nl])
    elif r <= 15:
        Gl[r, :, :, :] = _outer_product(
            Gl_pole(b4pt.basis_beta_f, pole)[:Nl],
            Gl_pole(b4pt.basis_beta_b, pole)[:Nl],
            Gl_pole(b4pt.basis_beta_f, pole)[:Nl])
    else:
        raise RuntimeError("Not supported")

    return Gl

class TestMethods(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestMethods, self).__init__(*args, **kwargs)

    def test_matsubara(self):
        Lambda = 10.0
        beta = 0.2
        wmax = Lambda/beta
        b4pt = FourPoint(Lambda, beta, 1e-12)
        Nl = b4pt.Nl

        pole = 0.2 * wmax

        n1, n2, n3 = 1, 2, 4
        n4 = - n1 - n2 - n3 - 2

        prj = b4pt.projector_to_matsubara(n1, n2, n3, n4)

        # Test No. 1, 2, 5, 6
        for r in [0, 1, 4, 5]:
            giw_4pt = _compute_G4pt_iw(beta, pole, r, numpy.array([n1, n2, n3, n4]))
            gl_4pt = _compute_G4pt_l(b4pt, pole, r)

            diff = numpy.abs(numpy.sum(prj * gl_4pt) - giw_4pt)
            self.assertLess(diff, 1e-10)

    def test_sampling_points_matsubara(self):
        Lambda = 10.0
        beta = 0.2
        alpha = 1e-15
        augmented = True
        wmax = Lambda / beta
        b4pt = FourPoint(Lambda, beta, 1e-2, augmented)
        Nl = b4pt.Nl
        whichl = Nl - 1
        pole = 0.2 * wmax
        # build the sampling frequency structure
        sp = b4pt.sampling_points_matsubara(whichl)
        S = b4pt.normalized_S()
        n_sp = len(sp)
        prj = numpy.array(b4pt.projector_to_matsubara_vec(sp))[:, :,  :, :]
        prj_mat = prj[:, :, :, :].reshape((n_sp, 16 * Nl**3))
        print(Nl, prj_mat.shape)
        # Build the check frequency structure
        n1234_check = []
        niw = 100
        for i, j, k in product(range(-niw, niw, 10), repeat=3):
            n1234_check.append((i, j, k, - i - j - k - 2))
        prj_check = numpy.array(b4pt.projector_to_matsubara_vec(n1234_check))[:, :, :, :]
        # Test No. 1, 2, 5, 6
        for r in [0, 1, 4, 5]:
            Giwn = numpy.array([ _compute_G4pt_iw(beta, pole, r, n1234) for n1234 in sp])
            Giwn_check_ref = numpy.array([_compute_G4pt_iw(beta, pole, r, n1234) for n1234 in n1234_check])
            coeffs = ridge_complex(prj_mat, Giwn, alpha).reshape((16, Nl, Nl, Nl))
            Giwn_check = numpy.dot(prj_check.reshape((len(n1234_check), 16 * Nl**3)), (coeffs).reshape((16 * Nl**3)))
            self.assertLessEqual(numpy.amax(numpy.abs(Giwn_check - Giwn_check_ref)), 1e-2)

    def test_transformation_to_PH(self):
        self.assertEqual(to_PH_convention(from_PH_convention( (0,1,2) )),  (0,1,2))

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


# -*- coding: utf-8 -*-
import re
import sqlite3
from collections import defaultdict

import requests
import gnupg

class OTCDB(object):
	gpg_file = 'GPG.db'
	rating_file = 'RatingSystem.db'

	def __init__(self, path):
		self.path = path
		self.trusted = {}

	def open_db(self):
		gpg_path = '{0}/{1}'.format(self.path, self.gpg_file)
		self.gdb = sqlite3.connect(gpg_path, check_same_thread=False)
		self.gdb.row_factory = sqlite3.Row
		rating_path = '{0}/{1}'.format(self.path, self.rating_file)
		self.rdb = sqlite3.connect(rating_path, check_same_thread=False)
		self.rdb.row_factory = sqlite3.Row

	def close_db(self):
		self.gdb.close()
		self.rdb.close()

	def update_db(self):
		g = self.update_rating_db()
		r = self.update_gpg_db()

	def update_rating_db(self):
		filename = '{0}/{1}'.format(self.path, self.rating_file)
		url = 'http://bitcoin-otc.com/otc/RatingSystem.db'
		r = requests.get(url, stream=True)
		if r.status_code == 200:
			with open(filename, 'wb') as f:
				for chunk in r.iter_content(10*1024):
					f.write(chunk)
				return True
		else:
			return False

	def update_gpg_db(self):
		filename = '{0}/{1}'.format(self.path, self.gpg_file)
		url = 'http://bitcoin-otc.com/otc/GPG.db'
		r = requests.get(url, stream=True)
		if r.status_code == 200:
			with open(filename, 'wb') as f:
				for chunk in r.iter_content(10*1024):
					f.write(chunk)
				return True
		else:
			return False

	def update_trust(self):
		self.update_db()

		self.open_db()
		new_trust = self.assbot_trust()
		self.close_db()

		return new_trust

	def trust_diff(self, old, new):
		new_keys = set(new.keys())
		old_keys = set(old.keys())
		added_keys = list(new_keys - old_keys)
		removed_keys = list(old_keys - new_keys)
		# add metadata in removed list before we throw it away
		removed_keys = [(rk, old[rk][0]) for rk in removed_keys]
		return (added_keys, removed_keys)

	def assbot_trust(self):
		assbot_ratings = defaultdict(int)
		trusted = {}

		sel = """SELECT nick, rated_user_id, rater_user_id, rating FROM ratings
			 JOIN users ON ratings.rated_user_id = users.id
			 WHERE rater_user_id = 5506 OR rater_user_id IN (SELECT rated_user_id FROM ratings WHERE rater_user_id = 5506)
			"""
		cursor = self.rdb.cursor()
		cursor.execute(sel)
		results = cursor.fetchall()

		for row in results:
			add = 1 if row['rating'] > 0 else -1
			assbot_ratings[ row['nick'] ] += add

		selkey = 'SELECT fingerprint FROM users WHERE lower(nick) = ? AND fingerprint IS NOT NULL'
		gcursor = self.gdb.cursor()

		for nick in assbot_ratings:
			if  assbot_ratings[nick] > 0:
				row = gcursor.execute(selkey, (nick.lower(),)).fetchone()
				if row:
					trusted[ row['fingerprint'] ] = (nick, assbot_ratings[nick])
		return trusted

## make gpg pubring
class GPGManager(object):

	def __init__(self, gpghome, keyserver=None):
		self.gpghome = gpghome
		self.keyserver = keyserver if keyserver else 'hkp://pool.sks-keyservers.net'

		self.notfound = re.compile('key ([^ ]+) not found on keyserver')

		self.gpg = gnupg.GPG(homedir=gpghome)
		self.gpg.keyserver = self.keyserver

	def recv_keys(self, fingerprints, batch=10):
		not_found = []
		imported = 0
		for chunk in chunks(fingerprints, batch):
			r = self.gpg.recv_keys(*chunk)

			missing = self.notfound.findall(r.data)
			for k in missing:
				if k in chunk: not_found.append(k)

			imported += r.counts['imported']

		return (imported, not_found)

	def verify(self, data):
		return self.gpg.verify(data)

	def delete_keys(self, fingerprints):
		return self.gpg.delete_keys(fingerprints)

def chunks(l, n):
	""" Yield successive n-sized chunks from l."""
	for i in xrange(0, len(l), n):
		yield l[i:i+n]


'''
                                        颜色特征向量
                                            ||
                          （赋权颜色直方向量   拼接   赋权颜色矩向量）
                                 //                     \\
                      归一化颜色直方向量                 归一化颜色矩向量
                             //                             \\
                      颜色直方向量√                        颜色矩向量
                                    这里获取颜色直方向量
'''
import tensorflow as tf

'''
    下面涉及到的3600，只是假设有3600，图片张数可以变
'''
def getColorStraight(rawImagesList):
    picsStraightR = []  # 3600张图片的颜色直方向量R分量 3600X86
    picsStraightG = []  # 3600张图片的颜色直方向量G分量 3600X86
    picsStraightB = []  # 3600张图片的颜色直方向量B分量 3600X86
    colorStraightList = []  # 颜色直方向量3600X258，3600张图片，每张图片有258个颜色直方特征
    picsStraightRGBList = [picsStraightR, picsStraightG, picsStraightB]  # 为下面放picsStraightR/G/B值做准备
    for i in range(3):
        # 3600张图片一次性拿出R/G/B分量，按照专利，选取图片中心的112X112部分作为主要图片
        majorImagesList = rawImagesList[:, 56:168, 56:168, i]

        for j in range(86):
            # tempListForRGB=[]#存储计算R/G/B各自分量时的数据，3600X86
            if j == 0:
                # 先统计有多少个0
                totoalZero = 0
                majorImagesListCmpWithZero = tf.math.equal(majorImagesList, 0)  # 看看每个数是否为0,真的为T
                out = tf.cast(majorImagesListCmpWithZero, dtype=tf.float32)  # True的时候会变为1，False的时候会变为0

                # 统计0的个数
                '''
                    下面两行代码意思: 上面的out是一个shape为(3600,112,112)3600张图片，宽高112X112，
                    而一张图片的shape为（1,112,112）所以要单独统计3600张图片中 的0的个数
                    使得最后的totoalZero 的shape为(3600,)，3600个数，每个数代表每张图片0的个数
                    再将其添加到tempListForRGB
                    而最后picsStraightR/G/B 先得到的是 86X3600的矩阵,可以之后再转置
                '''
                totoalZero = tf.reduce_sum(out, axis = 1)
                totoalZero = tf.reduce_sum(totoalZero, axis = 1)

                picsStraightRGBList[i].append(totoalZero)  # 将0的个数放到列表的第一个位置

            else:  # 统计非0的个数
                '''
                根据专利，将0-255划分为85个区间，也就是每3个数为一个区间，在端点处的值归到其左侧的区间
                每个数都必须 j<x<=j+2  例如    1<x<=3
                但是当j为1的时候，由于1的左边是0，没有左侧区间所以当J为1的时候
                将1统计到其右侧区间
                '''
                if j == 1:
                    # 当j是1的时候将其放到右侧区间也就是[1-3]这个区间
                    majorImagesListGT = tf.math.greater_equal(majorImagesList, (j * 3) - 2)
                else:
                    # J不是1的时候取开区间
                    majorImagesListGT = tf.math.greater(majorImagesList, (j * 3) - 2)
                # 由于要将端点值放到其左侧区间，也就是说 [1-3],[4,6],当端点值为4的时候，它是归到[1-3]区间的以此类推
                majorImagesListLE = tf.math.less(majorImagesList, (j * 3) + 1)
                # 还没找到能够一次性提取语句  比如    x>i&& x<=i*3 这种提取方法现在想到的是将他们各自求出来然后再找共同部分
                # 经过比较之后的majorImagesListGT，majorImagesListLE，里面的元素是bool类型，只需要两部分均为ture
                majorImagesGTAndLE = tf.math.equal(majorImagesListGT, majorImagesListLE)
                # 转为float
                out = tf.cast(majorImagesGTAndLE, dtype=tf.float32)
                # 统计  j-2 < x <=j*3+1 的个数
                sumMajorImagesGTAndLE = tf.reduce_sum(out, axis=1)
                sumMajorImagesGTAndLE = tf.reduce_sum(sumMajorImagesGTAndLE, axis=1)
                picsStraightRGBList[i].append(sumMajorImagesGTAndLE)  # 将统计到的数据放进去

    picsStraightR = tf.convert_to_tensor(picsStraightRGBList[0])#得到 86X3600
    picsStraightR = tf.transpose(picsStraightR)#转置变为3600x86
    picsStraightG = tf.convert_to_tensor(picsStraightRGBList[1])
    picsStraightG = tf.transpose(picsStraightG)
    picsStraightB = tf.convert_to_tensor(picsStraightRGBList[2])
    picsStraightB = tf.transpose(picsStraightB)
    #3个3600x86拼接起来得到3600X258这就是颜色直方向量
    colorStraightList = tf.concat([picsStraightR,picsStraightG,picsStraightB],axis=1)
    return colorStraightList


# given two strings A and B, write a function to return a list of
# all start indices within A where the substring of A is an
# anagram of B. for example, if A = "abcdcbac" and B = "abc" then
# you want to return [0,4,5] since those are the starting indices
# of substrings of A that are anagrams of B

A = 'abcdcbac'
B = 'abc'

B_hash = {}
for n in B:
    B_hash.update({n:True})

indices = []
for i,m in enumerate(A):
   if i<=(len(A)-len(B)):
       curr_window = A[i:i+len(B)]
       A_count = {}
       for n in curr_window:
           if B_hash.get(n)==True:
               if A_count.get(n)==None:
                   A_count.update({n:1})
               else:
                   A_count.update({n:A_count[n]+1})
           else:
               A_count = {}
       if len(A_count)==3:
           indices.append(i)

print(indices)


import cv2
import imutils
import numpy as np

cv2.namedWindow("MyImage")

img = cv2.imread("img.jpg")

# translated = imutils.translate(img, 25, -75)
# rotated = imutils.rotate(img, 45)
img = imutils.resize(img, width=600)
# url_images = imutils.url_to_image(
#     "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png")

kernel = np.ones((4, 4))
# conv = cv2.filter2D(img, cv2.CV_16U, kernel)
blur = cv2.GaussianBlur(img, (5, 5), 1)

cv2.imshow("MyImage", blur)
cv2.waitKey(0)
cv2.destroyAllWindows()


from flask import Flask, render_template, request, redirect, session, flash
from mysqlconnection import connectToMySQL
import json

app = Flask(__name__)

@app.route("/")
def index():
    mysql = connectToMySQL("leads_and_clients_db")
    query = "SELECT concat(clients.first_name, ' ', clients.last_name) as name, count(leads.client_id) as leads FROM clients JOIN leads ON leads.client_id = clients.id GROUP BY leads.client_id"
    all_leads = mysql.query_db(query)
    print(all_leads)
    total_leads = 0
    for each in all_leads:
        total_leads += int(each['leads'])
    print("*************************total number of leads**********************",total_leads)
    percentage = int(all_leads[0]["leads"])/total_leads
    print(round(percentage, 3))
    return render_template("index.html", template_all_leads = all_leads, template_total_leads = total_leads, json_leads = map(json.dumps, all_leads))

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