# Author: Swadhin Agrawal
# E-mail: swadhin12a@gmail.com

from random import uniform
import numpy as np
# from multiprocessing import Pool
from multiprocessing.pool import ThreadPool as Pool
import matplotlib.pyplot as plt
import itertools
import random_number_generator as rng
import pandas as pd
import Object as obj
import DecisionMaking as DM
import Params as params
from math import sqrt,exp
import ParamSet as ps
from numba.typed import List
import os
from functools import partial
import copy
from numba import guvectorize, float64
from scipy.special import lambertw
import time
def parallel(func,inputs):
	batch_size = len(inputs)
	inps = [(i,) for i in inputs]
	output = []
	for i in range(0,len(inputs),batch_size):
		opt_var = []
		with Pool(20) as processor:#,ray_address="auto") as p:
			opt_var = processor.starmap(func,inps[i:i+batch_size])

		output += list(opt_var)
	return output

def option_assignment(num_opts,num_robo):
    opts = np.arange(1,num_opts+1)
    assig = np.copy(opts)
    for i in range(int(num_robo/num_opts)):
        assig = np.concatenate((assig,np.copy(opts)))
    np.random.shuffle(assig)
    return assig

def deciding_phase(p,robots,time_counter):
    options = parallel(partial(obj.Object,o_type = 'O', p = p),range(p.num_opts))

    opt_assignment =  option_assignment(num_opts = p.num_opts, num_robo = p.num_opts*p.mu_m_1)
    for r in robots[:p.num_opts*p.mu_m_1]:
        r.assigned_opt = opt_assignment[r.id]

    decision = DM.DecisionMaking(robots[:p.num_opts*p.mu_m_1],options,p,time_counter,p.memory_length,p.delta_h)

    matching = decision.compare_with_best(options)

    return decision, options

def nullifier(i,robotss,run):
    robotss[i].response = 0	# 0 = "No", 1 = "Yes"
    robotss[i].opt = -1 #self.response*self.assigned_opt
    robotss[i].best_opt = None
    robotss[i].threshold_update = 0

def param_change(i,p,robotss):
    robotss[i].p = p

def PDF(pdf_func,mu,sigma,bins):
    pdf =  pdf_func(bins,mu,sigma)                          #   PDF function values at each base values (array)
    normalized_pdf = pdf/(np.sum(pdf)*(bins[1]-bins[0]))
    ar = np.sum(pdf)*(bins[1]-bins[0])                #   Normalized PDF
    return normalized_pdf,ar

class Prediction:
    @staticmethod
    @guvectorize([(float64[:], float64[:], float64[:],float64[:])], '(n),(m),(m)->(n)')
    def gaussian(x,mu,sigma,result):
        '''
        Inputs: base, list of means, list of variance (in case of bimodal distributions)
        Output: list of Normal PDF values
        '''
        n = x.shape[0]
        m = mu.shape[0]
        for j in range(x.shape[0]):         #   For all base values we need the PDF values
            f = 0.0
            for i in range(len(mu)):        #   We calculate the PDF value due to each modal and superimpose them to
                #   get PDF of bimodal distribution (We can later also make it weighted by adding weights list for each peak)
                k = 1/(sqrt(2*np.pi)*sigma[i])
                f += k*exp(-((x[j]-mu[i])**2)/(2*(sigma[i]**2)))
            result[j] = f

    @staticmethod
    @guvectorize([(float64[:], float64[:], float64[:],float64[:])], '(n),(m),(m)->(n)')
    def uniform(x,mu,sigma,result):
        '''
        Inputs: base, list of means, list of variance (in case of bimodal distributions)
        Output: list of Uniform PDF values
        '''
        n = x.shape[0]
        m = mu.shape[0]
        for j in range(n):      #   For all base values we need the PDF values
            f = 0.0
            for i in range(m):  #   We calculate the PDF value due to each modal and superimpose them to
                #   get PDF of bimodal distribution (We can later also make it weighted by adding weights list for each peak)
                a = (mu[i] - np.sqrt(3)*sigma[i])   #   Lower bound of PDF
                b = (mu[i] + np.sqrt(3)*sigma[i])   #   Upper bound of PDF
                if x[j]<=b and x[j]>=a:
                    f += 1/abs(b-a)
            result[j] = f

    @staticmethod
    def ICPDF(area,mu,step,x,pdf):
        '''
        Inputs: Area where we need base of PDF, list of mean of PDF, resolution of PDF, base of PDF, PDF function values
        Output: The base where cummulative area under the PDF is as desired
        '''
        if len(mu)>1:                           #   If the PDF is bimodal
            dummy_area = 0.5                    #   We make a dummy area variable to start at a known point closest to the area where we need the base
            x_ = (mu[0]+mu[1])/2.0              #   The base where dummy area under the PDF is true is the mean of means
        else:
            dummy_area =0.5
            x_ = mu[0]                          #   If the PDF is unimodal, the 50% area is at the mean of PDF

        count = np.argmin(np.abs(x-x_))         #   We find the index of element in our base list closest to the known base point for dummy area

        while abs(dummy_area-area)>0.001:       #   While the distance between dummy area and the desired area is not small enough, we keep searching the point
            if dummy_area>area:                 #   If desired area is smaller than the dummy
                count -= 1                      #   We decrease the index count in base list
                dummy_area -= pdf[count]*step   #   Hence, decrease the area under the PDF accordingly
                x_ -= step                      #   Also, move the search point location towards left
            elif area>dummy_area:               #   Otherwise, if desired area > dummy
                count += 1                      #   We increment the index of base list
                dummy_area += pdf[count]*step   #   Hence, add area to existing dummy
                x_ += step                      #   Also, move the search point rightward
        return x_

def looper(p,vP):
    # robots = parallel(partial(obj.Object,o_type = 'R', p = p),range(p.num_robots))
    prd = Prediction()
    num_opt = [47, 59, 88,  9, 13]
    dist = [2, 0, 1, 2, 0]
    for run in range(len(num_opt)):
        # Update world
        p.Dx = vP.Dx[dist[run]]
        p.mu_x_1 = (p.mu_x_2 + p.mu_x_1)/2 + 1 #+ np.random.randint(2)*(-2)
        if p.Dx == 'G' or p.Dx == 'U':
            p.mu_x_2 = p.mu_x_1
        else:
            p.mu_x_2 = p.mu_x_1 + 5
        p.sigma_x_1 = 1
        p.sigma_x_2 = 1
        p.num_opts = num_opt[run]#np.random.randint(101)
        mean_x = List([p.mu_x_1,p.mu_x_2])
        sigma_x = List([1,1])
        step = 0.0001
        stop = np.sum(mean_x)/len(mean_x) + 2*np.sum(sigma_x)+5
        start = np.sum(mean_x)/len(mean_x) - 2*np.sum(sigma_x)-5
        x = np.arange(start,stop,0.0001)
        if p.Dx == 'G':
            pdf_x,area = PDF(prd.gaussian,mean_x,sigma_x,x)
            sig_h = (0.07*np.log10(p.num_opts)+0.57)*1
            mu_h = prd.ICPDF(1-(1/p.num_opts),mean_x,step,x,pdf_x)

        elif p.Dx == 'K':
            pdf_x,area = PDF(prd.gaussian,mean_x,sigma_x,x)
            sig_h = (0.05*np.log10(p.num_opts)+0.58)*1
            mu_h = prd.ICPDF(1-(1/p.num_opts),mean_x,step,x,pdf_x)

        elif p.Dx == 'U':
            pdf_x,area = PDF(prd.uniform,mean_x,sigma_x,x)
            sig_h = (-0.07*np.log10(p.num_opts)+0.67)*1
            mu_h = prd.ICPDF(1-(1/p.num_opts),mean_x,step,x,pdf_x)

        p.mu_h_1 = mu_h
        p.mu_h_2 = mu_h
        p.sigma_h_1 = sig_h
        p.sigma_h_2 = sig_h
        p.mu_m_1 = int(p.num_robots/p.num_opts)
        p.mu_m_2 = int(p.num_robots/p.num_opts)
        p.h_distribution_fn = rng.threshold_n
        p.packaging(vP)
        p.data_columns_name = p.data_columns_name[:6]
        p.add_columns(p.num_robots)
        p.pre = p.prefix(vP.results_path)
        open(vP.results_path+str(p.pre),'w+')
        p.save_data(vP.results_path)
        time.sleep(5)
        robots = parallel(partial(obj.Object,o_type = 'R', p = p),range(p.num_robots))
        data = {'$\mu_{h_1}$':np.round(p.mu_h_1,decimals=3),'$\sigma_{h_1}$':np.round(p.sigma_h_1,decimals=3),'$x_{max}$ opt No.':None,'$x_{max}$':None,'$CDM$ opt No.':None,'$CDM$':None}
        data['Dq'] = p.Dx
        data['$\mu_{q_1}$'] = p.mu_x_1
        data['$\mu_{q_2}$'] = p.mu_x_2
        data['$\sigma_{q_1}$'] = p.sigma_x_1
        data['$\sigma_{q_2}$'] = p.sigma_x_2
        data['n'] = p.num_opts

        for i in range(len(robots[:p.num_opts*p.mu_m_1])):
            data['h_'+str(i)] = np.round(robots[i].threshold,decimals=3)
            data['tr_'+str(i)] = np.round(robots[i].t_r,decimals=3)
            data['Response_'+str(i)] = robots[i].response
            data['rt_step'+str(i)] = robots[i].delta_t_r
        out = pd.DataFrame(data=[data],columns=p.data_columns_name)
        out.to_csv(p.data_f_path,mode = 'a',header = False, index=False)
        for repeat in range(100):
            parallel(partial(nullifier,robotss=robots,run=run),range(p.num_robots))
            decision,options = deciding_phase(p,robots[:p.num_opts*p.mu_m_1],1)

            data = {'$\mu_{h_1}$':np.round(p.mu_h_1,decimals=3),'$\sigma_{h_1}$':np.round(p.sigma_h_1,decimals=3),'$x_{max}$ opt No.':decision.ref_best,'$x_{max}$':np.round(options[decision.ref_best].quality,decimals=3),'$CDM$ opt No.':decision.best_option,'$CDM$':np.round(options[decision.best_option].quality,decimals=3)}
            out = pd.DataFrame(data=[data],columns=p.data_columns_name)
            out.to_csv(p.data_f_path,mode = 'a',header = False, index=False)

def paramObjects(fixed_params,vP_,data_columns,path,fixed_params_column):
    p = params.Params(data_columns,path)
    p.initializer(vP_,n = fixed_params[0]['n'], Dm = fixed_params[0]['$D_{m}$'], mum1 = fixed_params[0]['$\mu_{m_{1}}$'],\
        mum2 = fixed_params[0]['$\mu_{m_{2}}$'], sm1 = fixed_params[0]['$\sigma_{m_{1}}$'], sm2 = fixed_params[0]['$\sigma_{m_{2}}$'],\
        dmum = vP_.delta_mu_m, Dx = fixed_params[0]['$D_{x}$'], mux1 = fixed_params[0]['$\mu_{x_{1}}$'],\
        mux2 = fixed_params[0]['$\mu_{x_{2}}$'], sx1 = fixed_params[0]['$\sigma_{x_{1}}$'], sx2 = fixed_params[0]['$\sigma_{x_{2}}$'],\
        dmux = vP_.delta_mu_x, Dh = fixed_params[0]['$D_{h}$'], muh1 = fixed_params[0]['$\mu_{h_{1}}$'],\
        muh2 = fixed_params[0]['$\mu_{h_{2}}$'], sh1 = fixed_params[0]['$\sigma_{h_{1}}$'], sh2 = fixed_params[0]['$\sigma_{h_{2}}$'],\
        dmuh = fixed_params[0]['$\delta_{\mu_{h}}$'], delta_tr = fixed_params[0]['delta_tr'])

    p.packaging(vP_)
    p.add_columns(p.num_robots)
    p.pre = p.prefix(path)
    p.save_params(fixed_params_column,fixed_params,path)
    p.save_data(path)
    return p

