#encoding: utf-8
import sys
from collections import defaultdict

from config import candidates_cache_path, type_restrictions
from sparql_access import select_types, count_entities_of_type, select_entities_of_type_not_in_relation, full_type_name
from pickler import Pickler

class CandidatesSelector:
    #types that are too wide, cover too many entities and should never be returned
    wide_types = [
        'http://schema.org/Place',
        'http://www.w3.org/2002/07/owl#Thing',
        'http://dbpedia.org/ontology/Place',
        'http://dbpedia.org/ontology/NaturalPlace',
        'http://dbpedia.org/ontology/BodyOfWater',
        'http://www.opengis.net/gml/_Feature'
    ]

    @staticmethod
    def get_predominant_types(predicate, subject=True):
        if predicate in type_restrictions:
            return [full_type_name(type_restrictions[predicate])]
        #limiting these relations to settlements only increases precision a lot
        if predicate in ['stolica', 'gmina', 'region', 'prowincja', 'hrabstwo']:
            return [u'http://dbpedia.org/ontology/Settlement']
        type_preciseness = .75
        types_list = select_types(predicate, subject)
        types_count = defaultdict(int)
        for types in types_list:
            for type in types:
                if type not in CandidatesSelector.wide_types:
                    types_count[type] += 1
        num_entities = len(types_list)
        return [
            type for type, count in types_count.iteritems()
            if count >= type_preciseness * num_entities
        ]

    @staticmethod
    def get_most_specific_types(predominant_types):
        threshold = 100
        return [
            type for type in predominant_types
            if count_entities_of_type(type) > threshold
        ]

    @staticmethod
    def get_candidates(predicate):
        try:
            return Pickler.load(candidates_cache_path % predicate)
        except IOError:
            pass
        types = CandidatesSelector.get_most_specific_types(
            CandidatesSelector.get_predominant_types(predicate)
        )
        if types:
            candidates = select_entities_of_type_not_in_relation(
                types[0],
                predicate
            )
            if predicate == 'gmina':
                candidates = filter(lambda e: 'Gmina' not in e, candidates)
            if predicate == 'powiat':
                candidates = filter(lambda e: 'Powiat' not in e, candidates)
            if predicate == 'hrabstwo':
                candidates = filter(lambda e: 'hrabstwo_miejskie' not in e and 'Hrabstwo' not in e, candidates)
            Pickler.store(candidates, candidates_cache_path % predicate)
            return candidates
        else:
            return []


# IMPORTANDO E TRABALHANDO COM DADOS NO PANDAS

import pandas as pd
import numpy as np

# Ler arquivo csv com dados de peso dos estudantes de escolas dos U.S.
dados = pd.read_excel('Students_Weight.xls')

# observar as cinco primeiras linhas do conjunto de dados
primeirosDados = dados.head()

# Acessar colunas
acimaDoPeso = dados[['GRADE LEVEL', 'NO. OVERWEIGHT', 'PCT OVERWEIGHT', 'NO. OBESE', 'PCT OBESE',
                     'NO. OVERWEIGHT OR OBESE', 'PCT OVERWEIGHT OR OBESE',
                     'SCHOOL YEARS', 'COUNTY', 'CITY', 'STREET ADDRESS', 'ZIP CODE']]

# observar as cinco últimas linhas do conjunto de dados
ultimosDados = dados.tail()

#Dados nulos
acimaDoPeso.isnull()

#Contar dados nulos por coluna
acimaDoPeso[['NO. OVERWEIGHT OR OBESE', 'PCT OVERWEIGHT OR OBESE']].isnull()

acimaDoPeso['NO. OVERWEIGHT OR OBESE'].isnull().value_counts()

# Excluir linhas onde COUNTY é nulo.
acimaDoPeso = acimaDoPeso.dropna(subset=['NO. OVERWEIGHT OR OBESE'])
acimaDoPeso = acimaDoPeso.dropna(subset=['NO. OVERWEIGHT'])
acimaDoPeso = acimaDoPeso.dropna(subset=['NO. OBESE'])

# Criar df menor para ver melhor a substituição dos dados vazios
# *********************************************************************************************************
x = np.random.randn(10,6)*10
df = pd.DataFrame(x, index=['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10'],
                  columns=['x', 'y', 'z', 'j', 'q', 'k'])

df = df.reindex(['a1', 'a2', 'b1', 'a3', 'a4', 'b2', 'a5', 'a6', 'b3',
                 'a7', 'a8', 'b4', 'a9', 'a10', 'b5', 'b6'])
df['x'].isnull().value_counts()

# Substituir valorez NA por zero, ou selecionar valor por coluna inserindo um dicionário {'x':0}
df2 = df.fillna(0)

# Substituir propagando o valor para frente
df3 = df.fillna(method='pad')

# Substituir pela média
df4 = df.fillna(df.mean())

# Trabalhar com Correlação: três opções (‘pearson’, ‘kendall’, ‘spearman’)
cor = df4.corr()
# *********************************************************************************************************

# Contar distribuição dos dados
acimaDoPeso['SCHOOL YEARS'].value_counts()
acimaDoPeso['COUNTY'].value_counts()
acimaDoPeso['COUNTY'].value_counts().sort_index()
acimaDoPeso['COUNTY'].unique()

## AGRUPAMENTO - Realizar operações com conjuntos agrupados
# (Separa os dados, aplica uma função e combina os resultados)

## Importante fazer a pergunta certa...
# Quantas vezes uma máquina parou? Quais instrumentos dão mais problemas em um ano?

group1 = acimaDoPeso.groupby(['SCHOOL YEARS', 'GRADE LEVEL'])

#OPÇÃO DE SELECIONAR COLUNAS
group2 = acimaDoPeso[['NO. OVERWEIGHT', 'NO. OBESE']].groupby([acimaDoPeso['SCHOOL YEARS'],
                    acimaDoPeso['GRADE LEVEL']]).sum()

group3 = acimaDoPeso[['NO. OVERWEIGHT', 'NO. OBESE']].groupby([acimaDoPeso['SCHOOL YEARS'],
                    acimaDoPeso['GRADE LEVEL']]).mean()

group4 = acimaDoPeso[['NO. OVERWEIGHT', 'NO. OBESE']].groupby([acimaDoPeso['SCHOOL YEARS'],
                    acimaDoPeso['GRADE LEVEL']]).median()

group2.plot.barh(title='Obesos ou acima do peso')
group2[['NO. OVERWEIGHT']].plot.barh(title='Acima do peso')


import os
from glob import glob

import numpy as np
from PIL import Image

import torch
from torchvision.utils import save_image
from torch.utils.tensorboard import SummaryWriter

from utils.grid import create_grid
from models.siren import SirenModel

'''
    픽셀 수 스케일링 해보기
'''

EXP_NAME = 'learn-multi'
DATA_ROOT = 'inputs/balloons_multiscale'

W0 = 50
MAX_ITERS = 2000
LR = 1e-4

SCALE = 10
MAPPING_SIZE = 256

if __name__ == '__main__':
    os.makedirs(f'exps/{EXP_NAME}/img', exist_ok=True)
    os.makedirs(f'exps/{EXP_NAME}/ckpt', exist_ok=True)
    os.makedirs(f'exps/{EXP_NAME}/logs', exist_ok=True)
    writer = SummaryWriter(f'exps/{EXP_NAME}/logs')

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model = SirenModel(coord_dim=2, num_c=3, w0=W0).to(device)
    # B_gauss = torch.randn((MAPPING_SIZE, 2)).to(device) * SCALE

    grids = []
    reals = []
    real_paths = sorted(glob(f'{DATA_ROOT}/*'))
    for idx, p in enumerate(real_paths):
        img = Image.open(p).convert('RGB')
        img = np.array(img) / 255.
        img = torch.FloatTensor(img).to(device)
        h, w, c = img.shape

        os.makedirs(f'exps/{EXP_NAME}/img/{idx}', exist_ok=True)
        grid = create_grid(h, w, device=device)
        grids.append(grid)
        reals.append(img)

    optim = torch.optim.Adam(model.parameters(), lr=LR)
    loss_fn = torch.nn.MSELoss()

    for step in range(MAX_ITERS):
        for idx, (real, grid) in enumerate(zip(reals, grids)):
            model.train()
            optim.zero_grad()

            # x_proj = (2. * np.pi * grid) @ B_gauss.t()
            # mapped_input = torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1)

            pred = model(grid)
            loss = loss_fn(pred, real)

            loss.backward()
            optim.step()

            print(f'{idx}|{step}: loss:{loss.item()}')
            writer.add_scalar(f"{idx}_loss", loss.item(), step)

            if (step+1) % 50 == 0:
                pred = pred.permute(2, 0, 1)
                save_image(pred, f'exps/{EXP_NAME}/img/{idx}/{step}.jpg')

    torch.save(model.state_dict(), f'exps/{EXP_NAME}/ckpt/final.pth')