if __name__=="__main__":
    path = os.path.realpath(os.path.dirname(__file__)) + "/results/"

    data_columns = ['$\mu_{h_1}$','$\sigma_{h_1}$','$x_{max}$ opt No.','$x_{max}$','$CDM$ opt No.','$CDM$']

    packed_parameters = []
    vP = ps.variableParams()
    gaussianM = 1
    uniformM = 0
    # for delt_tr in np.arange(0.001,0.21,0.01):
    if gaussianM:
        # for n in vP.num_opts:
        n = 10
        delt_tr = 1/(n*n)
        fixed_params_column = ['n','$D_{m}$','$\mu_{m_{1}}$','$\mu_{m_{2}}$','$\sigma_{m_{1}}$','$\sigma_{m_{2}}$',\
            '$D_{x}$','$\mu_{x_{1}}$','$\mu_{x_{2}}$','$\sigma_{x_{1}}$','$\sigma_{x_{2}}$',\
            '$D_{h}$','$\mu_{h_{1}}$','$\mu_{h_{2}}$','$\sigma_{h_{1}}$','$\sigma_{h_{2}}$','$\delta_{\mu_{h}}$','delta_tr']
        fixed_params = [{'n': n, '$D_{m}$':vP.Dm[0],'$\mu_{m_{1}}$':vP.mu_m_1[1],'$\mu_{m_{2}}$':vP.mu_m_2[1],\
            '$\sigma_{m_{1}}$':vP.sigma_m_1[0],'$\sigma_{m_{2}}$':vP.sigma_m_2[0],'$D_{x}$':vP.Dx[0],\
            '$\mu_{x_{1}}$':17,'$\mu_{x_{2}}$':17,'$\sigma_{x_{1}}$':vP.sigma_x_1[9],\
            '$\sigma_{x_{2}}$':vP.sigma_x_2[9], '$D_{h}$':None, '$\mu_{h_{1}}$': 13, '$\mu_{h_{2}}$': 13,\
            '$\sigma_{h_{1}}$':0, '$\sigma_{h_{2}}$':0,'$\delta_{\mu_{h}}$':0,'delta_tr':delt_tr}]

        packed_parameters.append(paramObjects(fixed_params=fixed_params,vP_=vP,data_columns=copy.copy(data_columns),path=path,fixed_params_column=fixed_params_column))

    elif uniformM:
        for n in vP.num_opts[:5]:
            fixed_params_column = ['n','$D_{m}$','$\mu_{m_{1}}$','$\mu_{m_{2}}$','$\sigma_{m_{1}}$','$\sigma_{m_{2}}$',\
                '$D_{x}$','$\mu_{x_{1}}$','$\mu_{x_{2}}$','$\sigma_{x_{1}}$','$\sigma_{x_{2}}$',\
                '$D_{h}$','$\mu_{h_{1}}$','$\mu_{h_{2}}$','$\sigma_{h_{1}}$','$\sigma_{h_{2}}$','$\delta_{\mu_{h}}$','delta_tr']
            fixed_params = [{'n': n, '$D_{m}$':vP.Dm[0],'$\mu_{m_{1}}$':vP.mu_m_1[1],'$\mu_{m_{2}}$':vP.mu_m_2[1],\
                '$\sigma_{m_{1}}$':vP.sigma_m_1[0],'$\sigma_{m_{2}}$':vP.sigma_m_2[0],'$D_{x}$':vP.Dx[0],\
                '$\mu_{x_{1}}$':vP.mu_x_1[99],'$\mu_{x_{2}}$':vP.mu_x_2[99],'$\sigma_{x_{1}}$':vP.sigma_x_1[9],\
                '$\sigma_{x_{2}}$':vP.sigma_x_2[9], '$D_{h}$':None, '$\mu_{h_{1}}$': 13, '$\mu_{h_{2}}$': 13,\
                '$\sigma_{h_{1}}$':0, '$\sigma_{h_{2}}$':0,'$\delta_{\mu_{h}}$':0,'delta_tr':None}]

            packed_parameters.append(paramObjects(fixed_params=fixed_params,vP_=vP,data_columns=copy.copy(data_columns),path=path,fixed_params_column=fixed_params_column))

    looper(p = packed_parameters[0], vP = vP)


# FATEC Ourinhos - Programação 1 - Seg Info
# Augusto Finotti Oliveira
# Prof. Rogério Lazanha
# 03/05/2022

# 31 - Faca um programa que receba a altura e o peso de uma pessoa. De acordo com a tabela a seguir,
# verifique e mostra qual a classificacao dessa pessoa
#        Altura    |                        Peso
#                  |   Ate 60  |  Entre 60 e 90 (Inclusive)  |  Acima de 90
# Menor que 1,20   |      A    |               D             |       G
# De 1,20 a 1,70   |      B    |               E             |       H
# Maior que 1,70   |      C    |               F             |       I

# Input
h = float(input("Digite sua altura em metros: "))
p = float(input("Digite seu peso em Kg: "))
# Processing and Output
if(h<1.2):
    if(p<60):
        cat = "A"
    elif(p>=60) and (p<=90):
        cat = "D"
    else:
        cat = "G"
elif(h>=1.2) and (h<=1.7):
    if(p<60):
        cat = "B"
    elif(p>=60) and (p<=90):
        cat = "E"
    else:
        cat = "H"
else:
    if(p<60):
        cat = "C"
    elif(p>=60) and (p<=90):
        cat = "F"
    else:
        cat = "I"
print("Sua catedora é: %s" %(cat))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 16 20:49:21 2022

@author: hanjiayue
"""

from textblob import TextBlob
import pandas as pd
import plotly.graph_objects as go
import requests
import numpy as np
import re
from textblob import Word
from collections import Counter, defaultdict
import matplotlib.pyplot as plt

class nlp:

    def __init__(self, files,stopfile = None):
        ''' Constructor '''
        self.M = {}  # stores text specific analysis variables
        self.text = ''
        self.contained_files = {}

        if stopfile is None:
            self.stop_words = requests.get(
                "https://gist.githubusercontent.com/rg089/35e00abf8941d72d"
                "419224cfd5b5925d/raw/12d899b70156fd0041fa9778d657330b024b959c"
                "/stopwords.txt").content.decode().splitlines()
        else:
            with open(stopfile) as f:
               for line in f:
                   self.stop_words = [line.split(',') for line in f]

        for file in files:
            brand = re.search('(?<=_)([a-zA-Z]*)(?=\.csv)', file)[0]
            self.contained_files[brand] = self.run_all(file)

    def load_stop_words(self, text):
        ''' A list of common or stop words.
        These get filtered from each file automatically'''
        return ' '.join([word for word in text.split()
                         if word not in self.stop_words])

    def clean_text(self, t):
        ''' remove everything after the https in the text'''
        split_str = t.split('https', 1)
        substr = split_str[0]
        return substr

    def read_text(self, filename):
        ''' Reads given text from file, converts to string and stores '''
        raw_csv = pd.read_csv(filename, names=['brand', 'date',
                                               'code', 'text',
                                               'unlike', 'like'], sep=',')
        raw_csv['text'] = raw_csv['text'].apply(lambda x: self.clean_text(x))

        for row in raw_csv['text']:
            row = self.load_stop_words(row.lower())
            row = row + '.'
            self.text += row

        self.M['text'] = self.text
        return self.text

    def sent_tokenize(self):
        ''' Returns list of stripped, tokenized sentences in text '''
        sents = re.split('[.!?]+', self.text.lower())
        self.M['Sents'] = sents
        return sents

    def word_tokenize(self):
        ''' Returns list of stripped, tokenized words in text '''
        words = [re.sub('[^\w\s]', '', word) for word in
                 re.split('\s+', self.text.lower())]
        while("" in words):
            words.remove("")
        return words

    def sentence_count(self):
        ''' Calculates total sentences in text '''
        sent_count = len(self.sent_tokenize())
        self.M['sentence_count'] = sent_count
        return sent_count

    def total_word_count(self):
        ''' Returns total words in text '''
        total_word_count = len(self.word_tokenize())
        self.M['total_word_count'] = total_word_count
        return total_word_count

    def word_count(self):
        ''' Returns word counts for each text and stores it'''

        word_count = Counter(self.word_tokenize())
        self.M['word_counts'] = word_count
        return word_count

    def avg_sent_len(self):
        ''' Returns and stores average sentence length for given text '''
        avg_slen = round(self.total_word_count() / self.sentence_count(), 3)
        self.M['avg_sentence_length'] = avg_slen
        return avg_slen

    def avg_word_len(self):
        ''' Returns and stores average word length for given
        text excluding punctuation '''
        total_wchar = sum([len(word) for word in self.word_tokenize()])
        avg_wlen = round(total_wchar / self.total_word_count(), 3)
        self.M['avg_word_length'] = avg_wlen
        return avg_wlen

    def sentiment(self, minsub=0.0, maxsub=1.0, minpol=-1.0, maxpol=1.0):
        ''' Gets polarity and subjectivity of text per given lines (int),
        stores, and returns tuple (pol,sub).'''

        # Finds Polarity and subjectivity for a string
        pol, sub = TextBlob(self.text).sentiment
        pol, sub = (round(pol,3), round(sub,3))

        # Sets the Polarity and Subjectivity values to an attribute
        if minpol <= pol <= maxpol and minsub <= sub <= maxsub:
            self.M['polarity'] = pol
            self.M['subjectivity'] = sub
        return pol,sub

    def count_syllable(self, word):
        ''' Counts the number of syllables in a word
        https://stackoverflow.com/questions/46759492/syllable-count-in-python'''

        # Sets the list of vowels
        count = 0
        vowels = 'aeiouy'

        # Checks if the first letter is a vowel
        if word[0] in vowels:
            count += 1

        # Iterates through string to see check for consonant followed by vowel
        for i in range(1, len(word)):
            if word[i] in vowels and word[i-1] not in vowels:
                count += 1

        # Checks if word ends with an e
        if word.endswith('e'):
            count -= 1

        # Sets the minimum count to 1 and then returns it
        return 1 if count < 1 else count

    def readability(self):
        ''' Assesses Readability using the Gunning Fog Formula '''

        # Finds the variables necessary for Gunning Fog Calculation
        sen_length = self.avg_sent_len()
        words = self.word_tokenize()
        syllables = [self.count_syllable(word) for word in words]

        # Finds percentage of hard words and then performs gunning fog equation
        hard_word_count = [count for count in syllables if count >= 2]
        hard_word_percent = (len(hard_word_count)/ len(words)) * 100
        score = 0.4 * (sen_length + hard_word_percent)
        self.M['readability'] = score

        return score

    def run_all(self, filename):
        ''' Runs all general analysis methods and
        returns dictionary with results '''
        self.M = {}
        # Runs all functions selected as below
        gen_methods = [self.read_text(filename), self.avg_sent_len(),
                       self.word_count(),self.avg_word_len(),self.sentiment(),
                       self.readability(),self.sentence_count(),
                       self.sent_tokenize()]

        for nlp_method in gen_methods:
            nlp_method

        return self.M

def main():
   pass

if __name__ == '__main__':
    main()


import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
from torch.utils.data import Dataset, DataLoader

torch.manual_seed(0)

class Greater1L(nn.Module):
    def __init__(self):
        super().__init__()

        self.WI = nn.Linear(2, 2, bias=True)
        self.WO = nn.Linear(2, 3, bias=True)
        self.criterion = nn.NLLLoss()

    def forward(self, x):
        x = self.WI(x)
        x = torch.relu(x)
        x = self.WO(x)
        x = torch.sigmoid(x)
        x = torch.softmax(x, dim=1)
        return x

    def loss(self, y_, y):
        return self.criterion(y_, y)

N = 10000
X = torch.rand(N, 2)
y = torch.empty(N, 1)
for i, x in enumerate(X):
    if x[0] > x[1]:
        y[i] = torch.as_tensor([0]).view(1, 1)
    elif x[0] < x[1]:
        y[i] = torch.as_tensor([1]).view(1, 1)
    else:
        y[i] = torch.as_tensor([2]).view(1, 1) #very rare due to random process in generating the pairs of numbers!

X_y = torch.cat((X, y), 1)

class PrepareData(Dataset):
    def __init__(self, X, y):
        self.X = X
        self.y = y

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

    def __getitem__(self, idx):
        return self.X[idx], self.y[idx]

ds = PrepareData(X=X_y[:, :-1], y=X_y[:, -1])
ds = DataLoader(ds, batch_size=10, shuffle=True)

greater_nn = Greater1L()
optmizer = optim.SGD(greater_nn.parameters(), lr=0.01)

epoch =100
for e in range(epoch):
    sum_l = 0
    for i, (X, y) in enumerate(ds):
        if i == 0:
            X_test, y_test = (X, y)
            continue
        optmizer.zero_grad()
        y_ = greater_nn.forward(X)
        y = y.type(torch.LongTensor)
        l = greater_nn.loss(y_, y)
        l.backward()
        optmizer.step()
        sum_l += l.item()
    else:
        with torch.no_grad():
            sum_tl = 0
            acc = 0
            y_ = greater_nn.forward(X_test)
            tl = greater_nn.loss(y_, y_test.type(torch.LongTensor))
            probs, predictions = y_.topk(1, dim=1)
            sum_tl += tl.item()
            equal = predictions == y_test.view(*predictions.shape)  # (b * 1) vs. (b)
            acc = torch.mean(equal.type(torch.FloatTensor))  # equals returns boolean values which mean function does not understand!
        print(f'Epoch: {e}/{epoch}\n'
              f'Batch Loss:{sum_l / i - 1}\n'
              f'Test Loss: {sum_tl}\n'
              f'Accuracy: {acc.item() * 100}%')

print(greater_nn.state_dict())

#try out of range numbers
y_ = greater_nn.forward(torch.as_tensor([[100., 20.]]))
print(y_.topk(1, dim=1))


# coding: utf-8
from __future__ import absolute_import

_CONFIG_DICT = {
    # Whether use wrapper class.
    #
    # Wrapper class is more adaptive to various types of callables but will
    # break if the code that was using the original function requires a real
    # function, instead of a callable. Known cases include PyQt slot functions.
    #
    'WRAP_USING_WRAPPER_CLASS': True,

    # Whether wrap base class attributes in a subclass.
    #
    # If enabled, wrapper attributes will be added to a subclass even if the
    # wrapped original attributes are defined in a base class.
    #
    # This helps in the case that base class attributes are implemented in C
    # extensions thus can not be traced directly.
    #
    'WRAP_BASE_CLASS_ATTRIBUTES': True,

    # Indentation unit text
    'INDENT_UNIT_TEXT': ' ' * 8,

    # Whether highlight title shows `self` argument's class instead of called
    # function's defining class.
    #
    # This helps reveal the real type of the `self` argument on which the
    # function is called.
    #
    'HIGHLIGHT_TITLE_SHOW_SELF_CLASS': True,

    # Highlight title line character count max
    'HIGHLIGHT_TITLE_LINE_CHAR_COUNT_MAX': 265,

    # Whether show main thread ID
    'SHOW_MAIN_THREAD_ID': False,

    # Whether show function's file path and line number in pre-call hook
    'SHOW_FUNC_FILE_PATH_LINENO_PRE_CALL': True,

    # Whether show function's file path and line number in post-call hook
    'SHOW_FUNC_FILE_PATH_LINENO_POST_CALL': False,

    # Whether wrapper function should debug info dict's URIs
    'WRAPPER_FUNC_DEBUG_INFO_DICT_URIS': False,

    # Whether printing handler should debug arguments inspect info
    'PRINTING_HANDLER_DEBUG_ARGS_INSPECT_INFO': False,

    # Whether printing handler should debug info dict.
    #
    # Notice info dict contains called function's arguments and printing these
    # arguments may cause errors.
    #
    'PRINTING_HANDLER_DEBUG_INFO_DICT': False,

    # Whether printing handler should debug info dict, excluding arguments.
    #
    # Use this if `PRINTING_HANDLER_DEBUG_INFO_DICT` causes errors.
    #
    'PRINTING_HANDLER_DEBUG_INFO_DICT_SAFE': False,
}

def get_config(key):
    return _CONFIG_DICT.get(key, None)

def set_config(key, value):
    _CONFIG_DICT[key] = value

def set_configs(config_dict):
    _CONFIG_DICT.update(config_dict)


import sys

import rostest

from file_helpers import create_temp_file
from s3_file_uploader_test_base import S3FileUploaderTestBase

PKG = 'rosbag_uploader_ros1_integration_tests'
NAME = 'test_s3_file_uploader_wrong_region'
AWS_REGION = 'eu-west-1'
INCORRECT_REGION_RESULT_CODE = 100 # Currently incorrect region returns UNKNOWN(100)

class TestS3FileUploaderWrongRegion(S3FileUploaderTestBase):
    @classmethod
    def extract_s3_region(cls):
        return AWS_REGION

    def test_bucket_in_wrong_region(self):
        self._create_upload_files_action_client()
        temp_file_name = create_temp_file()
        self.files_to_delete.append(temp_file_name)
        result = self._upload_temp_files([temp_file_name])
        self.assertFalse(result.result_code.success)
        self.assertEqual(result.result_code.error_code, INCORRECT_REGION_RESULT_CODE,
            "Result code was %d" % result.result_code.error_code)

if __name__ == '__main__':
    rostest.rosrun(PKG, NAME, TestS3FileUploaderWrongRegion, sys.argv)


#!/usr/bin/env python
"""Provides scikit interface."""

import numpy as np
import scipy as sp
import toolz as tz
import networkx as nx
from itertools import combinations
from collections import defaultdict
from scipy.sparse import csr_matrix
from ego.encode import node_encode_graph
from ego.encode import _make_encoder
from ego.encode import make_fragmenter_func
from ego.encode import fast_hash
from ego.component import convert
from ego.component import get_subgraphs_from_graph_component
from ego.vectorize import set_feature_size

def _to_sparse_matrix(encoding, node_ids, feature_size):
    mtx = defaultdict(int)
    for c, i in zip(encoding, node_ids):
        mtx[c, i] += 1
    data, row, col = [], [], []
    for c, i in mtx:
        row.append(i)
        col.append(c)
        data.append(mtx[(c, i)])
    shape = (max(row) + 1, feature_size)
    data_matrix = csr_matrix((data, (row, col)), shape=shape, dtype=np.float64)
    return data_matrix

def _node_vectorize(graph, decomposition_funcs, preprocessors, bitmask, feature_size):
    fragmenter_func = make_fragmenter_func(decomposition_funcs, preprocessors)
    encoding, node_ids = node_encode_graph(graph, fragmenter_func, bitmask)
    data_matrix = _to_sparse_matrix(encoding, node_ids, feature_size)
    return data_matrix

def node_vectorize(graph, decomposition_funcs, preprocessors=None, nbits=16):
    feature_size, bitmask = set_feature_size(nbits=nbits)
    return _node_vectorize(graph, decomposition_funcs, preprocessors, bitmask, feature_size)

def get_distance(graph, weight=None, type_of='shortest'):
    if type_of == 'shortest':
        dist_func = nx.shortest_path_length
    elif type_of == 'resistance':
        dist_func = nx.resistance_distance
    else:
        raise Exception('Unknown type of distance:%s'%type_of)
    n = nx.number_of_nodes(graph)
    dist_mtx = np.zeros((n,n))
    for i,u in enumerate(graph.nodes()):
        for j,v in enumerate(graph.nodes()):
            if u != v :
                dist_mtx[i,j] = dist_func(graph, u, v, weight=weight)
    return dist_mtx

def get_proximity(graph, effective_radius=1, weight=None, type_of='shortest', cutoff_effective_radius_factor=2):
    if effective_radius == 0:
        n = nx.number_of_nodes(graph)
        return np.eye(n,n)
    if effective_radius is None:
        effective_radius = nx.diameter(graph) / 4
    dist_mtx = get_distance(graph, weight=weight, type_of=type_of)
    prox_mtx = np.exp( - dist_mtx / effective_radius)
    prox_mtx[dist_mtx >= cutoff_effective_radius_factor * effective_radius] = 0
    return prox_mtx

def build_node_proximity_data_matrix(nodes_mtx, data_matrix, nbits, max_num_node_features=1):
    feature_size, bitmask = set_feature_size(nbits=nbits)
    data, row, col = [], [], []
    for node_id, (node_vec, row_vec) in enumerate(zip(nodes_mtx, data_matrix)):
        all_combinations = [sorted(comb) for combinations_order in range(1, max_num_node_features + 1) for comb in combinations(node_vec.indices, combinations_order)]
        for node_feature_id_combinations in all_combinations:
            node_feature_id_combinations = list(node_feature_id_combinations)
            for remote_feature_id, remote_feature_val in zip(row_vec.indices, row_vec.data):
                new_feature_id = fast_hash(node_feature_id_combinations+[remote_feature_id], bitmask=bitmask)
                row.append(node_id)
                col.append(new_feature_id)
                data.append(remote_feature_val)
    shape = (max(row) + 1, feature_size)
    new_data_matrix = csr_matrix((data, (row, col)), shape=shape, dtype=np.float64)
    return new_data_matrix

def node_attributes_vectorize(graph, attribute_label='attributes'):
    vec_list = [graph.nodes[u].get(attribute_label, [0]) for u in graph.nodes()]
    vecs = csr_matrix(np.array(vec_list))
    return vecs

def proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, weight=None, type_of='shortest', attribute_label=None, return_node_mtx=False):
    graph = nx.convert_node_labels_to_integers(graph_orig)
    prox_mtx = csr_matrix(get_proximity(graph, effective_radius=effective_radius, weight=weight, type_of=type_of, cutoff_effective_radius_factor=cutoff_effective_radius_factor))
    nodes_mtx = node_vectorize(graph, decomposition_funcs, preprocessors, nbits)

    if attribute_label is not None:
        attribute_nodes_mtx = node_attributes_vectorize(graph, attribute_label=attribute_label)
        nodes_mtx = sp.sparse.hstack([attribute_nodes_mtx, nodes_mtx])
        nodes_mtx = csr_matrix(nodes_mtx)

    data_matrix = prox_mtx.dot(nodes_mtx)

    if return_node_mtx:
        return nodes_mtx, data_matrix
    else:
        return data_matrix

def node_proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    nodes_mtx, data_matrix = proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=preprocessors, nbits=nbits, effective_radius=effective_radius, cutoff_effective_radius_factor=cutoff_effective_radius_factor, weight=weight, type_of=type_of, attribute_label=attribute_label, return_node_mtx=True)
    node_proximity_data_matrix = build_node_proximity_data_matrix(nodes_mtx, data_matrix, nbits, max_num_node_features)
    return node_proximity_data_matrix

def _graph_node_vectorize(graph, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    data_matrix = node_proximity_node_vectorize(graph, decomposition_funcs, preprocessors, nbits, effective_radius, cutoff_effective_radius_factor, max_num_node_features, weight, type_of, attribute_label)
    vec = csr_matrix(csr_matrix.sum(data_matrix, axis=0))
    return vec

def graph_node_vectorize(graphs, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    data_list = [_graph_node_vectorize(graph, decomposition_funcs, preprocessors, nbits, effective_radius, cutoff_effective_radius_factor, max_num_node_features, weight, type_of, attribute_label) for graph in graphs]
    data_matrix = sp.sparse.vstack(data_list)
    return data_matrix


import pandas as pd

def row_expander_seconds(row):
    """Expands the dataframe to have one row per minute, to remove the aggregation in the original df"""
    lower_bound = (row['start']-pd.Timedelta(seconds = 30)).round('T')
    upper_bound = (row['end']-pd.Timedelta(seconds = 30)).round('T')
    rows_needed = (int((upper_bound-lower_bound).total_seconds()/60))+1
    if rows_needed ==1:
        date_df = pd.DataFrame(columns=['date', 'screen_time'])
        new_row = {'date': lower_bound, 'screen_time' : row["duration_s"], 'pickups' : 1}
        date_df = date_df.append(new_row, ignore_index=True)
        return date_df
    dates = pd.date_range(lower_bound, upper_bound, freq='T')
    date_df = pd.DataFrame({'date': dates})
    for minute in range(1, rows_needed+1, 1):
        if minute == 1:
            seconds = (lower_bound + pd.Timedelta(minutes = 1) - row["start"]).total_seconds()
            pickup = 1
        elif minute != rows_needed:
            seconds = 60
            pickup = 0
        else:
            seconds = (row["end"] - upper_bound).total_seconds()
            pickup = 0
        date_df.loc[minute-1,'screen_time'] = seconds
        date_df.loc[minute-1,'pickups'] = pickup
    return date_df

def expand_df(df):
    """Expands the dataframe to have one row per minute, to remove the aggregation in the original df"""
    df['start'] = pd.to_datetime(df['start']) + pd.Timedelta(hours = 8)
    df['end'] = pd.to_datetime(df['end']) + pd.Timedelta(hours = 8)
    df['duration_s'] = (df.end - df.start).dt.total_seconds().astype(float)
    new_df = pd.DataFrame(columns=['date', 'screen_time', 'pickups'])
    old_df = pd.read_csv('files/processed_files/offscreen_processed.csv', sep = '|')
    #old_df['date'] = pd.to_datetime(old_df['date'])
    new_df = pd.concat([new_df, old_df], ignore_index=True)
    df = df[(df['start']-pd.Timedelta(seconds = 30)).round('T')>max(new_df["date"])].reset_index(drop=True)
    print(f'{df.shape[0]} new rows to expand')
    for _, row in df.iterrows():
        new_df = pd.concat([new_df, row_expander_seconds(row)], ignore_index=True)
    #new_df['date'] = pd.to_datetime(new_df['date'])
    new_df = new_df.groupby('date').sum().reset_index()
    return new_df

def screentime_before_sleep(df):
    """Computes the screentime in the hour before sleep"""
    df2 = pd.read_csv('files/processed_files/garmin_sleep_processed.csv', sep = '|')
    df['date'] = pd.to_datetime(df.date, utc = True).dt.tz_localize(None)
    df['sleepDate'] = df.date.apply(lambda x: x - pd.Timedelta(days = 1) if x.hour < 6 else x).dt.date
    df['sleepDate'] = pd.to_datetime(df.sleepDate, utc = True).dt.tz_localize(None)
    df2['sleepDate'] = pd.to_datetime(df2.calendarDate, utc = True).dt.tz_localize(None) - pd.Timedelta(days = 1)
    merged_df = pd.merge(df, df2[['sleepDate', "sleepStartTimestampLocal"]], on='sleepDate', how='left')
    merged_df['time_diff'] = pd.to_datetime(merged_df['sleepStartTimestampLocal']) - merged_df['date']
    merged_df['within_hour_before_sleep'] = merged_df['time_diff'].apply(lambda x: 1 if x <= pd.Timedelta(hours=1) else 0)
    return merged_df.drop(["sleepDate", "time_diff"], axis = 1)

def process_offscreen_export():
    df = pd.read_csv("files/exports/offscreen_exports/Pickup.csv")
    df = expand_df(df)
    df = screentime_before_sleep(df)
    df.sort_values("date", ascending = False).to_csv('files/processed_files/offscreen_processed.csv', sep = '|', index = False)


import copy
import numpy as np

from mher.samplers.sampler import RelabelSampler
from mher.samplers.her_sampler import HER_Sampler

class Nstep_Sampler(RelabelSampler):
    def __init__(self, T, reward_fun, batch_size, replay_p, nstep, gamma, *args):
        super(Nstep_Sampler, self).__init__(T, reward_fun, batch_size, replay_p, *args)
        self.nstep = nstep
        self.gamma = gamma

    def _sample_nstep_transitions(self, episode_batch):
        transitions, info = self._sample_transitions(episode_batch)
        episode_idxs, t_samples = info['episode_idxs'], info['t_samples']
        transitions['r'] = self.recompute_reward(transitions)
        transition_lis = [transitions]
        nstep_masks = [np.ones(self.batch_size)]
        for i in range(1, self.nstep):
            t_samples_i = t_samples + i
            out_range_idxs = np.where(t_samples_i > self.T-1)
            t_samples_i[out_range_idxs] = self.T - 1
            transitions = self._get_transitions(episode_batch, episode_idxs, t_samples_i)
            transition_lis.append(transitions)
            mask = np.ones(self.batch_size) * pow(self.gamma, i)
            mask[out_range_idxs] = 0
            nstep_masks.append(mask)
        return transition_lis, nstep_masks, info

    def _recompute_nstep_reward(self, transition_lis):
        for i in range(len(transition_lis)):
            transition_lis[i]['r'] = self.recompute_reward(transition_lis[i])
        return transition_lis

    # process to get final transitions
    def _get_out_transitions(self, transition_lis, nstep_masks):
        out_transitions = copy.deepcopy(transition_lis[0])
        final_gamma = np.ones(self.batch_size) * pow(self.gamma, self.nstep)  # gamma
        for i in range(1, self.nstep):
            out_transitions['r'] += nstep_masks[i] * transition_lis[i]['r']
            final_gamma[np.where((nstep_masks[i] == 0) & (final_gamma == pow(self.gamma, self.nstep)))] = pow(self.gamma, i)
        out_transitions['o_2'] = transition_lis[-1]['o_2'].copy()
        out_transitions['gamma'] = final_gamma.copy()
        return out_transitions

    def sample(self, episode_batch):
        transition_lis, nstep_masks, _ = self._sample_nstep_transitions(episode_batch)
        transition_lis = self._recompute_nstep_reward(transition_lis)
        out_transitions = self._get_out_transitions(transition_lis, nstep_masks)
        self.reshape_transitions(out_transitions)
        return out_transitions

class Nstep_HER_Sampler(Nstep_Sampler, HER_Sampler):
    def __init__(self, T, reward_fun, batch_size, relabel_p, nstep, gamma, strategy):
        super().__init__(T, reward_fun, batch_size, relabel_p, nstep, gamma, strategy)

    def relabel_nstep_transitions(self, episode_batch, transition_lis, info):
        relabel_ag, relabel_indexes = self._get_relabel_ag(episode_batch, info['episode_idxs'], info['t_samples'], info['num_episodes'])
        for i in range(len(transition_lis)):
            transitions = transition_lis[i]
            transitions = self.relabel_transition(transitions, relabel_indexes, relabel_ag)
            transition_lis[i] = transitions
        return transition_lis

    def sample(self, episode_batch):
        transition_lis, nstep_masks, info = self._sample_nstep_transitions(episode_batch)
        transition_lis = self.relabel_nstep_transitions(episode_batch, transition_lis, info)
        out_transitions = self._get_out_transitions(transition_lis, nstep_masks)
        out_transitions = self.reshape_transitions(out_transitions)
        return out_transitions


from db import db_session, Card

cards = [
    {
        'en_meaning': 'Puppy',
        'ru_meaning': 'Щенок',
        'example': 'National charity, Hearing Dogs for Deaf People, which has a training centre at Cliffe, near Selby, is appealing for volunteers to take in puppies and young dogs for anything up to six weeks.',
        'extra_info': 'ˈpʌpi',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Squirrel',
        'ru_meaning': 'Белка',
        'example': 'Tree-dwelling squirrels have lovely bushy tails, and we realised that the little ones, from the look of them, were also from one such splendid family line.',
        'extra_info': 'ˈskwɪrəl',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Truck',
        'ru_meaning': 'Грузовик',
        'example': 'There will be classic and vintage cars, racing cars, go-carts, bikes, trucks , service vehicles and just about anything else with wheels.',
        'extra_info': 'trʌk',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Unjustified',
        'ru_meaning': 'Необоснованный, неоправданный',
        'example': 'Unjustified price increases.',
        'extra_info': 'ˌənˈjəstəˌfīd',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Impeachment',
        'ru_meaning': 'Импичмент, сомнение, обвинение',
        'example': '',
        'extra_info': '',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Train',
        'ru_meaning': 'Поезд',
        'example': 'A freight train.',
        'extra_info': '',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Wine',
        'ru_meaning': 'Вино',
        'example': 'He opened a bottle of red wine.',
        'extra_info': 'wīn',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Roomy',
        'ru_meaning': 'Вместительный, просторный',
        'example': 'The open-plan lounge and dining room offer bright and roomy accommodation.',
        'extra_info': 'ˈro͝omē,ˈro͞omē',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Props',
        'ru_meaning': 'Реквизит, бутафория',
        'example': 'Erika gets props for the great work she did on the music.',
        'extra_info': 'präps',
        'score': '0',
        'is_active': 1,
    },
    {
        'en_meaning': 'Rag',
        'ru_meaning': 'Тряпка',
        'example': 'He wiped his hands on an oily rag.',
        'extra_info': 'rag',
        'score': '0',
        'is_active': 1,
    }
]

# в цикле создаём объект card, это объект класса Card
for c in cards:
    card = Card(c['en_meaning'], c['ru_meaning'], c['example'], c['extra_info'], c['score'], c['is_active'])
    db_session.add(card)

db_session.commit()


"""