def bubble_sort(l):
    for i in range(len(l)-1,-1,-1):
        flag = 0
        for j in range(i):
            if l[j+1] < l[j]:
                l[j+1],l[j] = l[j],l[j+1]
                flag = 1
        if flag ==0:
            return l
    return l

def insert_sort(l):
    for i in range(len(l)):
        for j in range(i,0,-1):
            if l[j-1] > l[j]:
                l[j-1],l[j] = l[j],l[j-1]
            else:
                break
    return l

def quick_sort(l):
    left = 0
    right = len(l)-1
    return q_sort(l,left,right)

def q_sort(l,left,right):
    if left<right:
        pivot = partition(l,left,right)
        q_sort(l,0,pivot-1)
        q_sort(l,pivot+1,right)
    return l

def partition(l,left,right):
    pivot = l[left]
    while left < right:
        while left < right and l[right] >= pivot:
            right -= 1
        l[left] = l[right]
        while left < right and l[left] <= pivot:
            left += 1
        l[right] = l[left]
    l[left] = pivot
    return left

def split_merge_sort(l):
    if len(l) == 1:return l
    mid = len(l) // 2
    left = split_merge_sort(l[:mid])
    right = split_merge_sort(l[mid:])
    return merge(left,right)

def merge(left,right):
    i = j = 0
    res = []
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            res.append(left[i])
            i += 1
        else:
            res.append(right[j])
            j += 1
    res += left[i:] or right[j:]
    return res

l1 = [6,5,4,3,2,1,1,8,8,9,2,5,1,7,6,4,3,1,8,8,9,2,5]
l2 = [6,5,4,3,2,1,1,8,8,9,2,5,1,7,6,4,3,1,8,8,9,2,5]
l3 = [6,5,4,3,2,1,1,8,8,9,2,5,1,7,6,4,3,1,8,8,9,2,5]
l4 = [6,5,4,3,2,1,1,8,8,9,2,5,1,7,6,4,3,1,8,8,9,2,5]
print(quick_sort(l1))
print(insert_sort(l2))
print(bubble_sort(l3))
print(split_merge_sort(l4))


def calculate_change(payment, cost):
    change = payment - cost

    fifty_count = change // 50000
    ten_count = (change % 50000) // 10000
    five_count = (change % 10000) // 5000
    one_count = (change % 5000) // 1000

    print("50000원 지폐: {}장".format(fifty_count))
    print("10000원 지폐: {}장".format(ten_count))
    print("5000원 지폐: {}장".format(five_count))
    print("1000원 지폐: {}장".format(one_count))

calculate_change(100000, 33000)
print()
calculate_change(500000, 378000)


import wave
import pyaudio
from array import array
import librosa
import soundfile
import pandas as pd
import os
import glob
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
# from soundfile import Sample_rate
from tqdm import tqdm
from scipy.io import wavfile

# vars
emotions = {
    '01': 'neutral',
    '02': 'calm',
    '03': 'happy',
    '04': 'sad',
    '05': 'angry',
    '06': 'fearful',
    '07': 'disgust',
    '08': 'surprise'
}
observed_emotions = ['calm', 'happy', 'fearful', 'disgust']

# functions

def envelope(y, rate, threshold):
    mask = []
    y = pd.Series(y).apply(np.abs)
    y_mean = y.rolling(window=int(rate/10), min_periods=1, center=True).mean()
    for mean in y_mean:
        if mean > threshold:
            mask.append(True)
        else:
            mask.append(False)
    return mask

def extract_feature(file_name, mfcc, chroma, mel):
    with soundfile.SoundFile(file_name) as sound_file:
        X, sample_rate = librosa.load(file_name)
        # X = sound_file.read(dtype="float32")
        # sample_rate = sound_file.samplerate
        if chroma:
            stft = np.abs(librosa.stft(X))
        result = np.array([])
        if mfcc:
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate,
                                                 n_mfcc=40).T, axis=0)
            result = np.hstack((result, mfccs))
        if chroma:
            chroma = np.mean(librosa.feature.chroma_stft(S=stft,
                                                         sr=sample_rate).T, axis=0)
            result = np.hstack((result, chroma))
        if mel:
            mel = np.mean(librosa.feature.melspectrogram(X,
                                                         sr=sample_rate).T, axis=0)
            result = np.hstack((result, mel))
    return result

def load_data(test_size=0.33):
    print("Loading Data")
    x, y = [], []
    for file in tqdm(glob.glob(r'C:\Users\shivangi shukla\Documents\Projects\minor project\speech-emotion-recognition-ravdess-data\\**\\*.wav')):
        file_name = os.path.basename(file)
        signal, rate = librosa.load(file, sr=16000)
        mask = envelope(signal, rate, 0.0005)
        clean_file_name = r'C:\Users\shivangi shukla\Documents\Projects\minor project\clean_audio\\'+str(file_name)
        wavfile.write(filename=r'C:\Users\shivangi shukla\Documents\Projects\minor project\clean_audio\\'+str(file_name),
                      rate=rate, data=signal[mask])
        emotion = emotions[clean_file_name.split("-")[2]]
        if emotion not in observed_emotions:
            continue
        feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
        x.append(feature)
        y.append([emotion, file_name])
    return train_test_split(np.array(x), y, test_size=test_size, random_state=9)

x_train, x_test, y_train, y_test = load_data(test_size=0.25)

print(np.shape(x_train), np.shape(x_test), np.shape(y_train), np.shape(y_test))

y_test_map = np.array(y_test).T
y_test = y_test_map[0]
test_filename = y_test_map[1]
y_train_map = np.array(y_train).T
y_train = y_train_map[0]
train_filename = y_train_map[1]

# print(np.shape(y_train),np.shape(y_test))
# print(*test_filename,sep="\n")

print(f'Features extracted:{x_train.shape[1]}')

model = MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08,
                      hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)

model.fit(x_train, y_train)

y_pred = model.predict(x_test)
y_pred = pd.Series(y_pred)
y_pred

accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))

y_pred1 = pd.DataFrame(y_pred, columns=['preedictions'])
y_pred1['file_names'] = test_filename
y_pred1.to_csv('prediction.csv')

Pkl_Filename = "Speech_Emotion_Recognition_Model.pkl"

with open(Pkl_Filename, 'wb') as file:
    pickle.dump(model, file)

with open(Pkl_Filename, 'rb') as file:
    speech_recognition = pickle.load(file)

speech_recognition

print("Starting voice record...")
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 6
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)
frames = []

check = 0

for i in tqdm(range(0, int(RATE/CHUNK * RECORD_SECONDS))):
    data = stream.read(CHUNK)
    data_chunk = array('h', data)
    vol = max(data_chunk)
    if(vol >= 500):
        check = 1
        frames.append(data)

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