This sample performs a PAdES signature in three steps using PKI Express and
Web PKI.

"""
import os
import uuid

from flask import render_template
from flask import request
from flask import current_app
from flask import Blueprint
from pkiexpress import standard_signature_policies
from pkiexpress import PadesSignatureStarter
from pkiexpress import SignatureFinisher

from sample.pades_visual_elements_express import PadesVisualElementsExpress
from sample.storage_mock import create_app_data
from sample.storage_mock import get_pdf_stamp_path
from sample.utils import set_pki_defaults

# 26-08-2022
# By further inspecting in the latest Blueprint documentation (https://flask.palletsprojects.com/en/2.2.x/api/#blueprint-objects),
# when creating a Blueprint object, the first parameter (name) is prepend to the URL endpoint. Therefore, Blueprint no longer
# allows dots in the name since it would break the URL entirely.
__name__ = __name__.replace(".","/")
blueprint = Blueprint(os.path.basename(__name__), __name__,
                      url_prefix='/pades-signature-express')

@blueprint.route('/<file_id>')
def index(file_id):
    """

    This method only renders the signature page.

    """
    # Verify if the provided userfile exists.
    file_path = os.path.join(current_app.config['APPDATA_FOLDER'], file_id)
    if not os.path.exists(file_path):
        return render_template('error.html', msg='File not found')

    return render_template('pades_signature_express/index.html')

@blueprint.route('/start/<file_id>', methods=['POST'])
def start(file_id):
    """

    This method starts the signature. In this sample, it will be called
    programatically after the user press the "Sign File" button (see method
    readCertificate() on static/js/signature-start-form.js).

    """
    try:
        # Recover variables from the POST arguments to be used on this step.
        cert_thumb = request.form['certThumbField']
        cert_content = request.form['certContentField']

        # Get an instance of the PadesSignatureStarter class, responsible for
        # receiving the signature elements and start the signature process.
        signature_starter = PadesSignatureStarter()

        # Set PKI default options (see utils.py).
        set_pki_defaults(signature_starter)

        # Set signature policy.
        signature_starter.signature_policy = \
            standard_signature_policies.PADES_BASIC

        # Set PDF to be signed
        signature_starter.set_pdf_to_sign_from_path(
            os.path.join(current_app.config['APPDATA_FOLDER'], file_id))

        # Set Base64-encoded certificate's content to signature starter.
        signature_starter.set_certificate_from_base64(cert_content)

        # Set a file reference for the stamp file. Note that this file can be
        # referenced later by "fref://{alias}" at the "url" field on the visual
        # representation (see static/vr.json or get_visual_representation()
        # method).
        signature_starter.add_file_reference('stamp', get_pdf_stamp_path())

        # Set the visual representation. We provided a dictionary that
        # represents the visual representation JSON model.
        signature_starter.set_visual_representation(
            PadesVisualElementsExpress.get_visual_representation())

        # Start the signature process. Receive as response the following fields:
        # - to_sign_hash:     The hash to be signed.
        # - digest_algorithm: The digest algorithm that will inform the Web PKI
        #                     component to compute the signature.
        # - transfer_file:    A temporary file to be passed to "complete" step.
        response = signature_starter.start()

        # Render the field from start() method as hidden field to be used on the
        # javascript or on the "complete" step.
        return render_template('pades_signature_express/start.html',
                               cert_thumb=cert_thumb,
                               transfer_file_id=response['transferFile'],
                               to_sign_hash=response['toSignHash'],
                               digest_algorithm=response['digestAlgorithm'])

    except Exception as e:
        return render_template('error.html', msg=e)

@blueprint.route('/complete/<file_id>', methods=['POST'])
def complete(file_id):
    """

    This function completes the signature, it will be called programatically
    after the Web PKI component perform the signature and submit the form (see
    method sign() on static/js/signature-complete-form.js).

    """
    try:

        # Recover variables form the POST arguments to be used on this step.
        transfer_file = request.form['transferFileIdField']
        signature = request.form['signatureField']

        # Get an instance of the SignatureFinisher class, responsible for
        # completing the signature process.
        signature_finisher = SignatureFinisher()

        # Set PKI default options (see utils.py).
        set_pki_defaults(signature_finisher)

        # Set PDF to be signed. It's the same file we used on "start" method.
        signature_finisher.set_file_to_sign_from_path(
            os.path.join(current_app.config['APPDATA_FOLDER'], file_id))

        # Set the transfer file.
        signature_finisher.set_transfer_file_from_path(transfer_file)

        # Set the signature file.
        signature_finisher.signature = signature

        # Generate path for output file and add to the signature finisher.
        create_app_data()  # Guarantees that "app data" folder exists.
        filename = '%s.pdf' % (str(uuid.uuid4()))
        signature_finisher.output_file = \
            os.path.join(current_app.config['APPDATA_FOLDER'], filename)

        # Complete the signature process.
        signer_cert = signature_finisher.complete(get_cert=True)

        return render_template('pades_signature_express/signature-info.html',
                               signer_cert=signer_cert,
                               signed_pdf=filename)

    except Exception as e:
        return render_template('error.html', msg=e)


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 26 09:47:54 2019

@author: admin
"""
import os
import pandas as pd
import numpy as np
from datetime import datetime
from utility.factor_data_preprocess import get_factor_data, fill_na, winsorize,\
                                        neutralize, standardize, process_input_names
from utility.single_factor_test import get_firt_industry_list
from utility.constant import info_cols
# from canslim.update_stock_pool import form_stock_pool
from barra_cne6.barra_template import Data

# print(__file__)                         # 打印 相对路径
# print(os.path.abspath(__file__))        # 打印 绝对路径
# 股票多因子存储位置

industry_benchmark = 'sw'               # 行业基准（用于缺失值填充和中性化）
                                        # zx - 中信； sw - 申万

# 基准信息所在列名（分别对应：
# code - 证券wind代码；  name - 证券简称；     ipo_date - 上市日期；
# industry_zx - 中信一级行业；     industry_sw - 申万一级行业；
# MKT_CAP_FLOAT - 流通市值；       is_open1 - 当日是否开盘；
# PCT_CHG_NM - 下个月的月收益率

def main(p_dict, fp, is_ind_neu, is_size_neu, is_plate_neu, special_plate=None,
         selection=None):
    """
    输入： 需要进行预处理的因子名称（可为1个或多个，默认为对所有因子进行预处理）
    is_ind_neu : 是否做行业中性化处理，对股票多因子需要，做行业多因子时不需要
    输出： 预处理后的因子截面数据（如2009-01-23.csv文件）

    对指定的原始因子数据进行预处理
    顺序：缺失值填充、去极值、中性化、标准化
    （因输入的截面数据中所含财务类因子默认已经过
    财务日期对齐处理，故在此不再进行该步处理）
    """
    file_path = p_dict['file_path']
    save_path = p_dict['save_path']

    # 读取原始因子截面数据
    try:
        data = pd.read_csv(os.path.join(file_path, fp), engine='python',
                           encoding='gbk')
    except Exception as e:
        print('debug')
    if 'No' in data.columns:
        data = data.set_index('No')

    # 若针对特定板块，则删除其他板块的股票数据
    if special_plate:
        data_ = Data()
        stock_basic = data_.stock_basic_inform
        sw_1 = stock_basic[['申万一级行业']]
        stock_list = list(sw_1.index[sw_1[sw_1.columns[0]] == special_plate])
        # 剔除在当期还未上市的股票
        codes = [i for i in data.index if data.loc[i, 'Code'] in stock_list]
        data = data.loc[codes, :]

        data.index = range(0, len(data))

    # 根据输入的因子名称将原始因子截面数据分割
    data_to_process, data_unchanged = get_factor_data(data)

    # '002345.SZ' in data_to_process['Code']

    # 预处理步骤依次进行
    data_to_process = fill_na(data_to_process)                                    # 缺失值填充
    if len(data_to_process) == 0:
        print('debug')
    data_to_process = winsorize(data_to_process)                                  # 去极值
    if is_ind_neu or is_size_neu:
        data_to_process = neutralize(data_to_process, ind_neu=is_ind_neu, size_neu=is_size_neu)  # 中性化
    data_to_process = standardize(data_to_process)                                # 标准化

    # 合并生成经过处理后的总因子文件
    if len(data_unchanged) > 0:
        data_final = pd.concat([data_to_process, data_unchanged.loc[data_to_process.index]], axis=1)
    else:
        data_final = data_to_process

    if data_final.index.name != 'No':
        data_final.index = range(1, len(data_final)+1)
        data_final.index.name = 'No'

    data_final.to_csv(os.path.join(save_path, fp), encoding='gbk')

def factor_preprocess():
    is_ind_neu = True  # 股票时需要，为True，行业时或单行业测试时不需要，为False,
    is_plate_neu = False  # 板块中性
    is_size_neu = True  # 是否需要对市值做中性化

    path_dict = {
                 'file_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                           '因子预处理模块', '因子').replace('\\', '/'),
                 'save_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                           '因子预处理模块', '因子（已预处理）').replace('\\', '/'),
                 }

    fls = os.listdir(path_dict['file_path'])
    processed_list = os.listdir(path_dict['save_path'])
    to_process_f = [f for f in fls if f not in processed_list]

    if len(to_process_f) == 0:
        print('无需要处理的数据')

    # 对所有横截面数据进行遍历
    for fpath in to_process_f:
        print('目前处理的月份为：')
        print(fpath)
        main(path_dict, fpath, is_ind_neu, is_size_neu, is_plate_neu)
    print('因子截面数据已全部处理！')

def factor_preprocess_for_indus_factor():
    is_ind_neu = False  # 股票时需要，为True，行业时或单行业测试时不需要，为False,
    is_plate_neu = False  # 板块中性
    is_size_neu = False  # 是否需要对市值做中性化

    path_dict = {
                 'file_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                           '行业多因子', 'second_industry', '因子').replace('\\', '/'),
                 'save_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                           '行业多因子', 'second_industry', '因子(已预处理)').replace('\\', '/'),
                 }

    fls = os.listdir(path_dict['file_path'])
    processed_list = os.listdir(path_dict['save_path'])
    to_process_f = [f for f in fls if f not in processed_list]

    if len(to_process_f) == 0:
        print('无需要处理的数据')
    else:
        # 对所有横截面数据进行遍历
        for fpath in to_process_f:
            print('目前处理的月份为：')
            print(fpath)
            main(path_dict, fpath, is_ind_neu, is_size_neu, is_plate_neu)
        print('因子截面数据已全部处理！')

if __name__ == '__main__':

    is_ind_neu = True         # 股票时需要，为True，行业时或单行业测试时不需要，为False,
    is_size_neu = True        # 是否需要对市值做中性化
    formed_stock_pool = None
    test_type = 'stock'       # 'stock'  'each_industry'  #
    indus_list = get_firt_industry_list()
    is_update = True          # 是否仅对更新的数据进行处理

    # 收集需要处理的因子名称
    # factor_names = input("请输入需处理的因子名称（请使用英文逗号','分隔多个因子名称，输入'a'代表全部处理）：")
    factor_names = process_input_names('a')

    # 股票
    if test_type == 'stock':
        path_dict = {
                     'file_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                               '因子预处理模块', '因子').replace('\\', '/'),
                     'save_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                               '因子预处理模块', '因子（已预处理）').replace('\\', '/'),
                     }

        # 创建处理后因子的存放目录
        if not os.path.exists(path_dict['save_path']):
            os.makedirs(path_dict['save_path'])

        if is_update:
            to_deal_fpath = [fp for fp in os.listdir(path_dict['file_path'])
                             if fp not in os.listdir(path_dict['save_path'])]
        else:
            to_deal_fpath = os.listdir(path_dict['file_path'])[:]

            # 对所有横截面数据进行遍历
        for fpath in to_deal_fpath:
            print('目前处理的月份为：')
            print(fpath)
            # if fpath == '2018-02-28.csv':
            #     print('here')
            main(path_dict, fpath, is_ind_neu, is_size_neu, factor_names)
        print('因子截面数据已全部处理！')

    elif test_type == 'each_industry':
        is_ind_neu = False
        completed = []
        # for indus in indus_list:
        if True:
            # if indus in completed:
            #     continue
            special_plate = '建筑材料'  # indus         # '建筑材料'  # '采掘'  #
            print(special_plate)

            path_dict = {
                'file_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                          '因子预处理模块', '因子').replace('\\', '/'),
                'save_path': os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                          '分行业研究', special_plate, '因子（已预处理）').replace('\\', '/'),
            }
            # 创建处理后因子的存放目录
            if not os.path.exists(path_dict['save_path']):
                os.makedirs(path_dict['save_path'])

            # 对所有横截面数据进行遍历
            for fpath in os.listdir(path_dict['file_path'])[:]:
                # print('目前处理的月份为：')
                # print(fpath)
                # if fpath == '2009-04-30.csv':
                #     print('here')

                main(path_dict, fpath, is_ind_neu, is_size_neu, factor_names, special_plate=special_plate)
            print('{}行业的因子截面数据已全部处理！'.format(special_plate))


import itertools
import sys
import numpy

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

def HammingDistance(p, q):
    if len(p) != len(q):
        return 'Invalid input'

    hamming_dist = 0 # Initialize to zero

    for i in range(len(p)):
        if p[i] != q[i]:
            hamming_dist += 1
    #print hamming_dist
    return hamming_dist