if(check == 1):
    data,  sampling_rate = librosa.load(WAVE_OUTPUT_FILENAME)
    # %matplotlib inline
    import matplotlib.pyplot as plt
    import librosa.display

    plt.figure(figsize=(15, 5))
    librosa.display.waveplot(data, sr=sampling_rate)

    file = WAVE_OUTPUT_FILENAME
    ans = []
    new_feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
    ans.append(new_feature)
    ans = np.array(ans)

    # Speech_Emotion_Recognition_Model.predict([ans])
    pred = model.predict(ans)
    pred = pd.Series(pred)
    print(pred[0])
else:
    print("Nothing Recorded")


import requests
import json
import pandas as pd

class PolygonAllTickers:
    def __init__(self, api_key, active="true", response_limit=1000, market="stocks"):
        self.api_key = api_key
        self.active = active
        self.response_limit = response_limit
        self.market = market
        self._ticker_data = []
        self.base_query = f"https://api.polygon.io/v3/reference/tickers?market={market}&active={active}&sort=ticker&order=asc&limit={response_limit}"
        self.and_api_key = f"&apiKey={api_key}"

    def get_ticker_names(self):
        ticker_list = []
        for ticker_data in self._ticker_data:
            for tickers in ticker_data["results"]:
                ticker_list.append(tickers["ticker"])
        return ticker_list

    def get_company_names(self):
        company_list = []
        for ticker_data in self._ticker_data:
            for tickers in ticker_data["results"]:
                company_list.append(tickers["name"])
        return company_list

    def get_ticker_company_tuple(self):
        data = []
        for ticker_data in self._ticker_data:
            for ticks in ticker_data["results"]:
                data.append((ticks["ticker"], ticks["name"]))
        return data

    def fetch_tickers_data(self):
        query = self.base_query
        continue_fetching = True
        while continue_fetching:
            result = requests.get(query + self.and_api_key).json()
            self._ticker_data.append(result)
            print("Fetching")
            try:
                query = result["next_url"] + self.and_api_key
            except Exception as e:
                continue_fetching = False
                print("Fetched")
        return self

def update_ticker_list_file(api_key):
    tickers = PolygonAllTickers(api_key=api_key)
    tickers.fetch_tickers_data()
    df = pd.DataFrame(columns=["Name", "symbol"])
    df["Name"] = tickers.get_company_names()
    df["symbol"] = tickers.get_ticker_names()
    df.to_csv("../app_data/company_tickers.csv", index=False)

if __name__ == "__main__":
    api_key = ""
    update_ticker_list_file(api_key)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0008_auto_20170822_1942'),
    ]

    operations = [
        migrations.AlterField(
            model_name='horsebasictemp',
            name='perundp',
            field=models.DecimalField(verbose_name='\u6bcf\u80a1\u672a\u5206', max_digits=7, decimal_places=4),
        ),
    ]


import networkx as nx
import random
import csv

dic = {'PUN':[], 'REST':[], 'ApVt':[], 'HyR': []}

class CPM(nx.DiGraph):
    def __init__(self):
        super().__init__()
        self._dirty = True
        self._makespan = -1
        self._criticalPath = None

    def add_node(self, *args, **kwargs):
        self._dirty = True
        super().add_node(*args, **kwargs)

    def add_nodes_from(self, *args, **kwargs):
        self._dirty = True
        super().add_nodes_from(*args, **kwargs)

    def add_edge(self, *args):  # , **kwargs):
        self._dirty = True
        super().add_edge(*args)  # , **kwargs)

    def add_edges_from(self, *args, **kwargs):
        self._dirty = True
        super().add_edges_from(*args, **kwargs)

    def remove_node(self, *args, **kwargs):
        self._dirty = True
        super().remove_node(*args, **kwargs)

    def remove_nodes_from(self, *args, **kwargs):
        self._dirty = True
        super().remove_nodes_from(*args, **kwargs)

    def remove_edge(self, *args):  # , **kwargs):
        self._dirty = True
        super().remove_edge(*args)  # , **kwargs)

    def remove_edges_from(self, *args, **kwargs):
        self._dirty = True
        super().remove_edges_from(*args, **kwargs)

    def _forward(self):
        for n in nx.topological_sort(self):
            S = max([self.node[j]['C']
                     for j in self.predecessors(n)], default=0)
            self.add_node(n, S=S, C=S + self.node[n]['p'])

    def _backward(self):
        for n in nx.topological_sort(self, reverse=True):
            Cp = min([self.node[j]['Sp']
                      for j in self.successors(n)], default=self._makespan)
            self.add_node(n, Sp=Cp - self.node[n]['p'], Cp=Cp)

    def _computeCriticalPath(self):
        G = set()
        for n in self:
            if self.node[n]['C'] == self.node[n]['Cp']:
                G.add(n)
        self._criticalPath = self.subgraph(G)

    @property
    def makespan(self):
        if self._dirty:
            self._update()
        return self._makespan

    @property
    def criticalPath(self):
        if self._dirty:
            self._update()
        return self._criticalPath

    def _update(self):
        self._forward()
        self._makespan = max(nx.get_node_attributes(self, 'C').values())
        self._backward()
        self._computeCriticalPath()
        self._dirty = False

def generate_nodes(processes):
    """genera un conjunto de nodos sin conexiones
    processes: lista de tuplas de la forma (id proceso, tiempo)"""
    graph_nodes = CPM() # creamos un nuevo objeto grafo
    for process in processes: # creamos los nodos con su id y tiempo
        graph_nodes.add_node(process[0], p=process[1])
    return graph_nodes

def generate_random_solution(graph_nodes, no_processes, no_requests):
    """generamos una solucion aleatoria agregando arcos a los nodos creados
    graph_nodes: grafo que contiene los nodos sin conexion
    no_processes: cantidad de procesos
    no_requests: cantidad de pedidos"""
    # obtenemos lista en orden de los nodos
    nodes = graph_nodes.nodes()
    # creamos lista de listas vacias para organizar el orden de los procesos en comun
    node_list = [[] for _ in range(no_processes)]
    # separamos los nodos de procesos que pertenecen a cada pedido
    adjacency_list = [nodes[i:i + no_processes] for i in range(0, len(nodes), no_processes)]
    # creamos los arcos para unir los nodos en el orden establecido por el proceso
    for lst in adjacency_list:
        for i in range(len(lst)-1):
            graph_nodes.add_edges_from([(lst[i], lst[i+1])])
    # agrupamos los nodos que pertenecen a la misma operacion para asignarlas al azar
    nodes = graph_nodes.nodes()
    for i in range(len(node_list)):
        for j in range(i, len(nodes), no_processes):
            node_list[i].append(nodes[j])
    # asignar el orden de uso de cada estacion aleatoriamente
    for common_processes in node_list:
        random.shuffle(common_processes)
        for i in range(len(common_processes)-1):
            graph_nodes.add_edges_from([(common_processes[i], common_processes[i+1])])
    # regresamos el grafo con precedencias
    return graph_nodes, node_list

def perturbate_solution(graph, node_list, patch_size):
    """creamos una configuracion similar a la actual
    graph: el bjeto que representa al grafo
    node_list: configuracion de la solucion; lista con el orden de los procesos
    path_size: 'cantidad' de variabilidad"""
    # aplicambios patch_size cantidad de cambios
    while patch_size > 0:
        # seleccionamos un proceso aleatorio para modificar su orden
        selected_process = random.randint(0, len(node_list)-1)
        # obtenemos los nodos involucrados en el proceso seleccionado
        nodes = node_list[selected_process]
        #print(node_list)
        # creamos una lista de tuplas con esos nodos para eliminar sus conexiones
        tup_list = []
        for i in range(len(nodes)-1):
            tup_list.append((nodes[i], nodes[i+1]))
        # eliminamos las conexiones entre esos nodos
        graph.remove_edges_from(tup_list)
        # generamos un nuevo orden aleatorio
        random.shuffle(nodes)
        # creamos las conexiones nuevas en base al nuevo orden aleatorio
        for i in range(len(nodes)-1):
            graph.add_edges_from([(nodes[i], nodes[i+1])])
        patch_size -= 1
        # reemplazamos el orden de una operacion anterior con la actual
        node_list[selected_process] = nodes
    return graph, node_list