##    INPUT: string Pattern, list of strings DNA
##    OUTPUT: d(DNA, Pattern)
def DistanceBetweenPatternAndStrings(DNA, Pattern):
    k = len(Pattern)
    distance = 0

    for Text in DNA:
        HammingDistanceMax = sys.maxsize

        for i in range(len(Text) - k + 1):
            test_string = Text[i:i+k]

            condition = HammingDistance(Pattern, test_string)
            if HammingDistanceMax > condition:
                HammingDistanceMax = condition
        distance = distance + HammingDistanceMax

    return distance


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

"""
@author: jbl
@time: 2021/10/6 9:57
"""
from typing import List

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        a = b = c = float('-inf')
        for n in nums:
            if n > c:
                a, b, c = b, c, n
            elif b < n < c:
                a, b = b, n
            elif a < n < b:
                a = n
            else:
                pass
        if a == float('-inf'):
            return c
        else:
            return a

# class Solution:
#     def thirdMax(self, nums: List[int]) -> int:
#         nums = set(nums)
#         if len(nums) < 3:
#             return max(nums)
#         heap = []
#         for n in nums:
#             heapq.heappush(heap, -n)
#         heapq.heappop(heap)
#         heapq.heappop(heap)
#         return -heapq.heappop(heap)

if __name__ == '__main__':
    solution = Solution()
    print(solution)


arquivo = open("alunos.txt", "a") #abertura do arquivo (cria se não existir)

i = 1
while True:
    idade = int(input("Digite a idade do aluno %d: " % i))

    if idade < 0:
        break

    nome = input("Digite o nome do aluno %d: " % i)
    curso = input("Digite o curso do aluno %d: " % i)

    arquivo.write("%s;%d;%s\n" % (nome, idade, curso)) #escrita do arquivo
    i += 1

arquivo.close() #fechar o arquivo


def moving(d, y, x, cnt):       # 움직이기
    ny = y + dy[d]
    nx = x + dx[d]
    if 0 <= ny < n and 0 <= nx < n and visited[arr[ny][nx]] == 0:
        visited[arr[ny][nx]] = 1        # 방문 표시
        recur(d, ny, nx, cnt + 1, 1)
        visited[arr[ny][nx]] = 0        # 재귀를 빠져 나오면 방문 표시 제거

def recur(cur, y, x, cnt, move):
    global max_cnt
    if cur == 3 and i == y:             # 사각형 완성했을 때
        max_cnt = max(max_cnt, cnt)     # 최댓값 업데이트
        return
    if cur == 2 and max_cnt > cnt * 2:  # 가지치기 두 변의 길이가 현재 값보다 작으면 return
        return
    if cur == 2 and i + j == y + x:     # 두 변을 그린 후부터는 변의 길이에 맞춰 잇는다.
        recur(cur + 1, y, x, cnt, 0)    # 마지막 세번째 꺾고 시작점으로 잇는다.
        return
    # 가던 방향으로 움직인다.
    moving(cur, y, x, cnt)
    # 방향 전환 - 2번까지 임의로 꺾고 3번부터는 사각형을 그리니 임의로 꺾지 않는다.
    if cur < 2 and move:    # 한 번이라도 움직였을 때 꺾는다.
        recur(cur + 1, y, x, cnt, 0)

dy = [1, 1, -1, -1]     # 움직일 순서 : 오른쪽 아래, 왼쪽 아래, 왼쪽 위, 오른쪽 위
dx = [1, -1, -1, 1]
for tc in range(1, 1 + int(input())):
    n = int(input())
    arr = [list(map(int, input().split())) for _ in range(n)]
    visited = [0 for _ in range(101)]       # 중복 확인하기 위함
    max_cnt = 0                             # 출력할 결과

    for i in range(n):          # 모든 점에서 start
        for j in range(n):
            recur(0, i, j, 0, 0)

    if max_cnt == 0:            # 사각형을 그릴 수 없으면 -1 출력
        print(f'#{tc} -1')
    else:
        print(f'#{tc} {max_cnt}')


from transformers import pipeline

def img2txt(url: str) -> str :
    model = "Salesforce/blip-image-captioning-base"
    task = "image-to-text"
    image_to_text = pipeline(task, model=model)
    response = image_to_text(url)
    text = response[0]['generated_text']
    return text


class FormResults(object):
    """ postprocessing results of FORM analysis"""
    def __init__(self, iternum, beta1, pf1, dsptu, dsptx, alpha):
        """ results of reliability analysis
            iternum: iteration number
            beta1: reliability index by FORM
            pf1: failure probability by FORM
            dsptu: design point in u space (reduced norm)
            dsptx: design point in x space (random variable)
            alpha: alpha vector
        """
        self.iternum = iternum
        self.beta1 = beta1
        self.pf1 = pf1
        self.dsptu = dsptu
        self.dsptx = dsptx
        self.alpha = alpha
        # sensitivity results
        self.imptg = None
        self.gfunchist = None
        self.gradGhist = None
        self.stephist = None

    def set_sens_res(self, imptg, gfunchist, gradGhist, stephist):
        """sensitivity results
           imptg: importance vector gamma
           gfunchist: recorded gfunc values
           gradGhist: recorded gradient values of gfunc
           stephist: recorded step size values
        """
        self.imptg = imptg
        self.gfunchist = gfunchist
        self.gradGhist = gradGhist
        self.stephist = stephist

class ReliabilityResults(object):
    """ postprocessing results of FORM analysis"""
    def __init__(self, beta, pf):
        """ results of reliability analysis
            beta: reliability
            pf: failure probability
        """
        self.beta = beta
        self.pf = pf


import logging
from rich.logging import RichHandler

FORMAT = "%(message)s"

def configure_logger(level: int) -> None:
    logging.basicConfig(
        level=level, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
    )
    log.setLevel(level)

log = logging.getLogger("nettowel")
log.info("Hello, World!")


from math import ceil, log10

class Grid:

    START = "S"
    STOP = "E"

    START_VALUE = "a"
    STOP_VALUE = "z"

    @staticmethod
    def __read_input_file(path_in):
        if path_in is None:
            return None
        else:
            with open(path_in, "r") as input_file:
                return tuple(input_file.readlines())

    @staticmethod
    def __find_char(char, lines):
        for line in lines:
            if char in line:
                return line.index(char), lines.index(line)

    @staticmethod
    def __get_value_of_char(char):
        return ord(char.lower()) - 97

    def __init__(self, path_in=None):
        lines = self.__read_input_file(path_in)
        self.start = self.__find_char(self.START, lines)
        self.stop = self.__find_char(self.STOP, lines)

        self.__contents = self.__generate_contents(lines)
        self.__height = len(self.__contents)
        self.__width = 0 if self.__height == 0 else len(self.__contents[0])

    def __str__(self):
        if self.__contents:
            spacing = ceil(log10(self.maximum))
            single_format = "{:" + str(spacing) + "} "
            full_format = single_format * (self.__width - 1)
            return "\n".join(
                full_format.format(*row) for row in self.__contents
            )

        else:
            return "Grid()"

    def __generate_contents(self, lines):
        if lines is None:
            return tuple()
        else:
            return tuple(
                tuple(self.__get_value_of_char(c) for c in line) for line in lines
            )

    def access(self, coordinates):
        x, y = coordinates
        if x > self.__width or x < 0 or y > self.__height or y < 0:
            msg = f"{type(self).__name__} index out of range, indices must be positive"
            raise IndexError(msg)
        else:
            return self.__contents[y][x]

    @property
    def maximum(self):
        try:
            return max(
                max(row for row in self.__contents)
            )
        except ValueError:
            return 0

    @property
    def minimum(self):
        try:
            return min(
                min(row for row in self.__contents)
            )
        except ValueError:
            return 0

    @property
    def width(self):
        return self.__width

    @property
    def height(self):
        return self.__height


def shell_sort(arr):
    n = len(arr)
    gap = n // 2

    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2

if __name__ == "__main__":
    input_list = [12, 34, 54, 2, 3]
    print("Original list:", input_list)

    shell_sort(input_list)

    print("Sorted list:", input_list)


import os
import time
import datetime
import cv2
import queue
import threading
import ctypes
import pyarrow.plasma as plasma
import multiprocessing as mp
import subprocess as sp
import numpy as np
import copy
import itertools
import json
from collections import defaultdict
from frigate.util import draw_box_with_label, area, calculate_region, clipped, intersection_over_union, intersection, EventsPerSecond, listen, PlasmaManager
from frigate.objects import ObjectTracker
from frigate.edgetpu import RemoteObjectDetector
from frigate.motion import MotionDetector

def get_frame_shape(source):
    ffprobe_cmd = " ".join([
        'ffprobe',
        '-v',
        'panic',
        '-show_error',
        '-show_streams',
        '-of',
        'json',
        '"'+source+'"'
    ])
    print(ffprobe_cmd)
    p = sp.Popen(ffprobe_cmd, stdout=sp.PIPE, shell=True)
    (output, err) = p.communicate()
    p_status = p.wait()
    info = json.loads(output)
    print(info)

    video_info = [s for s in info['streams'] if s['codec_type'] == 'video'][0]

    if video_info['height'] != 0 and video_info['width'] != 0:
        return (video_info['height'], video_info['width'], 3)

    # fallback to using opencv if ffprobe didnt succeed
    video = cv2.VideoCapture(source)
    ret, frame = video.read()
    frame_shape = frame.shape
    video.release()
    return frame_shape

def get_ffmpeg_input(ffmpeg_input):
    frigate_vars = {k: v for k, v in os.environ.items() if k.startswith('FRIGATE_')}
    return ffmpeg_input.format(**frigate_vars)

def filtered(obj, objects_to_track, object_filters, mask):
    object_name = obj[0]

    if not object_name in objects_to_track:
        return True

    if object_name in object_filters:
        obj_settings = object_filters[object_name]

        # if the min area is larger than the
        # detected object, don't add it to detected objects
        if obj_settings.get('min_area',-1) > obj[3]:
            return True

        # if the detected object is larger than the
        # max area, don't add it to detected objects
        if obj_settings.get('max_area', 24000000) < obj[3]:
            return True

        # if the score is lower than the threshold, skip
        if obj_settings.get('threshold', 0) > obj[1]:
            return True

        # compute the coordinates of the object and make sure
        # the location isnt outside the bounds of the image (can happen from rounding)
        y_location = min(int(obj[2][3]), len(mask)-1)
        x_location = min(int((obj[2][2]-obj[2][0])/2.0)+obj[2][0], len(mask[0])-1)

        # if the object is in a masked location, don't add it to detected objects
        if mask[y_location][x_location] == [0]:
            return True

        return False

def create_tensor_input(frame, region):
    cropped_frame = frame[region[1]:region[3], region[0]:region[2]]

    # Resize to 300x300 if needed
    if cropped_frame.shape != (300, 300, 3):
        cropped_frame = cv2.resize(cropped_frame, dsize=(300, 300), interpolation=cv2.INTER_LINEAR)

    # Expand dimensions since the model expects images to have shape: [1, 300, 300, 3]
    return np.expand_dims(cropped_frame, axis=0)

def start_or_restart_ffmpeg(ffmpeg_cmd, frame_size, ffmpeg_process=None):
    if not ffmpeg_process is None:
        print("Terminating the existing ffmpeg process...")
        ffmpeg_process.terminate()
        try:
            print("Waiting for ffmpeg to exit gracefully...")
            ffmpeg_process.communicate(timeout=30)
        except sp.TimeoutExpired:
            print("FFmpeg didnt exit. Force killing...")
            ffmpeg_process.kill()
            ffmpeg_process.communicate()
        ffmpeg_process = None

    print("Creating ffmpeg process...")
    print(" ".join(ffmpeg_cmd))
    process = sp.Popen(ffmpeg_cmd, stdout = sp.PIPE, stdin = sp.DEVNULL, bufsize=frame_size*10, start_new_session=True)
    return process

class CameraCapture(threading.Thread):
    def __init__(self, name, ffmpeg_process, frame_shape, frame_queue, take_frame, fps, detection_frame):
        threading.Thread.__init__(self)
        self.name = name
        self.frame_shape = frame_shape
        self.frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2]
        self.frame_queue = frame_queue
        self.take_frame = take_frame
        self.fps = fps
        self.skipped_fps = EventsPerSecond()
        self.plasma_client = PlasmaManager()
        self.ffmpeg_process = ffmpeg_process
        self.current_frame = 0
        self.last_frame = 0
        self.detection_frame = detection_frame

    def run(self):
        frame_num = 0
        self.skipped_fps.start()
        while True:
            if self.ffmpeg_process.poll() != None:
                print(f"{self.name}: ffmpeg process is not running. exiting capture thread...")
                break

            frame_bytes = self.ffmpeg_process.stdout.read(self.frame_size)
            self.current_frame = datetime.datetime.now().timestamp()

            if len(frame_bytes) == 0:
                print(f"{self.name}: ffmpeg didnt return a frame. something is wrong.")
                continue

            self.fps.update()

            frame_num += 1
            if (frame_num % self.take_frame) != 0:
                self.skipped_fps.update()
                continue

            # if the detection process is more than 1 second behind, skip this frame
            if self.detection_frame.value > 0.0 and (self.last_frame - self.detection_frame.value) > 1:
                self.skipped_fps.update()
                continue

            # put the frame in the plasma store
            self.plasma_client.put(f"{self.name}{self.current_frame}",
                    np
                        .frombuffer(frame_bytes, np.uint8)
                        .reshape(self.frame_shape)
                )
            # add to the queue
            self.frame_queue.put(self.current_frame)
            self.last_frame = self.current_frame

def track_camera(name, config, global_objects_config, frame_queue, frame_shape, detection_queue, detected_objects_queue, fps, detection_fps, read_start, detection_frame):
    print(f"Starting process for {name}: {os.getpid()}")
    listen()

    detection_frame.value = 0.0

    # Merge the tracked object config with the global config
    camera_objects_config = config.get('objects', {})
    # combine tracked objects lists
    objects_to_track = set().union(global_objects_config.get('track', ['person', 'car', 'truck']), camera_objects_config.get('track', []))
    # merge object filters
    global_object_filters = global_objects_config.get('filters', {})
    camera_object_filters = camera_objects_config.get('filters', {})
    objects_with_config = set().union(global_object_filters.keys(), camera_object_filters.keys())
    object_filters = {}
    for obj in objects_with_config:
        object_filters[obj] = {**global_object_filters.get(obj, {}), **camera_object_filters.get(obj, {})}

    frame = np.zeros(frame_shape, np.uint8)

    # load in the mask for object detection
    if 'mask' in config:
        mask = cv2.imread("/config/{}".format(config['mask']), cv2.IMREAD_GRAYSCALE)
    else:
        mask = None

    if mask is None:
        mask = np.zeros((frame_shape[0], frame_shape[1], 1), np.uint8)
        mask[:] = 255

    motion_detector = MotionDetector(frame_shape, mask, resize_factor=6)
    object_detector = RemoteObjectDetector(name, '/labelmap.txt', detection_queue)

    object_tracker = ObjectTracker(10)

    plasma_client = PlasmaManager()
    avg_wait = 0.0
    fps_tracker = EventsPerSecond()
    fps_tracker.start()
    object_detector.fps.start()
    while True:
        read_start.value = datetime.datetime.now().timestamp()
        frame_time = frame_queue.get()
        duration = datetime.datetime.now().timestamp()-read_start.value
        read_start.value = 0.0
        avg_wait = (avg_wait*99+duration)/100
        detection_frame.value = frame_time

        # Get frame from plasma store
        frame = plasma_client.get(f"{name}{frame_time}")

        if frame is plasma.ObjectNotAvailable:
            continue

        fps_tracker.update()
        fps.value = fps_tracker.eps()
        detection_fps.value = object_detector.fps.eps()

        # look for motion
        motion_boxes = motion_detector.detect(frame)

        tracked_objects = object_tracker.tracked_objects.values()

        # merge areas of motion that intersect with a known tracked object into a single area to look at
        areas_of_interest = []
        used_motion_boxes = []
        for obj in tracked_objects:
            x_min, y_min, x_max, y_max = obj['box']
            for m_index, motion_box in enumerate(motion_boxes):
                if intersection_over_union(motion_box, obj['box']) > .2:
                    used_motion_boxes.append(m_index)
                    x_min = min(obj['box'][0], motion_box[0])
                    y_min = min(obj['box'][1], motion_box[1])
                    x_max = max(obj['box'][2], motion_box[2])
                    y_max = max(obj['box'][3], motion_box[3])
            areas_of_interest.append((x_min, y_min, x_max, y_max))
        unused_motion_boxes = set(range(0, len(motion_boxes))).difference(used_motion_boxes)

        # compute motion regions
        motion_regions = [calculate_region(frame_shape, motion_boxes[i][0], motion_boxes[i][1], motion_boxes[i][2], motion_boxes[i][3], 1.2)
            for i in unused_motion_boxes]

        # compute tracked object regions
        object_regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.2)
            for a in areas_of_interest]

        # merge regions with high IOU
        merged_regions = motion_regions+object_regions
        while True:
            max_iou = 0.0
            max_indices = None
            region_indices = range(len(merged_regions))
            for a, b in itertools.combinations(region_indices, 2):
                iou = intersection_over_union(merged_regions[a], merged_regions[b])
                if iou > max_iou:
                    max_iou = iou
                    max_indices = (a, b)
            if max_iou > 0.1:
                a = merged_regions[max_indices[0]]
                b = merged_regions[max_indices[1]]
                merged_regions.append(calculate_region(frame_shape,
                    min(a[0], b[0]),
                    min(a[1], b[1]),
                    max(a[2], b[2]),
                    max(a[3], b[3]),
                    1
                ))
                del merged_regions[max(max_indices[0], max_indices[1])]
                del merged_regions[min(max_indices[0], max_indices[1])]
            else:
                break

        # resize regions and detect
        detections = []
        for region in merged_regions:

            tensor_input = create_tensor_input(frame, region)

            region_detections = object_detector.detect(tensor_input)

            for d in region_detections:
                box = d[2]
                size = region[2]-region[0]
                x_min = int((box[1] * size) + region[0])
                y_min = int((box[0] * size) + region[1])
                x_max = int((box[3] * size) + region[0])
                y_max = int((box[2] * size) + region[1])
                det = (d[0],
                    d[1],
                    (x_min, y_min, x_max, y_max),
                    (x_max-x_min)*(y_max-y_min),
                    region)
                if filtered(det, objects_to_track, object_filters, mask):
                    continue
                detections.append(det)

        #########
        # merge objects, check for clipped objects and look again up to N times
        #########
        refining = True
        refine_count = 0
        while refining and refine_count < 4:
            refining = False

            # group by name
            detected_object_groups = defaultdict(lambda: [])
            for detection in detections:
                detected_object_groups[detection[0]].append(detection)

            selected_objects = []
            for group in detected_object_groups.values():

                # apply non-maxima suppression to suppress weak, overlapping bounding boxes
                boxes = [(o[2][0], o[2][1], o[2][2]-o[2][0], o[2][3]-o[2][1])
                    for o in group]
                confidences = [o[1] for o in group]
                idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

                for index in idxs:
                    obj = group[index[0]]
                    if clipped(obj, frame_shape):
                        box = obj[2]
                        # calculate a new region that will hopefully get the entire object
                        region = calculate_region(frame_shape,
                            box[0], box[1],
                            box[2], box[3])

                        tensor_input = create_tensor_input(frame, region)
                        # run detection on new region
                        refined_detections = object_detector.detect(tensor_input)
                        for d in refined_detections:
                            box = d[2]
                            size = region[2]-region[0]
                            x_min = int((box[1] * size) + region[0])
                            y_min = int((box[0] * size) + region[1])
                            x_max = int((box[3] * size) + region[0])
                            y_max = int((box[2] * size) + region[1])
                            det = (d[0],
                                d[1],
                                (x_min, y_min, x_max, y_max),
                                (x_max-x_min)*(y_max-y_min),
                                region)
                            if filtered(det, objects_to_track, object_filters, mask):
                                continue
                            selected_objects.append(det)

                        refining = True
                    else:
                        selected_objects.append(obj)

            # set the detections list to only include top, complete objects
            # and new detections
            detections = selected_objects

            if refining:
                refine_count += 1

        # now that we have refined our detections, we need to track objects
        object_tracker.match_and_update(frame_time, detections)

        # add to the queue
        detected_objects_queue.put((name, frame_time, object_tracker.tracked_objects))

    print(f"{name}: exiting subprocess")


# %%
import random

def main():
    bot_name = "Deeple"
    instruction = f'The following is conversation between an Deeple assistant called {bot_name}, and a human user called User. {bot_name} is intelligent, knowledgeable, wise and polite.'
    with open('filelist/v2/inst_v1_th_en_train.txt') as f:
        data = f.read()
        deeple_data = data.split('<|endoftext|>')
        total_data = []
        for d in deeple_data:
            total_data.append(f'{instruction}\n\n{d}')
    with open('filelist/v2/inst_v1_th_en_train_instruct.txt', 'w') as w:
        for row in total_data:
            w.write(f'{row}<|endoftext|>')

    with open('filelist/v2/inst_v1_th_en_test.txt') as f:
        data = f.read()
        deeple_data = data.split('<|endoftext|>')
        total_data = []
        for d in deeple_data:
            total_data.append(f'{instruction}\n\n{d}')

    with open('filelist/v2/inst_v1_th_en_test_instruct.txt', 'w') as w:
        for row in total_data:
            w.write(f'{row}<|endoftext|>')

if __name__ == '__main__':
    main()


import socket
import sys
import time
import datetime

sys.path.append(".\definitions")
from customer_def import customer
from retailer_def import retailer
from transaction_def import transaction
from product_def import product
from blockchain_def import blk_chain
from blockchain_def import block

customer1 = customer('customer1',10000)
chain_c1 = blk_chain()
rec = None
tranc = 0

while(1):
	s = socket.socket()
	print('press 1 to buy products, 0 to view previous transactions')
	t = input()
	if(t=='1'):
		port = 12345
		print('connecting to market...')
		time.sleep(2)
		s.connect(('127.0.0.1', port))
		print('connected to peer : (\'127.0.0.1\',{})'.format(port))
		print('Enter the product id')
		pid = input()
		print('Enter the quanitiy required')
		qty = input()
		senddet = 'reqt'+str(pid)+str(qty)
		s.send(bytes(senddet, 'utf-8'))
		x = str(s.recv(1024))
		print(x[2:len(x)-1])
		while(1):
			r = str(s.recv(1024))
			if(r[:5]=="b'yes"):
				print('Transaction successful')
				customer1.balance -= int(r[5:len(r)-1])
				ctime = datetime.datetime.now()
				transaction0 = transaction('customer1','retailer1',pid,qty,int(r[5:len(r)-1]),ctime)
				block0 = block(transaction0)
				if(tranc!=0):
					block0.prev = rec
				chain_c1.add_block(block0)
				rec = block0
				tranc += 1
				print('Disconnected')
				break
			else:
				print('Validation failed, try again later')
				break
		s.close()
	else:
		chain_c1.print_transaction()


import tkinter

root = tkinter.Tk()
root.title("MOYA GUI")
root.geometry("300x300")

def click():
    print("Button Clicked")

my_btn_image = tkinter.PhotoImage(file="01-GUI-Basic/image/test_btn.png")
btn6 = tkinter.Button(root, image=my_btn_image, command=click)
btn6.pack()

root.mainloop()


theBools = [0,1,0,0,1,1,1,0,0,1,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1]