def read():
    inp = input("Introduzca el nombre del archivo: ")
    with open(inp) as f:
        reader = csv.reader(f, delimiter="\t")
        firstline = True
        contPun = 0
        contRest = 0
        contApVt = 0
        contHyR = 0
        contID = 0
        lista = []
        for i in reader:
            if firstline:
                firstline = False
            else:
                a = i[0].split(";");
                if(a[0] == 'pedido'):
                    contID += 1
                    lista.append((contID,contPun))
                    dic['PUN'].append(contID)
                    contID += 1
                    lista.append((contID,contRest))
                    dic['REST'].append(contID)
                    contID += 1
                    lista.append((contID,contApVt))
                    dic['ApVt'].append(contID)
                    contID += 1
                    lista.append((contID,contHyR))
                    dic['HyR'].append(contID)
                    contPun = 0
                    contRest = 0
                    contApVt = 0
                    contHyR = 0
                else:
                    listaAux = []
                    listaAux.extend([int(a[3]),int(a[10]),int(a[13])])
                    maximum = max(listaAux)
                    contPun = contPun + (int(a[0]) * maximum)
                    listaAux = []
                    listaAux.extend([int(a[2]),int(a[4]),int(a[5]),int(a[9]),int(a[11]),int(a[12]),int(a[14])])
                    maximum = max(listaAux)
                    contRest = contRest + (int(a[0]) * maximum)
                    listaAux = []
                    listaAux.extend([int(a[6]),int(a[7])])
                    maximum = max(listaAux)
                    contApVt = contApVt + (int(a[0]) * maximum)
                    contHyR = contHyR + (int(a[0]) * int(a[8]))
    return lista

def write(lista_orden):
    counter = 0
    listaNUEVA = []
    listaPRUEBA = lista_orden
    listaPruebaPUN = listaPRUEBA[0]
    listaPruebaREST = listaPRUEBA[1]
    listaPruebaApVt = listaPRUEBA[2]
    listaPruebaHyR = listaPRUEBA[3]

    listaNuevaPUN = []
    listaNuevaPUN.append('PUN')
    for idCorr in listaPruebaPUN:
        counter = 0
        for id_proceso in dic['PUN']:
            if idCorr == id_proceso:
                desp = 'Pedido ' + str(counter+1)
                listaNuevaPUN.append(desp)
                break
            counter +=1

    listaNuevaREST = []
    listaNuevaREST.append('REST')
    for idCorr in listaPruebaREST:
        counter = 0
        for id_proceso in dic['REST']:
            if idCorr == id_proceso:
                desp = 'Pedido ' + str(counter+1)
                listaNuevaREST.append(desp)
                break
            counter +=1

    listaNuevaApVT = []
    listaNuevaApVT.append('ApVt')
    for idCorr in listaPruebaApVt:
        counter = 0
        for id_proceso in dic['ApVt']:
            if idCorr == id_proceso:
                desp = 'Pedido ' + str(counter+1)
                listaNuevaApVT.append(desp)
                break
            counter +=1

    listaNuevaHyR = []
    listaNuevaHyR.append('HyR')
    for idCorr in listaPruebaHyR:
        counter = 0
        for id_proceso in dic['HyR']:
            if idCorr == id_proceso:
                desp = 'Pedido ' + str(counter+1)
                listaNuevaHyR.append(desp)
                break
            counter +=1

    listaNUEVA.append(listaNuevaPUN)
    listaNUEVA.append(listaNuevaREST)
    listaNUEVA.append(listaNuevaApVT)
    listaNUEVA.append(listaNuevaHyR)
    cnt = 0
    for index in listaNUEVA:
        print(listaNUEVA[cnt])
        cnt += 1

# CLEVER ALGORITHM: Bees Algorithm
# Author: Santiago E. Conant-Pablos, October 6, 2015

def objective_function(graph):
    """returns value of function to optimize"""
    return graph.makespan

def create_random_bee(graph, no_processes, no_orders):
    """create a random bee position"""
    return generate_random_solution(graph, no_processes, no_orders)

def create_neigh_bee(graph, node_list, patch_size):
    """create a bee inside a neighborhood"""
    return perturbate_solution(graph, node_list, patch_size)

def search_neigh(parent, neigh_size, patch_size, node_list):
    """search inside the neighborhood of a site"""
    neigh = []
    for i in range(neigh_size):
        bee = list(create_neigh_bee(parent, node_list, patch_size))
        if len(bee) < 3: # si no se le ha asignado un fitness hay que hacerlo
            bee.append(objective_function(bee[0]))
        else: # de lo contrario solo hay que reemplazarlo
            bee[2] = objective_function(bee[0])
        neigh.append(bee)
    neigh.sort(key=lambda b: b[2])
    return neigh[0]

def create_scout_bees(requests, no_processes, no_orders, num_scouts):
    """creates scout bees for new sites"""
    return [create_random_bee(generate_nodes(requests), no_processes, no_orders) for i in range(num_scouts)]

def bees_algorithm(max_gens, requests, no_processes, num_bees, num_sites,
                   elite_sites, patch_size, patch_dec, e_bees, o_bees):
    """implements the Bees algorithm"""
    # el mejor encontrado
    best = None
    # creacion de una poblacion de soluciones aleatorias
    no_requests = int(len(requests)/no_processes)
    # pop[i] = (graph, solution, fitness); fitness se agrega en el for interno
    pop = [create_random_bee(generate_nodes(
        requests), no_processes, no_requests
    ) for i in range(num_bees)]
    # corre la optimizacion en terminos de generaciones
    for gen in range(max_gens):
        print_flag = False
        for bee in range(num_bees):
            pop[bee] = list(pop[bee])
            if len(pop[bee]) < 3:
                pop[bee].append(objective_function(pop[bee][0]))
            else:
                pop[bee][2] = objective_function(pop[bee][0])
        pop.sort(key = lambda b: b[2])
        if not best or pop[0][2] < best[2]:
            print_flag = True
            best = pop[0]
        next_gen = []
        for i, parent in enumerate(pop[:num_sites]):
            neigh_size = e_bees if i < elite_sites else o_bees
            next_gen.append(search_neigh(parent[0], neigh_size, patch_size, parent[1]))

        scouts = create_scout_bees(requests, no_processes, no_requests, num_bees - num_sites)
        pop = next_gen + scouts
        patch_size = patch_size * patch_dec
        if print_flag : print(" > it=%d, patch_size=%g, f=%g" % (gen+1,patch_size,best[2]))
    return best

# algorithm configuration
max_gens = 100 # maximun number of generations
num_bees = 45
num_sites = 3
elite_sites = 1
patch_size = 3.0
patch_dec = 0.95 # decrease of patch size in each generation
e_bees = 7    # number of elite bees
o_bees = 2    # number of other bees
no_jobs = 4 # cantidad de operaciones para ensamblaje (incluye varias en paralelo)
requests = read()#[(1,50),(2,3),(3,41),(4,94),(5,15),(6,13),(7,34),(8,29),(9,55),(10,73),(11,14),(12,91)]
best = bees_algorithm(max_gens, requests, no_jobs, num_bees, num_sites,
                      elite_sites, patch_size, patch_dec, e_bees, o_bees)
print("Done.\nBest Solution: f=%g" % (best[2]))
write(best[1])


import sys
import socket
import select
import os
import time

"Use this method to write Packet log"
def writePkt(logFile, procTime, pktNum, event):
    logFile.write('{:1.3f} pkt: {} | {}'.format(procTime, pktNum, event))

"Use this method to write ACK log"
def writeAck(logFile, procTime, ackNum, event):
    logFile.write('{:1.3f} ACK: {} | {}'.format(procTime, ackNum, event))

"Use this method to write final throughput log"
def writeEnd(logFile, throughput, avgRTT):
    logFile.write('File transfer is finished.')
    logFile.write('Throughput : {:.2f} pkts/sec'.format(throughput))
    logFile.write('Average RTT : {:.1f} ms'.format(avgRTT))

def fileSender():
    print('sender program starts...')
    srcFile = open(srcFilename, 'rb')
    sender_log = open(srcFilename + '_sending_log.txt', 'a')
    packet_list = []
    data = srcFile.read(1400)
    while data:
        packet_list.append(data)
        data = srcFile.read(1400)
    num_packets = len(packet_list)

    sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sckt.bind(('', 10080))

    en_dstFilenamedst = dstFilename.encode()
    sckt.sendto((num_packets.to_bytes(4, byteorder='big') + windowSize.to_bytes(4, byteorder='big') + en_dstFilenamedst), (recvAddr, 10080))

    base = 0
    send_num = 0
    rtt = 0
    timeout = 1
    start_time = time.time()
    flag = 0
    while base < num_packets:
        send_num = base
        while send_num < base + windowSize:
            sckt.sendto((flag.to_bytes(1, byteorder='big') + send_num.to_bytes(4, byteorder='big') + packet_list[send_num]), (recvAddr, 10080))
            flag = 0
            go_time = time.time() - start_time
            writePkt(sender_log, go_time, send_num, "send\n")
            send_num = send_num + 1
            if send_num >= num_packets:
                break

        ready = select.select([sckt], [], [], timeout)
        if ready[0]:
            ack = sckt.recv(65565)
        else:
            flag = 1
            writeAck(sender_log, go_time, base, "timeout\n")
            continue

        ack = int.from_bytes(ack, byteorder='big')
        base = ack
        arr_time = time.time() - start_time
        writeAck(sender_log, arr_time, base, "received\n")
        rtt += arr_time - go_time
    rtt = rtt * 1000 /(num_packets/windowSize)
    writeEnd(sender_log,  num_packets/(time.time()-start_time), rtt)

if __name__=='__main__':
    recvAddr = sys.argv[1]  #receiver IP address
    windowSize = int(sys.argv[2])   #window size
    srcFilename = sys.argv[3]   #source file name
    dstFilename = sys.argv[4]   #result file name

    fileSender()


"""Models for learning functional distances from videos."""

import functools
from typing import Any, Dict, Optional, Tuple

import flax.linen as nn
from immutabledict import immutabledict
import jax
import jax.numpy as jnp
import ml_collections
from scenic.model_lib.base_models import base_model
from scenic.model_lib.base_models import model_utils as base_model_utils
from scenic.model_lib.base_models import regression_model
from scenic.model_lib.layers import nn_layers
from scenic.projects.baselines import simple_cnn
from scenic.projects.baselines import resnet
from scenic.projects.func_dist import model_utils
from scenic.projects.func_dist import holdc_model
from scenic.projects.vivit import model as vivit_model

def get_model_cls(model_name):
  """"Selects distance model type."""
  if model_name == 'vivit_multilabel_classification':
    return vivit_model.ViViTMultilabelClassificationModel
  elif model_name == 'vivit_classification':
    return vivit_model.ViViTClassificationModel
  elif model_name == 'vivit_multihead_classification':
    return vivit_model.ViViTMultiHeadClassificationModel
  elif model_name == 'vivit_regression':
    return ViViTRegressionModel
  elif model_name == 'simple_cnn_regression':
    return SimpleCNNRegressionModel
  elif model_name == 'resnet_regression':
    return ResnetRegressionModel
  elif model_name == 'resnet_holdc':
    return holdc_model.ResnetTcModel
  elif model_name == 'vit_holdc':
    return holdc_model.VitTcModel
  else:
    raise ValueError('Unrecognized model: {}'.format(model_name))

_REGRESSION_METRICS = immutabledict({
    'mean_absolute_error':
        (base_model_utils.weighted_absolute_error,
         base_model_utils.num_examples),
    'mean_squared_error':
        (base_model_utils.weighted_squared_error,
         base_model_utils.num_examples)
})
_TEMPORAL_REGRESSION_METRICS = immutabledict({
    'mean_absolute_error_seconds':
        (base_model_utils.weighted_absolute_error,
         base_model_utils.num_examples),
    'mean_squared_error_seconds':
        (base_model_utils.weighted_squared_error,
         base_model_utils.num_examples),
    'mean_absolute_error_steps':
        (base_model_utils.weighted_absolute_error,
         base_model_utils.num_examples),
    'mean_squared_error_steps':
        (base_model_utils.weighted_squared_error,
         base_model_utils.num_examples),
     **_REGRESSION_METRICS
})
_FULL_SEQUENCE_METRICS = immutabledict({
    'spearman_correlation':
        (model_utils.pmapped_spearman_correlation,
         holdc_model.pmapped_num_videos),
    'hinge_loss':
        (model_utils.hinge_loss,
         model_utils.num_frames_sub_one),
    'misclassification_rate':
        (model_utils.binary_misclassified_count,
         model_utils.num_frames_sub_one),
    **_TEMPORAL_REGRESSION_METRICS
})

def temporal_regression_metrics_function(
    predictions: jnp.ndarray,
    batch: base_model.Batch,
    metrics: base_model.MetricNormalizerFnDict = _TEMPORAL_REGRESSION_METRICS,
    predict_seconds: bool = False,
    mean_fps: float = 12.,
    split: str = 'valid',
    pmapped: bool = True,
) -> Dict[str, Tuple[float, int]]:
  """Calculate metrics for the regression task.

  Currently we assume each metric_fn has the API:
    ```metric_fn(predictions, targets, weights)```
  and returns an array of shape [batch,]. We also assume that to compute
  the aggregate metric, one should sum across all batches, then divide by the
  total samples seen. In this way we currently only support metrics of the 1/N
  sum f(inputs, targets). Note, the caller is responsible for dividing by
  the normalizer when computing the mean of each metric.

  Args:
   predictions: Output of model in shape [batch, length].
   batch: Batch (dict) with keys 'targets' and optionally 'batch_mask'.
   metrics: The regression metrics to evaluate. The key is the
     name of the  metric, and the value is the metrics function.

  Returns:
    A dict of metrics, in which keys are metrics name and values are tuples of
    (metric, normalizer).
  """
  if split == 'test':
    targets_steps = batch['targets']
    targets_seconds = targets_steps / batch['frame_rate'][0]
  else:
    targets_steps = batch['targets']
    targets_seconds = batch['targets_seconds']
  weights = batch.get('batch_mask')
  frame_rate = batch['frame_rate']
  evaluated_metrics = {}
  for key, val in metrics.items():
    if key in ['mean_absolute_error_seconds', 'mean_squared_error_seconds',
               'hinge_loss']:
      targets = targets_seconds
      # If preds are in steps, divide by frame rate.
      preds = predictions if predict_seconds else predictions / frame_rate
    else:
      targets = targets_steps
      if key in ['mean_absolute_error_steps', 'mean_squared_error_steps']:
        # Multiply by frame rates of individual videos.
        preds = (predictions * frame_rate if predict_seconds
                else predictions * frame_rate / mean_fps)
      else:
        # Multiply by the average frame rate only.
        preds = predictions * mean_fps if predict_seconds else predictions
    metric_normalizer = (base_model_utils.psum_metric_normalizer if pmapped
                         else model_utils.sum_metric_normalizer)
    evaluated_metrics[key] = metric_normalizer(
        (val[0](preds, targets, weights), val[1](preds, targets, weights)))
  return evaluated_metrics

def full_sequence_metrics_function(
    predictions: jnp.ndarray,
    batch: base_model.Batch,
    metrics: base_model.MetricNormalizerFnDict = _FULL_SEQUENCE_METRICS,
) -> Dict[str, Tuple[float, int]]:
  weights = batch.get('batch_mask')
  evaluated_metrics = {}
  targets = jnp.transpose(batch['targets'])[:-1]
  for key, val in metrics.items():
    evaluated_metrics[key] = base_model_utils.psum_metric_normalizer(
        (val[0](predictions, targets, weights),
         val[1](predictions, targets, weights)))
  return evaluated_metrics