#Your code go here:
def wikiOrWoko(string):
    if (string == 0):
        return "woko"
    else:
        return "wiki"
        # print(str ("wiki"))

new_list = list(map(wikiOrWoko,theBools))
print (new_list)


# -*- coding: utf-8 -*-
from telnetlib import EC

import pandas as pd
import openpyxl
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from openpyxl import Workbook

driver = webdriver.Chrome(executable_path='chromedriver')
driver.maximize_window()

write_wb = Workbook();
write_ws = write_wb.active

count = 0

url = 'https://aihub.or.kr/aihub-data/vision/about'
driver.get(url)
for k in range(8):
    for j in range(8):
        if j==0 :
            clicknum = 20
        elif j==1 :
            clicknum = 20
        elif j==2 :
            clicknum = 1
        elif j==3 :
            clicknum = 12
        elif j==4 :
            clicknum = 15
        elif j==5 :
            clicknum = 20
        elif j==6 :
            clicknum = 20
        elif j==7 :
            clicknum = 20
        for i in range(clicknum):
            count += 1
            # //*[@id="block-views-block-8dae-bunlyu-block-5"]/div/div/div/div/ul/li[1]
            # //*[@id="block-views-block-8dae-bunlyu-block-5"]/div/div/div/div/ul/li[48]
            xpathhead = '//*[@id="block-views-block-8dae-bunlyu-block-3"]/div/div/div/div/ul/li['
            xpathmiddle = str(i + 1)
            xpathtail = ']'
            xpath = xpathhead + xpathmiddle + xpathtail
            driver.find_element(By.XPATH,xpath).click()
            cururl = driver.current_url
            write_ws.cell(count, 1, cururl)
            print(count)
            driver.back()
        if  j== 0 :
            driver.find_element(By.XPATH,'//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[2]/a').click()
        elif j==1 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[3]/a').click()
        elif j==2 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[4]/a').click()
        elif j==3 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[5]/a').click()
        elif j==4 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[6]/a').click()
        elif j==5 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[7]/a').click()
        elif j==6 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[8]/a').click()

write_wb.save('AI_Url.xlsx')

# 다음 페이지 버튼 클릭


import io
import socket
import struct
import cv2
import numpy as np

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)

# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')

cap = cv2.VideoCapture(0)

try:
    while True:
        f,frame = cap.read()
        cv2.imshow('frame', frame)
        # Read the length of the image as a 32-bit unsigned int. If the
        # length is zero, quit the loop
        image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
        if not image_len:
            break

        image = np.fromstring(connection.read(image_len), dtype=np.uint8).reshape((480,640,3))
        image2 = image.copy()
        cv2.imshow('image', image2)

        if cv2.waitKey(10) != -1:
            connection.close()
            server_socket.close()
            cap.release()
            cv2.destroyAllWindows()

finally:
    connection.close()
    server_socket.close()
    cv2.destroyAllWindows()


#Ensemble Backend Project by I Ahmed

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os

#Initialize App

backend_app = Flask(__name__)
base_directory = os.path.abspath(os.path.dirname(__file__))

#Database
backend_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(base_directory, 'db.sqlite')
#backend_app['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# Initialize Database
db = SQLAlchemy(backend_app)

#initialize Marshmallow
ma = Marshmallow(backend_app)

# Movie Class (Object Creation)
class Movie(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    movie_title = db.Column(db.String(100), unique=True) #we dont want 2 movies with same name so unique = true
    description = db.Column(db.String(400))
    release_year = db.Column(db.Integer)
    duration = db.Column(db.Integer) #duration in minutes
    rating = db.Column(db.Float)
    likes = db.Column(db.Integer) #we start at 0 likes when we create a movie
    dislikes = db.Column(db.Integer) #we start at 0  dislikes when we create a movie

    def __init__(self, movie_title, description, release_year, duration, rating, likes, dislikes):

        self.movie_title = movie_title
        self.description = description
        self.release_year = release_year
        self.duration = duration
        self.rating = rating
        self.likes = likes
        self.dislikes = dislikes

# Movie Schema

class MovieSchema(ma.Schema):
    class Meta:
        fields = ('id', 'movie_title', 'description', 'release_year', 'duration', 'rating', 'likes', 'dislikes')

#Initialize Schema
movie_schema = MovieSchema()
movies_schema = MovieSchema(many=True) #if we are dealing with many movies(list)

#App routes

#Add or create a movie
@backend_app.route('/movie', methods=['POST'])
def add_movie():
    movie_title = request.json['movie_title']
    description = request.json['description']
    release_year = request.json['release_year']
    duration = request.json['duration']
    rating = request.json['rating']
    likes = request.json['likes']
    dislikes = request.json['dislikes']

    new_movie = Movie(movie_title, description, release_year, duration, rating, likes, dislikes)
    db.session.add(new_movie)
    db.session.commit()

    return movie_schema.jsonify(new_movie)

#Get all movies
@backend_app.route('/movie', methods=['GET'])
def get_movies():
    all_movies = Movie.query.all()
    return_result = movies_schema.dump(all_movies)
    return jsonify(return_result)

#Get single movie by using ID
@backend_app.route('/movie/<id>', methods=['GET'])
def get_movie(id):
    movie = Movie.query.get(id)
    return movie_schema.jsonify(movie)

#Search Movie by using Movie Title (query)
@backend_app.route('/movie/<movie_title>')
def show_movie_by_title(movie_title):
    movie = Movie.query.filter_by(movie_title=movie_title).first_or_404()
    return render_template('show_movie_by_title', movie=movie)

#update a movie info
@backend_app.route('/movie/<id>', methods=['PUT'])
def update_movie(id):
    movie = Movie.query.get(id)

    movie_title = request.json['movie_title']
    description = request.json['description']
    release_year = request.json['release_year']
    duration = request.json['duration']
    rating = request.json['rating']
    likes = request.json['likes']
    dislikes = request.json['dislikes']

    movie.movie_title = movie_title
    movie.description = description
    movie.release_year = release_year
    movie.duration = duration
    movie.rating = rating
    movie.likes = likes
    movie.dislikes = dislikes

    db.session.commit()

    return movie_schema.jsonify(movie)

#update likes
@backend_app.route('/movie/<id>', methods=['PUT'])
def update_likes(id):
    setattr(id, 'likes', id.likes + 1)
    db.session.commit()

#update dislikes
@backend_app.route('/movie/<id>', methods=['PUT'])
def update_dislikes(id):
    setattr(id, 'dislikes', id.dislikes + 1)
    db.session.commit()

#Delete a movie
@backend_app.route('/movie/<id>', methods=['DELETE'])
def delete_movie(id):
    movie = Movie.query.get(id)
    db.session.delete(movie)
    db.session.commit()

    return movie_schema.jsonify(movie)

#running server

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


import re
import streamlit as st
from send_email import send_email

st.header('Contact me')

with st.form(key='contact_form'):
    st.info('All fields are mandatory.')
    email = st.text_input('Your email address')
    subject = st.text_input('Enter subject').strip()
    details = st.text_area('Describe some details').strip()
    submit = st.form_submit_button()

    if submit:
        if re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email) is not None\
                and len(details) > 0 and len(subject) > 0:
            send_email(receiver=email, subject=subject, details=details)
            st.success("Form submitted successfully!")
        else:
            st.error('An error occured.')


import webbrowser
import json
from parse_plan import parse_plan
import click

from parse_idxs import find_identifiers
from dfs import DAG, get_leaf_to_filename
from run_notebook import get_raw_plan

# Spark Explain Output Related variables
FINAL_COLUMNS_HEAD = "AdaptiveSparkPlan"
URL = "https://www.beeline-track.ml/"
raw_plan_content = []
parsed_plan_content = []

# Dependency clauses to parse
AS_CLAUSE = " AS "
FILTERING_CLAUSES = ["Filter", "BroadcastHashJoin"]

# Graph-related variables
adj_map = {}

def initialize(is_explain: bool, file_path: str):
    global parsed_plan_content, raw_plan_content
    if is_explain:
        raw_plan = []
        explain_file = open(file_path, 'r')
        for line in explain_file:
            raw_plan.append(line)
    else:
        raw_plan = get_raw_plan(file_path)
    skip = True
    for line in raw_plan:
        if len(line) <= 1:
            skip = False
        if not skip:
            raw_plan_content.append(line)
    parsed_plan_content = parse_plan(raw_plan_content)

def add_as_clause_dependency(line: str):
    for i in range(0, len(line)-3):
        if line[i:i+4] == AS_CLAUSE:
            parent_name = find_identifiers(line[i:], True)
            if parent_name is None:
                continue
            if not(line[i+4:].strip().startswith(parent_name)):
                continue
            left = i
            bracket_depth = 0
            while not(left < 1 or (bracket_depth == 0 and line[left] in [',', '['])):
                if line[left] == ')':
                    bracket_depth += 1
                elif line[left] == '(':
                    bracket_depth -= 1
                left -= 1
            child_names = find_identifiers(line[left:i])
            if parent_name not in adj_map:
                adj_map[parent_name] = []
            for child in child_names:
                adj_map[parent_name].append(child)

def add_as_clause_deps():
    global raw_plan_content
    for line in raw_plan_content:
        add_as_clause_dependency(line)

def get_filtering_deps():
    filtering_deps = []
    for step in parsed_plan_content:
        if step["_name"] == "Filter":
            filtering_deps += find_identifiers(step["Condition"])
        elif step["_name"] == "BroadcastHashJoin":
            filtering_deps += find_identifiers(step["Left keys"])
            filtering_deps += find_identifiers(step["Right keys"])
            filtering_deps += find_identifiers(step["Join condition"])
    return list(set(filtering_deps))

def get_final_ids():
    final_ids = []
    for step in parsed_plan_content:
        if step["_name"] == "AdaptiveSparkPlan":
            final_ids = find_identifiers(step["Output"])
            break
    return final_ids

@click.command()
@click.argument('file_path')
@click.option('--mode', type=click.Choice(['explain', 'script']), required=True)
def run(file_path, mode):
    initialize(mode == "explain", file_path)
    add_as_clause_deps()
    final_ids = get_final_ids()

    dag = DAG(adj_map)

    final_id_to_deps = dict()
    for id in final_ids:
        final_id_to_deps[id] = dag.get_deps(id)

    leaf_to_filename = get_leaf_to_filename(parsed_plan_content)

    common_leaf_deps = set()

    for id in get_filtering_deps():
        for leaf in dag.get_deps(id):
            common_leaf_deps.add(leaf)

    filtered_deps = set()
    filtered_source_names = set()

    for dep in common_leaf_deps:
        if dep not in leaf_to_filename:
                continue
        source_name = leaf_to_filename[dep]
        col_name = dep.split('#')[0]
        formatted_col_name = f"{source_name}.{col_name}"

        filtered_deps.add(formatted_col_name)
        filtered_source_names.add(source_name)

    result = {"non_trivial_cols_deps": list(filtered_deps)}
    result["non_trivial_data_sources"] = list(filtered_source_names)

    for id, deps in final_id_to_deps.items():
        col_names = set()
        source_names = set()
        for dep in set(deps):
            if dep not in leaf_to_filename:
                continue

            source_name = leaf_to_filename[dep]
            col_name = dep.split('#')[0]
            formatted_col_name = f"{source_name}.{col_name}"

            if dep in deps:
                col_names.add(formatted_col_name)

            source_names.add(source_name)

        result[id.split('#')[0]] = {
            "data_sources": list(source_names),
            "cols_deps": list(col_names)
        }

    pretty_json = json.dumps(result, indent=4)
    print(pretty_json)

    json_file = open("deps_graph.json", "w")
    json_file.write(pretty_json)
    json_file.close()

    webbrowser.open_new_tab(URL)

if __name__ == '__main__':
    run()