class TemporalRegressionModel(regression_model.RegressionModel):

  def __init__(self, config, dataset_meta_data):
    self.predict_seconds = config.get('predict_seconds', False)
    self.dataset_mean_fps = dataset_meta_data.get('mean_frame_rate', 12.)
    self.loss_type = config.get('loss_type', 'mse')
    super().__init__(config, dataset_meta_data)

    if self.predict_seconds:
      print('Predicting in seconds')
    else:
      print('Predicting in time steps')
    print('Dataset mean fps:', self.dataset_mean_fps)

  def get_metrics_fn(self, split: Optional[str] = None) -> base_model.MetricFn:
    """Returns a callable metric function for the model.

    Args:
      split: The split for which we calculate the metrics. It should be one
        of the ['train',  'validation', 'test'].
    Returns: A metric function with the following API: ```metrics_fn(logits,
      label, weights)```
    """
    # del split  # for all splits, we return the same metric functions

    additional_metrics = immutabledict({
        'pairwise_hinge_loss':
        (model_utils.pairwise_hinge_loss,
         model_utils.num_frames_div_two),
        })
    metrics = (_FULL_SEQUENCE_METRICS if split == 'test'
               else {**_TEMPORAL_REGRESSION_METRICS, **additional_metrics})

    return functools.partial(
        temporal_regression_metrics_function,
        metrics=metrics,
        predict_seconds=self.predict_seconds,
        mean_fps=self.dataset_mean_fps,
        split=split)

  def loss_function(self,
                    predictions: jnp.ndarray,
                    batch: base_model.Batch,
                    model_params: Optional[jnp.ndarray] = None) -> float:
    """Returns the (weighted) mean squared error.

    Args:
      predictions: Output of model in shape [batch, length].
      batch: Batch (dict) with keys 'targets' and optionally 'batch_mask'.
      model_params: Parameters of the model, for optionally applying
        regularization.

    Returns:
      The (weighted) mean squared error.
    """
    weights = batch.get('batch_mask')
    if self.predict_seconds:
      targets = batch['targets_seconds']
    else:
      targets = batch['targets']
      # Adjust for variable frame rates.
      predictions = predictions * batch['frame_rate'] / self.dataset_mean_fps

    if self.loss_type == 'hinge_loss':
      total_loss = model_utils.pairwise_hinge_loss(
        predictions, targets, weights)
    else:
      total_loss = base_model_utils.weighted_mean_squared_error(
          predictions, targets, weights)
    if self.config.get('l2_decay_factor'):
      l2_loss = base_model_utils.l2_regularization(model_params)
      total_loss += 0.5 * self.config.l2_decay_factor * l2_loss
    return total_loss

class ResnetRegressionModel(TemporalRegressionModel):

  def build_flax_model(self) -> nn.Module:
    model_dtype = getattr(jnp, self.config.get('model_dtype_str', 'float32'))
    return resnet.ResNet(
        num_outputs=self.dataset_meta_data['num_targets'],
        num_filters=self.config.num_filters,
        num_layers=self.config.num_layers,
        dtype=model_dtype)

class SimpleCNNRegressionModel(TemporalRegressionModel):

  def build_flax_model(self) -> nn.Module:
    model_dtype = getattr(jnp, self.config.get('model_dtype_str', 'float32'))
    return simple_cnn.SimpleCNN(
        num_outputs=self.dataset_meta_data['num_targets'],
        output_projection_type=self.config.model.output_projection_type,
        num_filters=self.config.model.num_filters,
        kernel_sizes=self.config.model.kernel_sizes,
        use_bias=self.config.model.use_bias,
        dtype=model_dtype,
    )

class MultiOutputSpaceTimeViViT(vivit_model.SpaceTimeViViT):
  """Modified SpaceTimeViViT which returns both prediction and prelogits."""

  @nn.compact
  def __call__(self, x: jnp.ndarray, *, train: bool, debug: bool = False):

    del debug
    x, _ = vivit_model.temporal_encode(
        x, self.temporal_encoding_config, self.patches, self.hidden_size,
        return_1d=False)
    bs, t, h, w, c = x.shape
    x = x.reshape(bs, t, h * w, c)

    def vit_body(x, mlp_dim, num_layers, num_heads, encoder_name='Transformer'):
      # If we want to add a class token, add it here.
      if self.classifier in ['token']:
        n, _, c = x.shape
        cls = self.param(f'cls_{encoder_name}', nn.initializers.zeros,
                         (1, 1, c), x.dtype)
        cls = jnp.tile(cls, [n, 1, 1])
        x = jnp.concatenate([cls, x], axis=1)

      x = vivit_model.Encoder(
          temporal_dims=0,  # This is unused for Factorised-Encoder
          mlp_dim=mlp_dim,
          num_layers=num_layers,
          num_heads=num_heads,
          attention_config=self.attention_config,
          dropout_rate=self.dropout_rate,
          attention_dropout_rate=self.attention_dropout_rate,
          stochastic_droplayer_rate=self.stochastic_droplayer_rate,
          dtype=self.dtype,
          name=encoder_name)(x, train=train)

      if self.classifier in ['token', '0']:
        x = x[:, 0]
      elif self.classifier in ('gap', 'gmp', 'gsp'):
        fn = {'gap': jnp.mean, 'gmp': jnp.max, 'gsp': jnp.sum}[self.classifier]
        x = fn(x, axis=list(range(1, x.ndim - 1)))
      return x

    # run attention across spacec, per frame
    x = jax.vmap(
        functools.partial(
            vit_body,
            mlp_dim=self.spatial_mlp_dim,
            num_layers=self.spatial_num_layers,
            num_heads=self.spatial_num_heads,
            encoder_name='SpatialTransformer'),
        in_axes=1,
        out_axes=1,
        axis_name='time')(
            x)
    assert x.ndim == 3 and x.shape[:2] == (bs, t)

    # run attention across time, over all frames
    if not self.attention_config.get('spatial_only_baseline', False):
      x = vit_body(
          x,
          mlp_dim=self.temporal_mlp_dim,
          num_layers=self.temporal_num_layers,
          num_heads=self.temporal_num_heads,
          encoder_name='TemporalTransformer')
    else:
      # Do global average pooling instead, as method of combining temporal info.
      x = jnp.mean(x, axis=list(range(1, x.ndim - 1)))

    if self.representation_size is not None:
      x = nn.Dense(self.representation_size, name='pre_logits')(x)
      x = nn.tanh(x)
    else:
      x = nn_layers.IdentityLayer(name='pre_logits')(x)

    return_values = tuple()
    if self.return_prelogits:
      return_values = (x,) + return_values
    x = nn.Dense(
        self.num_classes,
        kernel_init=nn.initializers.zeros,
        name='output_projection')(x)
    if return_values:
      return (x,) + return_values
    else:
      return x

class ViViTRegressionModel(TemporalRegressionModel):
  """Video Transformer model for regression."""

  def build_flax_model(self) -> nn.Module:
    model_dtype = getattr(jnp, self.config.get('model_dtype_str', 'float32'))
    attention_type = self.config.model.attention_config.get(
        'type', 'spacetime')
    if attention_type in [
        'spacetime', 'factorized_transformer_block',
        'factorized_self_attention_block', 'factorized_dot_product_attention'
    ]:
      return vivit_model.ViViT(
          num_classes=self.dataset_meta_data['num_targets'],
          mlp_dim=self.config.model.mlp_dim,
          num_layers=self.config.model.num_layers,
          num_heads=self.config.model.num_heads,
          representation_size=self.config.model.representation_size,
          patches=self.config.model.patches,
          hidden_size=self.config.model.hidden_size,
          temporal_encoding_config=self.config.model.temporal_encoding_config,
          attention_config=self.config.model.attention_config,
          classifier=self.config.model.classifier,
          dropout_rate=self.config.model.get('dropout_rate', 0.1),
          attention_dropout_rate=self.config.model.get(
              'attention_dropout_rate', 0.1),
          stochastic_droplayer_rate=self.config.model.get(
              'stochastic_droplayer_rate', 0),
          return_prelogits=self.config.model.get('return_prelogits', False),
          return_preclassifier=self.config.model.get(
              'return_preclassifier', False),
          dtype=model_dtype,
      )
    elif attention_type == 'factorized_encoder':
      return MultiOutputSpaceTimeViViT(
          num_classes=self.dataset_meta_data['num_targets'],
          spatial_mlp_dim=self.config.model.spatial_transformer.mlp_dim,
          spatial_num_layers=self.config.model.spatial_transformer.num_layers,
          spatial_num_heads=self.config.model.spatial_transformer.num_heads,
          temporal_mlp_dim=self.config.model.temporal_transformer.mlp_dim,
          temporal_num_layers=self.config.model.temporal_transformer
          .num_layers,
          temporal_num_heads=self.config.model.temporal_transformer.num_heads,
          representation_size=self.config.model.representation_size,
          patches=self.config.model.patches,
          hidden_size=self.config.model.hidden_size,
          temporal_encoding_config=self.config.model.temporal_encoding_config,
          attention_config=self.config.model.attention_config,
          classifier=self.config.model.classifier,
          dropout_rate=self.config.model.get('dropout_rate', 0.1),
          attention_dropout_rate=self.config.model.get(
              'attention_dropout_rate', 0.1),
          stochastic_droplayer_rate=self.config.model.get(
              'stochastic_droplayer_rate', 0),
          return_prelogits=self.config.model.get('return_prelogits', False),
          dtype=model_dtype,
      )
    else:
      raise ValueError(f'Attention type {attention_type} does not exist.')

  def init_from_train_state(self,
                            train_state: Any,
                            restored_train_state: Any,
                            restored_model_cfg: ml_collections.ConfigDict,
                            restore_output_proj: bool = False) -> Any:
    """Updates the train_state with data from restored_train_state."""
    attention_type = self.config.model.attention_config.get(
        'type', 'spacetime')
    if attention_type in [
        'spacetime', 'factorized_transformer_block',
        'factorized_self_attention_block', 'factorized_dot_product_attention'
    ]:
      vivit_transformer_key = 'Transformer'
    elif attention_type == 'factorized_encoder':
      vivit_transformer_key = 'SpatialTransformer'
    else:
      raise ValueError(f'Attention type {attention_type} does not exist.')
    return model_utils.initialise_from_train_state(
        self.config,
        train_state,
        restored_train_state,
        restored_model_cfg,
        restore_output_proj,
        vivit_transformer_key=vivit_transformer_key)


#!/usr/bin/python3

import os
import re
import sys
from copy import deepcopy
from argparse import ArgumentParser

# ========== MARKDOWN FUNCTIONS ========== #

def generic_parse_string(string):
  # generic parser for header string and para strings
  # returns {
  #   "text": <clean text>,
  #   "spans": [{
  #     "start": <int>,
  #     "end": <int>,
  #     "type": ,
  #     "link": <link/None>
  #   }]
  # }
  string = re.sub('\s+', " ", string.strip())
  target_string = deepcopy(string)

  # first extract bold
  for x in re.findall(r"\*{2}((<|>|\'|\"|\w|\s)*)\*{2}", string):
    seq = x[0].strip()
    repl = f"{seq}"
    target_string = target_string.replace(f"**{seq}**", repl)

  # then extract italics
  for x in re.findall(r"\*((<|>|\'|\"|\w|\s)*)\*", string):
    seq = x[0].strip()
    repl = f"{seq}"
    target_string = target_string.replace(f"*{seq}*", repl)

  # then extract strikethroughs
  for x in re.findall(r"~+((<|>|\'|\"|\w|\s)*)~+", string):
    seq = x[0].strip()
    repl = f"{seq}"
    target_string = target_string.replace(f"~~{seq}~~", repl)

  # then replace Links
  for x in re.findall(r"\[(.*)\]\((.*)\)", string):
    cap = x[0].strip()
    url = x[1].strip()
    repl = f'{cap}'
    seq = f"[{cap}]({url})"
    target_string = target_string.replace(seq, repl)

  # inline code
  for x in re.findall(r"\`([^\`].*?)\`", string):
    repl = f'{x.strip()}'
    target_string = target_string.replace(f"`{x}`", repl)

  return {"text": target_string}

def parse_lines(text):
  # this function takes in the raw file string and returns the sections
  # in form of a list. Each object in the list has the following format:
  # {
  #   "type": "p",
  #   "content": "Well this is some BS paragraph about how India is poor",
  #   "links": [{"start": 4, "end": 5, "link": "some/nice/link.html"}]
  # }
  #
  # the way to go about it is as follows:
  # - get the code that is present in ```...``` (codeblock order below)
  #   and remove that from the string because it can cause problems later
  # - split the text into sections by "\n\n"
  # - parse this text and determine the types
  sections = []

  codeblocks = re.split("```", text)
  if len(codeblocks) > 1:
    # ``` has some problem in regex, so manually built this
    assert (len(codeblocks)-1) % 2 == 0, "codeblocks not correct, missed ```?"
    codeblocks = [f"```{codeblocks[i]}```" for i in range(1, len(codeblocks), 2)]
    for c in codeblocks:
      text = text.replace(c, "<|codeblock|>")

  # splitting by \n\n+ also remove accidental multiple newlines
  simple_splits = re.split("\n\n+", text)
  code_cntr = 0
  for x in simple_splits:
    x = x.strip()
    # clause for code that has been removed above
    if x == "<|codeblock|>":
      text = codeblocks[code_cntr].replace("`", "").strip()
      sections.append({"type": "codeblock", "text": text})
      code_cntr += 1
      continue

    # is this a header?
    header = re.match(r"^#+\s(.*)$", x)
    if header is not None:
      parsed_string = generic_parse_string(header.group(1))
      header_level = len(re.findall(r"#", x))
      sections.append({"type":f"h{header_level}", **parsed_string})
      continue

    # is this a list?
    list_items = re.findall(r"\n(-|\*)", x)
    if list_items:
      list_items = re.split(r"\n(-|\*)", x)
      if list_items[0][0] not in ['-', '*']:
        parsed_string = generic_parse_string(list_items[0])
        sections.append({"type":"p", **parsed_string})
        list_items = list_items[1:]
      for i, item in enumerate(list_items):
        if i % 2 == 0:
          continue
        parsed_string = generic_parse_string(item)
        sections.append({"type":"li", **parsed_string})
      continue

    # is this an image or some link?
    image_link = re.match(r"!\[((.|\n)*)\]\((.*)\)", x)
    if image_link is not None:
      parsed_string = generic_parse_string(image_link.groups()[0])
      _weblink = image_link.groups()[-1]
      sections.append({"type": "figure", "src": _weblink})
      # sections.append({"type": "figcaption", "text": get_figcaption(parsed_string, _weblink)})
      sections.append({"type":"figcaption", **parsed_string, "url": _weblink})
      continue

    # is this a quote?
    if x[:2] == "> ":
      x = re.sub("\n?>+", "\n", x)
      x = generic_parse_string(x)
      sections.append({"type":"blockquote", **x})
      continue

    # if it is not a codeblock/quote/list/link/header then it's a para
    x = generic_parse_string(x)
    sections.append({"type": "p", **x})

  return sections

# ============ HTML FUNCTIONS ============ #

def get_header(title):
  return f"""
  <meta charset="utf-8">

  <title>{title}</title>

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link href="https://fonts.googleapis.com/css?family=Poppins:100,400,700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=IBM+Plex+Serif:300,400,400i,600,600i,700,700i,800" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,400i,700,700i" rel="stylesheet">

  <link href="styles/reset.css" rel="stylesheet">
  <link href="styles/-debug.css" rel="stylesheet">
  <link href="styles/article.css" rel="stylesheet">
  <link href="styles/article-text.css" rel="stylesheet">
  <link href="styles/article-figure.css" rel="stylesheet">
  <link href="styles/nav.css" rel="stylesheet">
  <link href="styles/code-block.css" rel="stylesheet">
  <link href="styles/footer-blog.css" rel="stylesheet">
"""

def get_time_string():
  from datetime import datetime
  d = datetime.now()
  _d = d.strftime("%d-%m-%Y")
  _m = {
    "01": "JAN.",
    "02": "FEB",
    "03": "MAR",
    "04": "APR",
    "05": "MAY",
    "06": "JUNE",
    "07": "JULY",
    "08": "AUG",
    "09": "SEP",
    "10": "OCT",
    "11": "NOV",
    "12": "DEC"
  }[d.strftime("%m")]
  _d_str = f"{_m} {d.strftime('%d')} {d.strftime('%Y')}"
  return f'<time datetime="{_d}">{_d_str}</time>'

def get_footer():
  return """

        CONNECT WITH ME

"""

def get_body(sections):
  # first get the correct section codes
  section_codes = []
  for x in sections:
    if x["type"] not in ["codeblock", "figcaption", "figure"]:
      code = f"""<{x["type"]}>
  {x["text"]}
</{x["type"]}>"""

    elif x["type"] == "codeblock":
      code = f"""

  {x["text"]}

"""

    elif x["type"] == "figcaption":
      text = x["text"]
      if "https" in x["url"]:
        to_add = ""
        if text[-1] != ".":
          to_add += "."
        text += f'{to_add} From here'
      figcaption = f"""

    {text}

"""
      code = figcaption

    elif x["type"] == "figure":
      code = f"""

"""

    section_codes.append(code)

  # combine get body
  start_header = "\n".join(section_codes[:2])
  time_string = get_time_string()
  start_header += f"\n{time_string}"

  body = "\n\n".join(section_codes[2:])
  body = f"""
      HOME
      PROJECTS
      BLOG
      RESUME

    {start_header}

    {body}

"""
  return body

def convert_to_html(sections):
  # this function takes in the parsed sections from parse_lines(...) func
  # and stitches together a webpage as a single string.
  title = list(filter(lambda x: "h" in x["type"], sections))[0]
  header = get_header(title["text"])
  body = get_body(sections)
  footer = get_footer()

  return f"""<!DOCTYPE html>

    {header}

    {body}

    {footer}

"""

if __name__ == "__main__":
  args = ArgumentParser(description="Converts markdown to my HTML code")
  args.add_argument("input", type = str, help = "markdown file path")
  args.add_argument("output", type = str, help = "Output filename")
  args = args.parse_args()

  inpath_full = os.path.abspath(args.input)
  here = os.path.dirname(os.path.abspath(__file__))
  blogs_folder = os.path.join(here, "blogs")
  output_file = os.path.join(blogs_folder, args.output)
  if not output_file.endswith(".html"):
    output_file += ".html"

  print("Reading file:", inpath_full)
  print("Output file:", output_file)

  # open the markdown file -> parse -> stitch -> save
  with open(inpath_full, "r") as f:
    lines = f.read()
  parsed_data = parse_lines(lines)
  html_str = convert_to_html(parsed_data)
  with open(output_file, "w") as f:
    f.write(html_str)


# This program requires the files to be in the same directory as the python file.
# The python file will overwrite any files that result from the conversion of ':' to '-'.
# The program first gets a list of strings that are all the files in the directory.
# The program then iterates through the list and checks for file names with ':' in them.
# The program then replaces ':' with '-' and copies the old file with the new filename.
# Program is made to run with python 3.6 or higher due to print statement.

import os
import shutil

currDir = os.getcwd()

dirList = os.listdir(currDir)

print(" ")
#print(currDir)
#print(dirList)

numAttempted = 0;
numChanged = 0;

for i in dirList:
    #print(':' in i)
    numAttempted = numAttempted + 1
    if (':' in i):
        #print("True")
        #print(i.replace(":", "-"))
        shutil.copyfile(i, i.replace(":", "-"))
        numChanged = numChanged + 1
    #else:
        #print("False")
        #print(i)
print("Completed. Changed ", numChanged, " out of ", numAttempted, " files in local folder.")


# https://www.acmicpc.net/problem/1520
# 백준 1520 : 내리막 길
# LEVEL : Gold 3

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def solution():
    direction = [[-1, 0], [1, 0], [0, -1], [0, 1]]

    M, N = map(int, input().split()) # 세로크기, 가로크기
    area = [list(map(int, input().split())) for _ in range(M)]

    dp = [[-1 for _ in range(N)] for _ in range(M)]

    def dfs(y, x):
        if y == M-1 and x == N-1:
            return 1

        if dp[y][x] != -1:
            return dp[y][x]

        dp[y][x] = 0

        for dy, dx in direction:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < M and 0 <= nx < N and area[ny][nx] < area[y][x]:
                dp[y][x] += dfs(ny, nx)

        return dp[y][x]

    print(dfs(0, 0))

solution()


from datasets import load_dataset
from transformers import AutoTokenizer

def get_average_lengths(dataset, tokenizer):
    lengths = []
    for example in dataset:
        # example["question_answer"] = f'{example["question"]} {example["answer"]}'
        inputs = tokenizer(
            example["excerpt"],
            truncation=True,
            padding="max_length",
        )
        lengths.append(sum(inputs["attention_mask"]))
    average_length = sum(lengths) / len(lengths)
    return average_length

# Load the MultIRC dataset from SuperGLUE
dataset = load_dataset("nlp-thedeep/humset", "1.0.0")

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")

# # Get the average lengths of the samples for each split
train_avg_length = get_average_lengths(dataset["train"], tokenizer)
dev_avg_length = get_average_lengths(dataset["validation"], tokenizer)
test_avg_length = get_average_lengths(dataset["test"], tokenizer)

for split in ["train", "validation", "test"]:
    # print average length of columns sectors, pillars_1d, pillars_2d, subpillars_1d, subpillars_2d
    print(f"Average length of {split} split:")
    for column in ["sectors", "pillars_1d", "pillars_2d", "subpillars_1d", "subpillars_2d"]:
        content = dataset[split][column]
        lengths = [len(c) for c in content]
        average_length = sum(lengths) / len(lengths)
        # avg, min, max
        print(f"{column}: {average_length}, {min(lengths)}, {max(lengths)}")

# Print the average lengths for each split
print("Average Lengths:")
print("Train Split:", train_avg_length)
print("Dev Split:", dev_avg_length)
print("Test Split:", test_avg_length)


# 5356. 의석이의 세로로 말해요

# Solution by wYuYw

T = int(input())

for test_case in range(1, T + 1):
    matrix = []
    for r in range(5):
        matrix.append(list(input()))

    max_length = 0
    for r in matrix:
        if len(r) > max_length:
            max_length = len(r)

    for r in matrix:
        while len(r) < max_length:
            r.append(0)

    result = ''
    for c in range(max_length):
        for r in range(5):
            if matrix[r][c] != 0:
                result += matrix[r][c]

    print(f"#{test_case} {result}")


from typing import Tuple

from pyKook.Api.eventAPI.baseEventHandler import baseEventHandler as baseEventHandler
from pyKook.Api.objects import User as User
from pyKook.Utils.loggingTool import logging

class cardButtonClickMessageHandler(baseEventHandler):
    def __init__(self):
        super().__init__()
        self._event = "message_btn_click"

    def handle(self, msg: dict, bot_info: User) -> Tuple[dict, str]:
        logging.info("Card button clicked.")
        logging.info(
            "User {} clicked at card {} on button {}.".format(
                msg["extra"]["body"]["user_id"],
                msg["extra"]["body"]["msg_id"],
                msg["extra"]["body"]["value"],
            )
        )
        msg["author_id"] = msg["extra"]["body"]["user_id"]  # 采用触发事件者的身份替代系统
        msg["channel_id"] = msg["extra"]["body"]["target_id"]  # 标识位置，便于后续处理

        return msg, "cardClicked.{}.{}".format(
            msg["extra"]["body"]["msg_id"], msg["extra"]["body"]["value"]
        )