import numpy as np
import re
import matplotlib.pyplot as plt
from scipy import stats

# Define regular expressions for extracting relevant information
pod_results_pattern = re.compile(r'Pod Results(.+?)Pod Creation Success Rate: ([\d.]+) %', re.DOTALL)
deployment_results_pattern = re.compile(r'Deployment Results(.+?)Pod Creation Success Rate: ([\d.]+) %', re.DOTALL)
namespace_results_pattern = re.compile(r'Namespace Results(.+?)Service API Call Latencies \(ms\)', re.DOTALL)
namespace_results_pattern1 = re.compile(r'Namespace Results(.+?)Benchmark run completed.', re.DOTALL)
service_results_pattern = re.compile(r'Service Results(.+?)Benchmark run completed.', re.DOTALL)

pod_creation_throughput = []
pod_client_server_e2e_latency = []
pod_scheduling_latency_stats = []
pod_initialization_latency_kubelet = []
pod_starting_latency_stats = []
pod_creation_avg_latency = []
pod_startup_total_latecy = []

ficheiro = "cp_light_4client"

pods = {
    'Pod Creation Throughput': [],
    'Pod Client-Server E2E Latency': [],
    'Pod Scheduling Latency Stats': [],
    'Pod Initialization Latency (Kubelet)': [],
    'Pod Starting Latency Stats': [],
    'Pod Creation Avg Latency': [],
    'Pod Startup Total Latency': [],
}
pod_api = {

    "Delete Latency": [],
    "Create Latency": [],
    "List Latency": [],
    "Get Latency": [],
    "Update Latency": []
}
service_api = {

    "Delete Latency": [],
    "Create Latency": [],
    "List Latency": [],
    "Get Latency": [],
    "Update Latency": []
}

deployment_api = {

    "Delete Latency": [],
    "Create Latency": [],
    "List Latency": [],
    "Get Latency": [],
    "Update Latency": []
}

namespace_api = {

    "Delete Latency": [],
    "Create Latency": [],
    "List Latency": [],
    "Get Latency": [],
    "Update Latency": []
}
deployments = {
    'Pod Creation Throughput': [],
    'Pod Client-Server E2E Latency': [],
    'Pod Scheduling Latency Stats': [],
    'Pod Initialization Latency (Kubelet)': [],
    'Pod Starting Latency Stats': [],
    'Pod Creation Avg Latency': [],
    'Pod Startup Total Latency': [],
}

# Function to extract and print the relevant information
def extract_results(log_content):
    pod_results_match = pod_results_pattern.search(log_content)
    deployment_results_match = deployment_results_pattern.search(log_content)
    namespace_results_match = namespace_results_pattern.search(log_content)
    namespace_results_match1 = namespace_results_pattern1.search(log_content)
    service_results_match = service_results_pattern.search(log_content)

    if pod_results_match:
        pod_results_content = pod_results_match.group(1).strip()
        pod_success_rate = pod_results_match.group(2).strip()
        # print("Pod Results:")
        # print(pod_results_content)
        lista = pod_results_content.split("\n")
        for i in lista:
            if "Pod creation throughput (pods/minutes):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                # print(float(linha[6]))
                pods['Pod Creation Throughput'].append(float(linha[6]))

            elif "Pod creation average latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                # print(float(linha[6]))
                pods['Pod Creation Avg Latency'].append(float(linha[6]))

            elif "Pod startup total latency (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pods['Pod Startup Total Latency'].append(l)

            elif "Pod client-server e2e latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pods['Pod Client-Server E2E Latency'].append(l)

            elif "Pod scheduling latency stats (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pods['Pod Scheduling Latency Stats'].append(l)

            elif "Pod initialization latency on kubelet (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pods['Pod Initialization Latency (Kubelet)'].append(l)

            elif "Pod starting latency stats (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pods['Pod Starting Latency Stats'].append(l)

            elif "delete pod latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pod_api['Delete Latency'].append(l)
            elif "create pod latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pod_api['Create Latency'].append(l)
            elif "list pod latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pod_api['List Latency'].append(l)
            elif "get pod latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pod_api['Get Latency'].append(l)
            elif "update pod latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                pod_api['Update Latency'].append(l)

        print(f"Pod Success Rate: {pod_success_rate}%")

    if deployment_results_match:
        deployment_results_content = deployment_results_match.group(1).strip()
        deployment_success_rate = deployment_results_match.group(2).strip()
        # print("\nDeployment Results:")
        # print(deployment_results_content)
        print(f"Deployment Success Rate: {deployment_success_rate}%")

        lista = deployment_results_content.split("\n")
        for i in lista:
            if "Pod creation throughput (pods/minutes):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                # print(float(linha[6]))
                deployments['Pod Creation Throughput'].append(float(linha[6]))

            elif "Pod creation average latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                # print(float(linha[6]))
                deployments['Pod Creation Avg Latency'].append(float(linha[6]))

            elif "Pod startup total latency (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployments['Pod Startup Total Latency'].append(l)

            elif "Pod client-server e2e latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployments['Pod Client-Server E2E Latency'].append(l)

            elif "Pod scheduling latency stats (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployments['Pod Scheduling Latency Stats'].append(l)

            elif "Pod initialization latency on kubelet (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployments['Pod Initialization Latency (Kubelet)'].append(l)

            elif "Pod starting latency stats (client):" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployments['Pod Starting Latency Stats'].append(l)

            elif "delete deployment latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployment_api['Delete Latency'].append(l)
            elif "create deployment latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployment_api['Create Latency'].append(l)
            elif "list deployment latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployment_api['List Latency'].append(l)
            elif "get deployment latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployment_api['Get Latency'].append(l)
            elif "update deployment latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                deployment_api['Update Latency'].append(l)

    if namespace_results_match or namespace_results_match1:
        if namespace_results_match:
            namespace_results_content = namespace_results_match.group(1).strip()
        else:
            namespace_results_content = namespace_results_match1.group(1).strip()

        # print("\nNamespace Results:")
        lista = namespace_results_content.split("\n")
        for i in lista:
            if "delete namespace latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                namespace_api['Delete Latency'].append(l)
            elif "create namespace latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                namespace_api['Create Latency'].append(l)
            elif "list namespace latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                namespace_api['List Latency'].append(l)
            elif "get namespace latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                namespace_api['Get Latency'].append(l)
            elif "update namespace latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                namespace_api['Update Latency'].append(l)

    if service_results_match:
        service_results_content = service_results_match.group(1).strip()
        # print("\nService Results:")
        # print(service_results_content)
        lista = service_results_content.split("\n")
        for i in lista:
            if "delete service latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                service_api['Delete Latency'].append(l)
            elif "create service latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                service_api['Create Latency'].append(l)
            elif "list service latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                service_api['List Latency'].append(l)
            elif "get service latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                service_api['Get Latency'].append(l)
            elif "update service latency" in i:
                linha = re.split(":|\t|\"", i)
                # print(linha)
                numeros = linha[6].split()
                # print(numeros)
                l = []
                for j in numeros:
                    l.append(float(j))
                service_api['Update Latency'].append(l)

# Função para calcular o intervalo de confiança
def calcular_intervalo_confianca(dados, alpha):
    media = np.mean(dados)
    desvio_padrao = np.std(dados, ddof=1)  # Usar ddof=1 para calcular o desvio padrão amostral
    tamanho_amostra = len(dados)
    t_valor = stats.t.ppf(1 - alpha / 2, tamanho_amostra - 1)  # Valor crítico da distribuição t de Student

    margem_erro = t_valor * (desvio_padrao / np.sqrt(tamanho_amostra))

    intervalo_inferior = media - margem_erro
    intervalo_superior = media + margem_erro

    return intervalo_inferior, intervalo_superior

def calcular_estatisticas_das_listas(listas):
    estatisticas = {}

    for nome, lista in listas.items():
        if isinstance(lista[0], list):
            lista_transposta = np.array(lista).T
            medias = [np.mean(sublista) for sublista in lista_transposta]
            minI = [-1 for _ in range(4)]
            maxI = [-1 for _ in range(4)]
            for i in range(len(lista_transposta)):
                minI[i], maxI[i] = calcular_intervalo_confianca(lista_transposta[i], 0.05)

            estatisticas[nome] = {
                'Mediana': [minI[0], medias[0], maxI[0]],
                'Mínimo': [minI[1], medias[1], maxI[1]],
                'Máximo': [minI[2], medias[2], maxI[2]],
                # 'Percentil99': medias[3]
            }

        else:
            minI, maxI = calcular_intervalo_confianca(lista, 0.05)

            # Listas simples - calcular estatísticas diretas
            medianas = np.mean(lista)
            minimos = np.mean(lista)
            maximos = np.mean(lista)
            percentil99 = np.mean(lista)

            estatisticas[nome] = {
                'Mediana': [minI, medianas, maxI],
                'Mínimo': [minI, minimos, maxI],
                'Máximo': [minI, maximos, maxI],
                # 'Percentil99': medias[3]
            }

    return estatisticas

def criar_grafico_de_barras(valoresk3s, valoresk8s, valoresk0s, titulo: str):
    plataformas = ['Minimum', 'Median', 'Maximum']
    k3s = [valoresk3s['Mínimo'][1], valoresk3s['Mediana'][1], valoresk3s['Máximo'][1]]
    k8s = [valoresk8s['Mínimo'][1], valoresk8s['Mediana'][1], valoresk8s['Máximo'][1]]
    k0s = [valoresk0s['Mínimo'][1], valoresk0s['Mediana'][1], valoresk0s['Máximo'][1]]

    ic_k3s = [(valoresk3s['Mínimo'][2] - valoresk3s['Mínimo'][0]) / 2,
              (valoresk3s['Mediana'][2] - valoresk3s['Mediana'][0]) / 2,
              (valoresk3s['Máximo'][2] - valoresk3s['Máximo'][0]) / 2]
    ic_k8s = [(valoresk8s['Mínimo'][2] - valoresk8s['Mínimo'][0]) / 2,
              (valoresk8s['Mediana'][2] - valoresk8s['Mediana'][0]) / 2,
              (valoresk8s['Máximo'][2] - valoresk8s['Máximo'][0]) / 2]
    ic_k0s = [(valoresk0s['Mínimo'][2] - valoresk0s['Mínimo'][0]) / 2,
              (valoresk0s['Mediana'][2] - valoresk0s['Mediana'][0]) / 2,
              (valoresk0s['Máximo'][2] - valoresk0s['Máximo'][0]) / 2]
    # Configura a largura das barras
    largura = 0.2

    posicoes = range(len(plataformas))

    plt.bar([p - largura for p in posicoes], k0s, width=largura, label='k0s', color='green', yerr=ic_k0s, capsize=5)
    plt.bar(posicoes, k3s, width=largura, label='k3s', color='blue', yerr=ic_k3s, capsize=5)
    plt.bar([p + largura for p in posicoes], k8s, width=largura, label='k8s', color='orange', yerr=ic_k8s, capsize=5)
    plt.grid("on")
    # plt.title(titulo)

    plt.xticks(posicoes, plataformas)
    # plt.xlabel("Mínimo, Mediana e Máximo de cada distribuição")
    if not "Throughput" in titulo:
        plt.ylabel("Time (ms)")
    else:
        plt.ylabel("Number of pods per second")

    plt.tight_layout(rect=[0, 0, 1, 0.85])
    plt.legend(['k0s', 'k3s', 'k8s'], bbox_to_anchor=(0, 1.02, 1, 0.2), loc="lower left", mode="expand",
               borderaxespad=0, ncol=3)
    titulo = titulo.replace(" ", "")
    titulo = titulo.replace(">", "")
    file = f"{titulo}-{ficheiro}.png"
    plt.savefig(file)
    plt.show()

def criar_grafico_de_barras_api(valoresk0s, valoresk3s, valoresk8s, titulo: str):
    operacoes = ['Create Latency', 'Delete Latency', 'Get Latency', 'List Latency', 'Update Latency']
    labels = ['k0s', 'k3s', 'k8s']
    cores = ['grey', 'blue', 'orange', 'black']

    posicoes = list(range(len(operacoes)))
    largura = 0.2

    k0s = [valoresk0s['Create Latency']['Mediana'][1], valoresk0s['Delete Latency']['Mediana'][1],
           valoresk0s['Get Latency']['Mediana'][1], valoresk0s['List Latency']['Mediana'][1],
           valoresk0s['Update Latency']['Mediana'][1]]
    k3s = [valoresk3s['Create Latency']['Mediana'][1], valoresk3s['Delete Latency']['Mediana'][1],
           valoresk3s['Get Latency']['Mediana'][1], valoresk3s['List Latency']['Mediana'][1],
           valoresk3s['Update Latency']['Mediana'][1]]
    k8s = [valoresk8s['Create Latency']['Mediana'][1], valoresk8s['Delete Latency']['Mediana'][1],
           valoresk8s['Get Latency']['Mediana'][1], valoresk8s['List Latency']['Mediana'][1],
           valoresk8s['Update Latency']['Mediana'][1]]

    ic_k0s = [valoresk0s['Create Latency']['Mínimo'][1], valoresk0s['Delete Latency']['Mínimo'][1],
              valoresk0s['Get Latency']['Mínimo'][1], valoresk0s['List Latency']['Mínimo'][1],
              valoresk0s['Update Latency']['Mínimo'][1]]
    ic_k8s = [valoresk8s['Create Latency']['Mínimo'][1], valoresk8s['Delete Latency']['Mínimo'][1],
              valoresk8s['Get Latency']['Mínimo'][1], valoresk8s['List Latency']['Mínimo'][1],
              valoresk8s['Update Latency']['Mínimo'][1]]
    ic_k3s = [valoresk3s['Create Latency']['Mínimo'][1], valoresk3s['Delete Latency']['Mínimo'][1],
              valoresk3s['Get Latency']['Mínimo'][1], valoresk3s['List Latency']['Mínimo'][1],
              valoresk3s['Update Latency']['Mínimo'][1]]

    plt.figure(figsize=(10, 5))
    plt.grid(True)

    plt.bar([p - largura for p in posicoes], k0s, width=largura, label='k0s', color='green', yerr=ic_k0s, capsize=5)
    plt.bar(posicoes, k3s, width=largura, label='k3s', color='blue', yerr=ic_k3s, capsize=5)
    plt.bar([p + largura for p in posicoes], k8s, width=largura, label='k8s', color='orange', yerr=ic_k8s, capsize=5)

    plt.xticks(posicoes, operacoes)

    plt.ylabel("Latency (ms)")

    # Adiciona título e legenda
    # plt.title(titulo)
    plt.legend(['k0s', 'k3s', 'k8s'], bbox_to_anchor=(0, 1.02, 1, 0.2), loc="lower left", mode="expand",
               borderaxespad=0, ncol=3)
    # Salva e mostra o gráfico
    # plt.tight_layout(rect=[0, 0, 1, 0.85])
    titulo = titulo.replace(' ', '_').replace('>', '')
    file = f"{titulo}.png"
    plt.savefig(file)
    plt.show()

if __name__ == '__main__':
    for i in range(1, 31):
        print(f'k3s/{ficheiro}/{ficheiro}_{i}.logs')
        k3s = open(f'k3s/{ficheiro}/{ficheiro}_{i}.logs', 'r')

        log_content_k3s = k3s.read()
        extract_results(log_content_k3s)

    estatisticas_deployment_k3s = calcular_estatisticas_das_listas(deployments)
    estatisticas_pod_k3s = calcular_estatisticas_das_listas(pods)
    # estatisticas_pod_api_k3s = calcular_estatisticas_das_listas(pod_api)
    estatisticas_deployment_api_k3s = calcular_estatisticas_das_listas(deployment_api)
    # estatisticas_namespace_api_k3s = calcular_estatisticas_das_listas(namespace_api)
    estatisticas_service_api_k3s = calcular_estatisticas_das_listas(service_api)

    print("Estatísticas dos Pods:")
    print(estatisticas_pod_k3s)
    print("Estatísticas dos Deployments:")
    print(estatisticas_deployment_k3s)

    # print("\nEstatísticas dos Pod API:")
    # print(estatisticas_pod_api_k3s)
    print("\nEstatísticas dos Deployment API:")
    print(estatisticas_deployment_api_k3s)
    print("\nEstatísticas dos NameSpace API:")
    # print(estatisticas_namespace_api_k3s)
    print("\nEstatísticas dos Service API:")
    print(estatisticas_service_api_k3s)

    pod_creation_throughput = []
    pod_client_server_e2e_latency = []
    pod_scheduling_latency_stats = []
    pod_initialization_latency_kubelet = []
    pod_starting_latency_stats = []
    pod_creation_avg_latency = []
    pod_startup_total_latecy = []

    pods = {
        'Pod Creation Throughput': [],
        'Pod Client-Server E2E Latency': [],
        'Pod Scheduling Latency Stats': [],
        'Pod Initialization Latency (Kubelet)': [],
        'Pod Starting Latency Stats': [],
        'Pod Creation Avg Latency': [],
        'Pod Startup Total Latency': [],
    }
    pod_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }
    service_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }

    deployment_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }

    namespace_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }

    deployments = {
        'Pod Creation Throughput': [],
        'Pod Client-Server E2E Latency': [],
        'Pod Scheduling Latency Stats': [],
        'Pod Initialization Latency (Kubelet)': [],
        'Pod Starting Latency Stats': [],
        'Pod Creation Avg Latency': [],
        'Pod Startup Total Latency': [],
    }

    for i in range(1, 31):
        print(f'k8s/{ficheiro}/{ficheiro}_{i}.logs')
        k8s = open(f'k8s/{ficheiro}/{ficheiro}_{i}.logs', 'r')

        log_content_k8s = k8s.read()
        extract_results(log_content_k8s)

    print(service_api)
    estatisticas_deployment_k8s = calcular_estatisticas_das_listas(deployments)
    estatisticas_pod_k8s = calcular_estatisticas_das_listas(pods)
    # estatisticas_pod_api_k8s = calcular_estatisticas_das_listas(pod_api)
    estatisticas_deployment_api_k8s = calcular_estatisticas_das_listas(deployment_api)
    # estatisticas_namespace_api_k8s = calcular_estatisticas_das_listas(namespace_api)
    estatisticas_service_api_k8s = calcular_estatisticas_das_listas(service_api)

    print("Estatísticas dos Pods:")
    print(estatisticas_pod_k8s)
    print("Estatísticas dos Deployments:")
    print(estatisticas_deployment_k8s)

    # print("\nEstatísticas dos Pod API:")
    # print(estatisticas_pod_api_k8s)
    print("\nEstatísticas dos Deployment API:")
    print(estatisticas_deployment_api_k8s)
    print(estatisticas_deployment_api_k8s)
    # print("\nEstatísticas dos NameSpace API:")
    # print(estatisticas_namespace_api_k8s)
    print("\nEstatísticas dos Service API:")
    print(estatisticas_service_api_k8s)

    for i in range(1, 31):
        print(f'k0s/{ficheiro}/{ficheiro}_{i}.logs')
        k0s = open(f'k0s/{ficheiro}/{ficheiro}_{i}.logs', 'r')

        log_content_k0s = k0s.read()
        extract_results(log_content_k0s)

    estatisticas_deployment_k0s = calcular_estatisticas_das_listas(deployments)
    estatisticas_pod_k0s = calcular_estatisticas_das_listas(pods)
    # estatisticas_pod_api_k0s = calcular_estatisticas_das_listas(pod_api)
    estatisticas_deployment_api_k0s = calcular_estatisticas_das_listas(deployment_api)
    # estatisticas_namespace_api_k0s = calcular_estatisticas_das_listas(namespace_api)
    estatisticas_service_api_k0s = calcular_estatisticas_das_listas(service_api)

    print("Estatísticas dos Pods:")
    print(estatisticas_pod_k0s)
    print("Estatísticas dos Deployments:")
    print(estatisticas_deployment_k0s)

    # print("\nEstatísticas dos Pod API:")
    # print(estatisticas_pod_api_k0s)
    print("\nEstatísticas dos Deployment API:")
    print(estatisticas_deployment_api_k0s)
    # print("\nEstatísticas dos NameSpace API:")
    # print(estatisticas_namespace_api_k0s)
    print("\nEstatísticas dos Service API:")
    print(estatisticas_service_api_k0s)

    pod_creation_throughput = []
    pod_client_server_e2e_latency = []
    pod_scheduling_latency_stats = []
    pod_initialization_latency_kubelet = []
    pod_starting_latency_stats = []
    pod_creation_avg_latency = []
    pod_startup_total_latecy = []

    pods = {
        'Pod Creation Throughput': [],
        'Pod Client-Server E2E Latency': [],
        'Pod Scheduling Latency Stats': [],
        'Pod Initialization Latency (Kubelet)': [],
        'Pod Starting Latency Stats': [],
        'Pod Creation Avg Latency': [],
        'Pod Startup Total Latency': [],
    }
    pod_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }
    service_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }

    deployment_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }

    namespace_api = {

        "Delete Latency": [],
        "Create Latency": [],
        "List Latency": [],
        "Get Latency": [],
        "Update Latency": []
    }
    deployments = {
        'Pod Creation Throughput': [],
        'Pod Client-Server E2E Latency': [],
        'Pod Scheduling Latency Stats': [],
        'Pod Initialization Latency (Kubelet)': [],
        'Pod Starting Latency Stats': [],
        'Pod Creation Avg Latency': [],
        'Pod Startup Total Latency': [],
    }

    for i in estatisticas_pod_k0s.keys():
        criar_grafico_de_barras(estatisticas_pod_k3s[i], estatisticas_pod_k8s[i],
                                estatisticas_pod_k0s[i], "Pod Statitics --> " + i)

    for i in estatisticas_deployment_k0s.keys():
        criar_grafico_de_barras(estatisticas_deployment_k3s[i], estatisticas_deployment_k8s[i],
                                estatisticas_deployment_k0s[i], "Deployment Statitics --> " + i)

    criar_grafico_de_barras_api(estatisticas_service_api_k0s, estatisticas_service_api_k3s,
                                estatisticas_service_api_k8s, f"{ficheiro}-ServiceAPI")
    criar_grafico_de_barras_api(estatisticas_deployment_api_k0s, estatisticas_deployment_api_k3s,
                                estatisticas_deployment_api_k8s, f"{ficheiro}-DeploymentAPI")
    # criar_grafico_de_barras_api(data, "Service API --> ")
    # criar_grafico_de_barras_api(estatisticas_pod_api_k0s,estatisticas_pod_api_k3s, estatisticas_pod_api_k8s,f"{ficheiro}-PodAPI")
    # criar_grafico_de_barras_api(estatisticas_namespace_api_k3s[i], estatisticas_namespace_api_k8s[i],estatisticas_namespace_api_k0s[i], f"{ficheiro}-NamespaceAPI")


from scrapy.conf import settings
from functools import wraps
import logging
import logging.handlers
import datetime
import os
import inspect

now = datetime.datetime.now()
def get_variable():
    for va in inspect.stack() :
        for v in va:
            if inspect.isframe(v):
                pass
                # print("v == ",v.f_locals)

def FinanceLogger(name=None):
    '''
    该模块用于打印使用该注释函数的日志信息
    :param level:
    :param name:
    :param message:
    :return:
    '''
    def decorate(func):
        logFileName = name if name else func.__code__.co_filename.split('/')[-1:][0].split(".")[0]
        # print(dir(func))
        # print(func.__name__)
        # print(dir(func.__code__))
        # print(func.__code__.co_filename.split('/')[-1:][0].split(".")[0])
        level=settings.get("LOG_LEVEL")
        logFileDir = settings.get("LOG_FILE_DIR")
        log = logging.getLogger(logFileName)
        log.setLevel(level)
        if(not os.path.exists(logFileDir+now.strftime('%Y%m%d')+"/")):
            os.makedirs(logFileDir+now.strftime('%Y%m%d')+"/")
        func_name = func.__name__
        handler = logging.handlers.RotatingFileHandler(logFileDir+now.strftime('%Y%m%d')+"/"+logFileName+".log",maxBytes=1024*1024,backupCount=5)
        handler.setLevel(level)
        ch = logging.StreamHandler()
        ch.setLevel(level)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelno)s - '+func_name+' - %(message)s')
        ch.setFormatter(formatter)
        handler.setFormatter(formatter)
        log.addHandler(handler)
        log.addHandler(ch)
        logMessage = ""
        @wraps(func)
        def wrapper(*args,**kwargs):
            log.log(level,logFileName)
            logMessage = str(args)
            # print("----",logMessage)
            log.info(logMessage)
            value = func(*args,**kwargs)
            get_variable()
            # setattr(func,"get_variable",get_variable())
            return value
        return wrapper

    return decorate

# import sys
# @FinanceLogger()
# def add(x:int,y:int):
#     # print(sys._getframe().f_code.co_name)
#     w=20
#     # get_variable()
#     return x+y
# add(2,3)


import sys
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from tqdm import tqdm
from kmeans import KMeans

SIGMA_CONST = 1e-6
LOG_CONST = 1e-32

class GMM(object):
    def __init__(self, X, K, max_iters = 100): # No need to change
        """
        Args:
            X: the observations/datapoints, N x D numpy array
            K: number of clusters/components
            max_iters: maximum number of iterations (used in EM implementation)
        """
        self.points = X
        self.max_iters = max_iters

        self.N = self.points.shape[0]        #number of observations
        self.D = self.points.shape[1]        #number of features
        self.K = K                           #number of components/clusters

    #Helper function for you to implement
    def softmax(self, logit): # [5pts]
        """
        Args:
            logit: N x D numpy array
        Return:
            prob: N x D numpy array. See the above function.
        """
        exp_logit = np.exp(logit - np.max(logit,axis= -1, keepdims= True))

        return exp_logit/np.sum(exp_logit,axis = -1, keepdims= True)

    def logsumexp(self, logit): # [5pts]
        """
        Args:
            logit: N x D numpy array
        Return:
            s: N x 1 array where s[i,0] = logsumexp(logit[i,:]). See the above function
        """
        exp_logit = np.exp(logit - np.max(logit,axis= -1, keepdims= True))

        return np.log(np.sum(exp_logit,axis = -1, keepdims= True)) + np.max(logit,axis=-1,keepdims=True)

    #for undergraduate student
    def normalPDF(self, logit, mu_i, sigma_i): #[5pts]
        """
        Args:
            logit: N x D numpy array
            mu_i: 1xD numpy array, the center for the ith gaussian.
            sigma_i: 1xDxD numpy array, the covariance matrix of the ith gaussian.
        Return:
            pdf: 1xN numpy array, the probability distribution of N data for the ith gaussian

        Hint:
            np.diagonal() should be handy.
        """
        N, D = np.shape(logit)
        if len(np.shape(sigma_i)) == 2:
            sigma_i = np.diagonal(sigma_i)
        else:
            sigma_i = sigma_i.reshape(1,-1)
            sigma_i = np.diagonal(sigma_i * sigma_i.T)
        pdf = np.ones((1, N))
        for i in range(D):
            exp = ((-2 * sigma_i[i] )**-1) * np.square(logit[:, i] - mu_i[i])
            pdf *= ((2 * np.pi * sigma_i[i])**-0.5) * np.exp(exp)
        return pdf.reshape((logit.shape[0],))

    #for grad students
    def multinormalPDF(self, logits, mu_i, sigma_i):  #[5pts]
        """
        Args:
            logit: N x D numpy array
            mu_i: 1xD numpy array, the center for the ith gaussian.
            sigma_i: 1xDxD numpy array, the covariance matrix of the ith gaussian.
        Return:
            normal_pdf: 1xN numpy array, the probability distribution of N data for the ith gaussian

        Hint:
            np.linalg.det() and np.linalg.inv() should be handy.
        """
        raise NotImplementedError

    def _init_components(self, **kwargs): # [5pts]
        """
        Args:
            kwargs: any other arguments you want
        Return:
            pi: numpy array of length K, prior
            mu: KxD numpy array, the center for each gaussian.
            sigma: KxDxD numpy array, the diagonal standard deviation of each gaussian.
                You will have KxDxD numpy array for full covariance matrix case
        """
        pi = np.full((self.K,),1/self.K )
        sigma = np.zeros((self.K, self.D, self.D))
        mu = np.zeros((self.K, self.D))
        for k in range(self.K):
            mu[k] = self.points[int(np.random.uniform(0,self.K))]
            sigma[k] = np.eye(self.D)

        return pi, mu, sigma

    def _ll_joint(self, pi, mu, sigma, full_matrix = False, **kwargs): # [10 pts]
        """
        Args:
            pi: np array of length K, the prior of each component
            mu: KxD numpy array, the center for each gaussian.
            sigma: KxDxD numpy array, the diagonal standard deviation of each gaussian. You will have KxDxD numpy
            array for full covariance matrix case
            full_matrix: whether we use full covariance matrix in Normal PDF or not. Default is True.

        Return:
            ll(log-likelihood): NxK array, where ll(i, k) = log pi(k) + log NormalPDF(points_i | mu[k], sigma[k])
        """
        ll = np.zeros((self.K,self.N))
        for k in range(self.K):
            ll[k] = np.log(pi[k]+ 10**(-32)) + np.log(self.normalPDF(self.points, mu[k], sigma[k]) + 10**(-32))

        return ll.T

    def _E_step(self, pi, mu, sigma, **kwargs): # [5pts]
        """
        Args:
            pi: np array of length K, the prior of each component
            mu: KxD numpy array, the center for each gaussian.
            sigma: KxDxD numpy array, the diagonal standard deviation of each gaussian.You will have KxDxD numpy
            array for full covariance matrix case
        Return:
            gamma(tau): NxK array, the posterior distribution (a.k.a, the soft cluster assignment) for each observation.

        Hint:
            You should be able to do this with just a few lines of code by using _ll_joint() and softmax() defined above.
        """
        ll = self._ll_joint(pi, mu, sigma)
        gamma = self.softmax(ll)
        return gamma

    def _M_step(self, gamma, full_matrix = False, **kwargs): # [10pts]
        """
        Args:
            gamma(tau): NxK array, the posterior distribution (a.k.a, the soft cluster assignment) for each observation.
        Return:
            pi: np array of length K, the prior of each component
            mu: KxD numpy array, the center for each gaussian.
            sigma: KxDxD numpy array, the diagonal standard deviation of each gaussian. You will have KxDxD numpy
            array for full covariance matrix case

        Hint:
            There are formulas in the slide and in the above description box.
        """
        N, D = np.shape(self.points)
        K = np.shape(gamma)[1]
        N_k = np.sum(gamma, axis=0)
        mu = np.zeros((self.K, self.D))

        pi = N_k / N
        mu = (np.matmul(gamma.T, self.points).T / N_k).T

        sigma = np.zeros((K, D, D))

        for k in range(K):
            temp = np.matmul((self.points - mu[k]).reshape((N, D, 1)), (self.points - mu[k]).reshape((N, 1, D)))
            sigma[k] = np.dot(np.transpose(temp, (1, 2, 0)), gamma[:,k])
            sigma[k] /= N_k[k]
            sigma[k] = sigma[k]*np.eye(self.D)

        return pi, mu, sigma

    def __call__(self, full_matrix = False, abs_tol=1e-16, rel_tol=1e-16, **kwargs): # No need to change
        """
        Args:
            abs_tol: convergence criteria w.r.t absolute change of loss
            rel_tol: convergence criteria w.r.t relative change of loss
            kwargs: any additional arguments you want

        Return:
            gamma(tau): NxK array, the posterior distribution (a.k.a, the soft cluster assignment) for each observation.
            (pi, mu, sigma): (1xK np array, KxD numpy array, KxDxD numpy array)

        Hint:
            You do not need to change it. For each iteration, we process E and M steps, then update the paramters.
        """
        pi, mu, sigma = self._init_components(**kwargs)
        pbar = tqdm(range(self.max_iters))

        for it in pbar:
            # E-step
            gamma = self._E_step(pi, mu, sigma)

            # M-step
            pi, mu, sigma = self._M_step(gamma, full_matrix)

            # calculate the negative log-likelihood of observation
            joint_ll = self._ll_joint(pi, mu, sigma, full_matrix)
            loss = -np.sum(self.logsumexp(joint_ll))
            if it:
                diff = np.abs(prev_loss - loss)
                if diff < abs_tol and diff / prev_loss < rel_tol:
                    break
            prev_loss = loss
            pbar.set_description('iter %d, loss: %.4f' % (it, loss))
        return gamma, (pi, mu, sigma)


from django import forms
from .models import Producto

class ProductoForm(forms.ModelForm):
    class Meta:
        model = Producto
        fields = [
            'nombre',
            'precio',
            'talla',
            'peso',
            'color',
        ]

        labels = {
            'nombre': 'Nombre',
            'precio': 'Precio',
            'talla': 'Talla',
            'peso': 'Peso',
            'color': 'Color',
        }


from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Ticket, Flight
from .forms import TicketForm, FlightFormSet

class TicketInline:
    form_class = TicketForm
    model = Ticket
    template_name = 'ticket/ticket_create_update.html'

    def form_valid(self, form):
        named_formsets = self.get_named_formsets()
        if not all((x.is_valid() for x in named_formsets.values())):
            return self.render_to_response(self.get_context_data(form=form))

        self.object = form.save()

        for name, formset in named_formsets.items():
            formset_value_func = getattr(self, 'formset_{0}_valid'.format(name), None)
            if formset_value_func is not None:
                formset_value_func(formset)
            else:
                formset.save()

            return redirect('ticket:list_tickets')

    def flight_formset_valid(self, formset):
        flights = self.save_formset(formset, contact)  # formset.save(commit=False)

        for obj in formset.deleted_objects:
            obj.delete()
        for flight in flights:
            flight.ticket = self.object
            flight.save()

class TicketCreate(TicketInline, CreateView):
    def get_context_data(self, **kwargs):
        ctx = super(TicketCreate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        if self.request.method == "GET":
            return {
                'flights': FlightFormSet(prefix='flights'),
            }
        else:
            return {
                'flights': FlightFormSet(self.request.POST or None, self.request.FILES or None, prefix='flights')
            }

class TicketUpdate(TicketInline, UpdateView):
    def get_context_data(self, **kwargs):
        ctx = super(TicketUpdate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        return {
            'flights': FlightFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object,
                                     prefix='flights'),
        }

class TicketList(ListView):
    model = Ticket
    template_name = 'ticket/ticket_list.html'
    context_object_name = 'tickets'

def delete_ticket(request, pk):
    try:
        flight = Flight.objects.get(id=pk)
    except Flight.DoesNotExist:
        messages.success(
            request, 'Object Does not exit'
        )
        return redirect('tickets:update_product', pk=flight.product.id)

    flight.delete()
    messages.success(
        request, 'Flight deleted successfully'
    )
    return redirect('tickets:update_product', pk=flight.product.id)


"""Uploads the PyInstaller binary to our S3 bucket for storing releases, then creates
a CloudFront invalidation to make those changes live.
"""

import argparse
import os
import uuid
from io import BytesIO
from pathlib import Path

import boto3
from brainframe.cli import __version__
from brainframe.cli.print_utils import fail

ssm = boto3.client("ssm")
s3 = boto3.client("s3")
cloudfront = boto3.client("cloudfront")

_COMPANY_NAMES = ["aotu", "dilililabs"]

def main():
    stage = os.environ.get("STAGE", "dev")

    args = _parse_args()

    if not args.binary_path.exists():
        fail(f"Missing binary at '{args.binary_path}'. Has a build been run?")

    bucket_name = _get_parameter(
        f"/content-delivery/bucket/releases/{stage}/bucket-name"
    )

    # Upload the binary
    s3.upload_file(
        Filename=str(args.binary_path),
        Bucket=bucket_name,
        Key="releases/brainframe-cli/brainframe",
    )

    # Upload a latest tag, containing the latest version number
    version_tag_file = BytesIO(__version__.encode("utf-8"))
    s3.upload_fileobj(
        Fileobj=version_tag_file,
        Bucket=bucket_name,
        Key="releases/brainframe-cli/latest",
    )

    for company_name in _COMPANY_NAMES:
        distribution_id = _get_parameter(
            f"/content-delivery/cdn/{company_name}/{stage}/distribution-id"
        )
        cloudfront.create_invalidation(
            DistributionId=distribution_id,
            InvalidationBatch={
                "Paths": {
                    "Quantity": 1,
                    "Items": ["/releases/brainframe-cli/*"],
                },
                "CallerReference": str(uuid.uuid4()),
            },
        )

def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Uploads the PyInstaller binary to the releases bucket and makes "
        "the change live"
    )
    parser.add_argument(
        "--binary-path",
        help="A path to the binary to upload",
        type=Path,
        default=Path("dist/brainframe"),
    )

    return parser.parse_args()

def _get_parameter(name: str) -> str:
    response = ssm.get_parameter(Name=name)
    return response["Parameter"]["Value"]

if __name__ == "__main__":
    main()


from django.urls import path

from . import views

app_name = "timetable"

urlpatterns = [
    path('', views.TimetableView.as_view(), name='timetable'),
    path('<int:pk>/', views.SingleTimetableView.as_view(), name='single_timetable'),
    path('subscribe/', views.SubscribeView.as_view(), name='subscribe')
]


from django import template
from django.core.cache import cache

from contact.models import Social, About

register = template.Library()

@register.simple_tag()
def get_social_links():
    socials = cache.get('socials')
    if not socials:
        socials = Social.objects.all()
        cache.set('socials', socials, 3600)
    return socials

@register.simple_tag()
def get_about():
    about = cache.get('about')
    if not about:
        about = About.objects.last()
        cache.set('about', about, 3600)
    return about


from .run import ModelRunner, RunTask

from dbt.exceptions import DbtInternalError
from dbt.events.functions import fire_event
from dbt.events.base_types import EventLevel
from dbt.events.types import LogSnapshotResult
from dbt.graph import ResourceTypeSelector
from dbt.node_types import NodeType
from dbt.contracts.results import NodeStatus
from dbt.utils import cast_dict_to_dict_of_strings

class SnapshotRunner(ModelRunner):
    def describe_node(self):
        return "snapshot {}".format(self.get_node_representation())

    def print_result_line(self, result):
        model = result.node
        cfg = model.config.to_dict(omit_none=True)
        level = EventLevel.ERROR if result.status == NodeStatus.Error else EventLevel.INFO
        fire_event(
            LogSnapshotResult(
                status=result.status,
                description=self.get_node_representation(),
                cfg=cast_dict_to_dict_of_strings(cfg),
                index=self.node_index,
                total=self.num_nodes,
                execution_time=result.execution_time,
                node_info=model.node_info,
                result_message=result.message,
            ),
            level=level,
        )

class SnapshotTask(RunTask):
    def raise_on_first_error(self):
        return False

    def get_node_selector(self):
        if self.manifest is None or self.graph is None:
            raise DbtInternalError("manifest and graph must be set to get perform node selection")
        return ResourceTypeSelector(
            graph=self.graph,
            manifest=self.manifest,
            previous_state=self.previous_state,
            resource_types=[NodeType.Snapshot],
        )

    def get_runner_type(self, _):
        return SnapshotRunner


"""django_conf URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import SimpleRouter
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView

from django_application.views import ArticleViewSet, ArticleProfileViewSet, ArticleDetailViewSet

router = SimpleRouter()
router.register('api/articles', ArticleViewSet),
router.register('api/article', ArticleDetailViewSet),
router.register('api/profile_article', ArticleProfileViewSet),

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/auth/', include('djoser.urls')),
    path('api/auth/', include('djoser.urls.authtoken')),
    path('api/auth/', include('djoser.urls.jwt')),

    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'),

    path('', include('react_application.urls')),
    path('registration/', include('react_application.urls'))
]

urlpatterns += router.urls

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# urlpatterns api/auth/:
# # # /users/
# # # /users/me/
# # # /users/confirm/
# # # /users/resend_activation/
# # # /users/set_password/
# # # /users/reset_password/
# # # /users/reset_password_confirm/
# # # /users/set_username/
# # # /users/reset_username/
# # # /users/reset_username_confirm/
# # # /token/login/ (Token Based Authentication)
# # # /token/logout/ (Token Based Authentication)
# # # /jwt/create/ (JSON Web Token Authentication)
# # # /jwt/refresh/ (JSON Web Token Authentication)
# # # /jwt/verify/ (JSON Web Token Authentication)


from tkinter import *
from tkinter import messagebox
import psutil
from PIL import Image, ImageTk
from lcu_driver import Connector

Window_Width = 200
Window_Height = 200
connector = Connector()

class MainWindow:
    owned_champion_count = 0
    owned_skin_count = 0

    def __init__(self, master):
        master.iconbitmap('Icon.ico')
        self.master = master
        master.title('Champion and Skin Count')
        canvas = Canvas(master, height=Window_Height, width=Window_Width)
        canvas.pack()
        main_window_background = Image.open('main_window_background.jpg')
        main_window_background = main_window_background.resize((365, 203), Image.ANTIALIAS)
        main_window_background = ImageTk.PhotoImage(main_window_background)
        main_background = Label(master, image=main_window_background)
        main_background.image = main_window_background
        main_background.place(relwidth=1, relheight=1)

        champion_info = Text(master, fg='#e6e6ff', bg='#0E0E0E', borderwidth=2, relief="groove")
        champion_info.place(relx=0.1, rely=0.2, relwidth=0.8, relheight=0.1)
        champion_info.insert('end', 'Champions:\t\t')
        champion_info.insert('end', self.owned_champion_count)
        skin_info = Text(master, fg='#e6e6ff', bg='#0E0E0E', borderwidth=2, relief="groove")
        skin_info.place(relx=0.1, rely=0.6, relwidth=0.8, relheight=0.1)
        skin_info.insert('end', 'Skins:\t\t')
        skin_info.insert('end', self.owned_skin_count)

def on_startup():
    @connector.event
    async def connect():
        summoner = await connector.request('get', '/lol-summoner/v1/current-summoner')
        if summoner.status == 200:
            data = await summoner.json()
            summoner = str(data['summonerId'])
            champion_get = await connector.request('get',
                                                   '/lol-champions/v1/inventories/' + summoner + '/champions')
            champion_data = await champion_get.json()
            for x in range(len(champion_data)):
                if champion_data[x]['ownership']['owned']:
                    MainWindow.owned_champion_count += 1
            skin_get = await connector.request('get', '/lol-champions/v1/inventories/' + summoner + '/skins-minimal')
            skin_get = await skin_get.json()
            for x in range(len(skin_get)):
                if skin_get[x]['ownership']['rental']['purchaseDate'] != 0:
                    MainWindow.owned_skin_count += 1
        else:
            messagebox.showerror("ERROR", "THERE WAS AN ERROR")
            exit(1)

    connector.start()

def process_exists():
    for p in psutil.process_iter():
        try:
            if p.name() == 'LeagueClient.exe':
                return True
        except psutil.Error:
            return False

if __name__ == '__main__':
    if process_exists():
        on_startup()
        root = Tk()
        root.resizable(0, 0)
        application = MainWindow(root)
        root.mainloop()
    else:
        messagebox.showerror("ERROR", "LEAGUE CLIENT IS NOT OPEN")

Champion And Skin Count/Champion And Skin Count.py


import sys
import numpy as np
import pandas as pd
import glob
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from os.path import join
def get_common_markers(files):
	common_markers = []
	for file in files:
		if file == files[0]:
			common_markers = pd.read_csv(file).columns.str.lower()
		else:
			common_markers = np.intersect1d(common_markers, pd.read_csv(file).columns.str.lower())
	common_markers = common_markers[common_markers != 'ID']
	common_markers = np.insert(common_markers, 0, 'ID', axis=0)
	return common_markers

def get_intensity_matrix(files):
	common_markers = ['ID', 'CD11c', 'CD21', 'CD4', 'CD8', 'Ki67']
	print(common_markers)
	intensity_image_pieces = []
	cell_num = []
	for file in files:
		print(file)
		intensity = pd.read_csv(file)[common_markers].values
		intensity_image_pieces.append(intensity)
		cell_num.append(intensity.shape[0])
	intensity_image_stacked = np.vstack(intensity_image_pieces)
	cell_id = intensity_image_stacked[:, 0].astype(int)
	return intensity_image_stacked[:, 1:], cell_id, cell_num

if __name__ == '__main__':
	data_dir = sys.argv[1]
	for intensity_type in ['total']:
		all_feature_matrix_z_5_tissues_pieces = []
		cell_file_list_5_tissues = []
		cell_id_5_tissues = []
		cell_num_5_tissues = []
		tissue_type = ['LN', 'THYMUS', 'SPLEEN', 'LI', 'SI']
		for tissue in tissue_type:
			cytoplasm_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*cell_channel_' + intensity_type + '.csv'), recursive=True))
			print(tissue)
			print(len(cytoplasm_intensity_file_list))
			# print(cytoplasm_intensity_file_list)
			cytoplasm_feature_matrix, cell_id, cell_num = get_intensity_matrix(cytoplasm_intensity_file_list)
			cell_file_list_5_tissues = cell_file_list_5_tissues + cytoplasm_intensity_file_list
			cell_id_5_tissues = cell_id_5_tissues + cell_id.tolist()
			cell_num_5_tissues = cell_num_5_tissues + cell_num
			nuclear_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*nuclei_channel_' + intensity_type + '.csv'), recursive=True))
			nuclear_feature_matrix, _, _ = get_intensity_matrix(nuclear_intensity_file_list)

			nuclear_membrane_intensity_file_list = sorted(glob.glob(join(data_dir, '**',  tissue, '**', '*nucleus_boundaries_channel_' + intensity_type + '.csv'), recursive=True))
			nuclear_membrane_feature_matrix, _, _ = get_intensity_matrix(nuclear_membrane_intensity_file_list)

			cell_membrane_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*cell_boundaries_channel_' + intensity_type + '.csv'), recursive=True))
			cell_membrane_feature_matrix, _, _ = get_intensity_matrix(cell_membrane_intensity_file_list)

			all_feature_matrix = cell_membrane_feature_matrix + cytoplasm_feature_matrix + nuclear_membrane_feature_matrix + nuclear_feature_matrix

			ss = StandardScaler()
			all_feature_matrix_z = ss.fit_transform(all_feature_matrix)
			all_feature_matrix_z_5_tissues_pieces.append(all_feature_matrix_z)
		all_feature_matrix_z = np.vstack(all_feature_matrix_z_5_tissues_pieces)
		cc = 5
		for c in range(cc, cc+46):
			print(c)
			model = KMeans(n_clusters=c, random_state=100).fit(all_feature_matrix_z)
			labels = model.labels_.astype(int)
			idx = 0
			for i in range(len(cell_file_list_5_tissues)):
				save_file_name = cell_file_list_5_tissues[i].split('-')[0] + '_individual_tissue_cluster_' + str(c) + '_label_' + intensity_type + '_5_tissues_individual_z_score.csv'
				cell_id_image = cell_id_5_tissues[idx:(idx+cell_num_5_tissues[i])]
				cell_label_image = labels[idx:(idx+cell_num_5_tissues[i])]
				cell_type_total_num = []
				for a in range(0, c):
					cell_type_total_num.append(sum(cell_label_image == a))
				save_matrix = pd.DataFrame(data=np.vstack((cell_id_image, cell_label_image)).T, columns=['ID', 'Cluster'])
				# save_matrix.to_csv(save_file_name, index=False)
				idx += cell_num_5_tissues[i]
				print(save_file_name)
				print(cell_type_total_num)


import re
import collections
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from glob import glob
from chainer.datasets import TupleDataset
from gensim.models import Word2Vec

class Data:
    """
    Class of text data, embedding weights.

    Parameters
    ==========
    dataname : str
        dataname, set as you like.
    txt_path_list : list
        list of pathes.
        ["foo.txt", "bar.txt", ..., "woo.txt"]
    labels : list
        list of labels.

    Parameters for Word2Vec embedding
    ---------------------------------
    num_workers: int
    vectorize_dim: int
    downsampling: float
    context: int
    min_word_count: int
    """
    def __init__(self, dataname: str,
                 txt_path_list: list, labels: list,
                 padding_word="<PAD/>",
                 w2v_num_workers=2, w2v_vectorize_dim=300,
                 w2v_downsampling=1e-3, w2v_context=10,
                 w2v_min_word_count=1) -> None:
        """
        set some parameters.
        """
        assert len(txt_path_list) != 0, "no path in list"
        self.dataname = dataname
        self.txt_path_list = txt_path_list
        assert len(labels) != 0, "no data in labels"
        self.labels = np.array(labels)
        self.n_labels = len(set(labels))
        self.padding_word = padding_word
        # embedding parameters
        self.w2v_vectorize_dim = w2v_vectorize_dim
        self.w2v_downsampling = w2v_downsampling
        self.w2v_context = w2v_context
        self.w2v_min_word_count = w2v_min_word_count
        self.w2v_num_workers = w2v_num_workers
        return

    def __call__(self) -> None:
        self.get_info()
        return

    def load(self, docs_line_style=False) -> object:
        """
        Main data-loading Function
        """
        if docs_line_style:
            self.load_docs_line()
        else:
            self.load_docs_file()
        self.pad_sentences()
        self.build_data()
        return self

    def get_info(self) -> str:
        output = "Data Info {}\n".format(self.dataname)
        output += "-" * 30 + "\n"
        output += "Vocab: {}\n".format(self.n_vocab)
        output += "Sentences: {}\n".format(len(self.sentences))
        output += "-" * 30 + "\n"
        output += "x_train: {}\n".format(self.x_train.shape)
        output += "x_test: {}\n".format(self.x_test.shape)
        output += "y_train: {}\n".format(self.y_train.shape)
        output += "y_test: {}\n".format(self.y_test.shape)
        return output

    def get_chainer_dataset(self) -> "chainer.datasets.Tupledataset":
        """
        get train/test splited chainer.datasets.Tupledataset data
        call this after exec self.build_data()
        """
        return (TupleDataset(self.x_train, self.y_train),
                TupleDataset(self.x_test, self.y_test))

    def load_docs_file(self, path_list=None) -> None:
        """
        load txt files and build vocab dictionaries and datasets.
        data format example
        -------------------
        - doc1.txt
            doc1 strings...
        - doc2.txt
            doc2 strings...
        variables
        ---------
        vocabulary (word2index, index2word) : dict
        - mapping from word to index (order of appearance in sentences).
        - ex. {'<PAD/>': 0, 'Hello': 1, 'World': 2}
        """
        if path_list is None:
            path_list = self.txt_path_list

        index2word = {}
        word2index = {}
        word2index[self.padding_word] = 0
        index2word[0] = self.padding_word
        counts = collections.Counter()
        sentences = []
        for file in tqdm(path_list, desc="Read Files.."):
            sentence = []
            with open(file, encoding="utf-8") as f:
                for line in f:
                    line = self.clean_str(line)
                    for word in line.split():
                        if word not in word2index:
                            ind = len(word2index)
                            word2index[word] = ind
                            index2word[ind] = word
                        counts[word2index[word]] += 1
                        sentence.append(word)
            sentences.append(sentence)
        self.index2word = index2word
        self.word2index = word2index
        self.sentences = sentences
        self.counts = counts
        self.n_vocab = len(word2index)
        return

    def load_docs_line(self, path_list=None) -> None:
        """
        load txt files and build vocab dictionaries and datasets.
        set different labels along with files.
        data format
        -----------
        - data.txt
            doc1 \n
            doc2 \n
            : \n
            : \n
            docN
        variables
        ---------
        vocabulary (word2index, index2word) : dict
        - mapping from word to index
        - ex. {'<PAD/>': 0, 'Hello': 1, 'World': 2}
        """
        if path_list is None:
            path_list = self.txt_path_list

        index2word = dict()
        word2index = dict()
        word2index[self.padding_word] = 0
        index2word[0] = self.padding_word
        counts = collections.Counter()
        sentences = list()
        labels = list()
        for label, file in tqdm(enumerate(path_list),
                                desc="Read lines.."):
            with open(file, encoding="utf-8") as f:
                for line in f:
                    sentence = []
                    line = self.clean_str(line)
                    for word in line.split():
                        if word not in word2index:
                            ind = len(word2index)
                            word2index[word] = ind
                            index2word[ind] = word
                        counts[word2index[word]] += 1
                        sentence.append(word)
                    sentences.append(sentence)
                    labels.append(label)
        self.index2word = index2word
        self.word2index = word2index
        self.sentences = np.array(sentences)
        self.labels = np.array(labels)
        self.counts = counts
        self.n_vocab = len(word2index)
        return

    def pad_sentences(self) -> None:
        """
        Pads all sentences to the same length. The length is defined by
        the longest sentence. Returns padded sentences, in order to align
        matrix dimensions.

        Parameters
        ----------
        sentences : list of lists of words
            [[sentence1], [sentence2], ..., [sentenceN]]
            Each sentence is list of words.

        padding_word : str
            Fill missed length of string with padding_word.
            When sequence length of longest sentence is 4,
            sentence=["Hello", "World"]
            would be padded_sentence=["Hello", "World", "<PAD/>", "<PAD/>"].

        padded_sentences : list of str
            [[padded_sentence1], [padded_sentence2], ..., [padded_sentenceN]]
            All the sentences have same char string length.
        """
        sequence_length = max(len(s) for s in self.sentences)
        padded_sentences = []
        for sentence in tqdm(self.sentences, desc="Padding"):
            num_padding = sequence_length - len(sentence)
            new_sentence = sentence + [self.padding_word] * num_padding
            padded_sentences.append(new_sentence)
        self.raw_sentences = self.sentences
        self.sentences = np.array(padded_sentences)
        return

    def clean_str(self, string: str) -> str:
        """
        Remove some simbols and extend abbreviated expressions.
        """
        string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
        string = re.sub(r"\'s", " is ", string)
        string = re.sub(r"\'ve", " have ", string)
        string = re.sub(r"n\'t", " not ", string)
        string = re.sub(r"\'re", " are ", string)
        string = re.sub(r"\'d", " would ", string)
        string = re.sub(r"\'ll", " will ", string)
        string = re.sub(r",", " ", string)
        string = re.sub(r"!", " ", string)
        string = re.sub(r"\(", " ", string)
        string = re.sub(r"\)", " ", string)
        string = re.sub(r"\?", " ", string)
        string = re.sub(r"\s{2,}", " ", string)
        return string.strip().lower()

    def build_data(self, ratio=0.5, seed=0) -> None:
        """
        Build train-test-splited X and y(label) data.
        Call this after executing self.load()

        data x dim : (n_sample, n_channel, words)
        """
        d = [[self.word2index[w] for w in s] for s in self.sentences]
        split = int(len(d) * ratio)
        np.random.seed(seed)
        np.random.shuffle(d)
        np.random.seed(seed)
        np.random.shuffle(self.labels)
        self.x_train, self.x_test = (np.array(d[:split])[:, np.newaxis, :],
                                     np.array(d[split:])[:, np.newaxis, :])
        self.y_train, self.y_test = (np.array(self.labels[:split]),
                                     np.array(self.labels[split:]))

        return

    def embed(self) -> None:
        """
        Embedding by gensim.Word2Vec skip-gram model and extract weight vector.
        """
        print("Training Word2vec model...")
        self.embed_model = Word2Vec(self.raw_sentences,
                                    workers=self.w2v_num_workers,
                                    size=self.w2v_vectorize_dim,
                                    min_count=self.w2v_min_word_count,
                                    window=self.w2v_context,
                                    sample=self.w2v_downsampling)
        self.embed_model.init_sims(replace=True)
        # make weights array
        weights = []
        for _, word in tqdm(self.word2index.items(),
                            desc="Getting Weights"):
            if word in self.embed_model:
                weights.append(self.embed_model[word])
            elif word == self.padding_word:
                weights.append(np.zeros(self.w2v_vectorize_dim))
            else:
                weights.append(np.random.uniform(-0.25, 0.25,
                                                 self.w2v_vectorize_dim))
        self.embed_weights = np.array(weights)

        print("Got embedding weights!")
        return

def load_imdb_data(pos_dir="data/pos/", neg_dir="data/neg/",
                   file_extend="txt") -> object:
    """
    load sample text data and build vocalbs. datasets, embedding-weights.
    """
    pos_pathes = glob(pos_dir + "*" + file_extend)
    neg_pathes = glob(pos_dir + "*" + file_extend)
    pathes = pos_pathes + neg_pathes
    labels = [1] * len(pos_pathes) + [0] * len(neg_pathes)
    return Data("imdb", pathes, labels).load()

def load_pos_neg_file(data_dir="data/", file_extends=["pos", "neg"]) -> object:
    """
    load sample text data and build vocalbs. datasets, embedding-weights.
    .pos/.neg style text file as data.
    """
    pathes = []
    for ext in file_extends:
        l = glob(data_dir + "/*" + ext)
        pathes.extend(l)
    labels = ["dummy", "dummy"]
    return Data("imdb", pathes, labels).load(docs_line_style=True)

if __name__ == "__main__":
    pass


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
from utils import get_data, rename_headers, get_dataframe
import matplotlib.animation as animation
import matplotlib
import seaborn as sns

# 1f77b4
# ff7f0e
# 2ca02c
# d62728
# 9467bd
# 8c564b

def animate(i):
    ann = fea.iloc[:int(i+1)] #select data range
    exp = chaboche.iloc[:int(i+1)] #select data range

    p = sns.lineplot(x=exp['ET12'], y=exp['S12'], data=exp, color='b')
    p = sns.lineplot(x=ann['ET12'], y=ann['S12'], data=ann, color='r',marker='.')

    # p = sns.lineplot(x=exp['Time'], y=exp['X11'], data=exp, color='#1f77b4')
    # p = sns.lineplot(x=ann['t'], y=ann['X11'], data=ann, color='#ff7f0e',marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X11'], data=exp, color='#1f77b4')
    #
    # p = sns.lineplot(x=ann['t'], y=ann['X22'], data=ann, color='#d62728', marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X22'], data=exp, color='#2ca02c')
    #
    # p = sns.lineplot(x=ann['t'], y=ann['X12'], data=ann, color='#8c564b', marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X12'], data=exp, color='#9467bd')
    p.tick_params(labelsize=7)
    plt.setp(p.lines,linewidth=3)

# Driver program
if __name__ == "__main__":
    workdir = '/home/miguel/Documents/tese/ViscoPlastic-ML/2D/butterfly/'
    abaqus_dir = '/home/miguel/Documents/tese/ViscoPlastic-ML/abaqus_deploy/butterfly/'
    workvid = '/home/miguel/Documents/tese/ViscoPlastic-ML/2D/butterfly/videos/'
    P = 'P_1'
    el_num = 75

    P = ['P_1', 'P_2', 'P_3']
    el_num = [75, 322, 229]

    for n in range(0,3):
        data_dir = []
        data_dir.append(abaqus_dir + 'state_butterfly_hist.txt')
        data_dir.append(workdir + '/results/' + P[n] + '/data_' + P[n] + '.csv')
        data_dir.append(abaqus_dir + 'deriv_butterfly_hist.txt')
        abaqus, chaboche = get_data(data_dir)
        chaboche = chaboche.drop(chaboche.index[len(chaboche)-1])
        abaqus = rename_headers(abaqus)

        # Select from abaqus dataframe info about el_num and int_point
        fea = get_dataframe(abaqus, el_num[n], 2)
        exit()
        # remove repeated values
        # Plot and save graphs of Back stress
        title = 'Back Stress'
        Writer = animation.writers['ffmpeg']
        writer = Writer(fps=2, metadata=dict(artist='Me'), bitrate=-1)
        fig = plt.figure(figsize=(10,6))
        plt.xlim(0, 0.05)
        plt.ylim(0, 75)
        ani = matplotlib.animation.FuncAnimation(fig, animate, frames=24, repeat=True)
        ani.save(workvid + 'S12.mp4', writer=writer)
        plt.close()
        break


import igraph
import leidenalg
import numpy as np
import itertools
import pandas as pd
import nwtools.communities, nwtools.consensus, nwtools.common
import click
import os

def load_graph(edge_file, node_file, sepn=',', sepe=',', idcol='id', weightcol='Weight'):
    edges_df = pd.read_csv(edge_file, na_filter=False, sep=sepe)
    edges_df = edges_df.rename({weightcol: 'weight'}, axis=1)
    edges_df['distance'] = 1.0 / edges_df['weight']
    edges_df['Source'] = edges_df.Source.astype(str)
    edges_df['Target'] = edges_df.Target.astype(str)

    # Reorder source and target so that source < target
    source = edges_df[['Source', 'Target']].apply(min, axis=1).values
    target = edges_df[['Source', 'Target']].apply(max, axis=1).values
    edges_df['Source'] = source
    edges_df['Target'] = target

    nodes = pd.read_csv(node_file, index_col=idcol, sep=sepn)
    nodes.index = nodes.index.astype(str)

    graph = nwtools.common.igraph_from_pandas_edgelist(edges_df, attributes=['weight', 'distance'])

    return edges_df, nodes, graph

def get_node_consistency(graph, edges_df, thres_list=None):
    suffix = ''
    names = graph.vs.get_attribute_values('name')
    nodes_df = pd.DataFrame(index=names)
    nodes_df['degree'+suffix] = pd.Series(graph.degree(), index=names)
    nodes_df['weighted_degree'+suffix] = pd.Series(graph.strength(weights='weight'), index=names)
    nodes_df['eigenvector_centrality'+suffix] = pd.Series(
        graph.eigenvector_centrality(directed=False), index=names)
    nodes_df['betweenness'+suffix] = pd.Series(graph.betweenness(directed=False), index=names)
    edges_unstacked = edges_df.set_index(['Source', 'Target']).consistency.unstack()

    stabilities_unstacked = edges_unstacked.add(edges_unstacked.transpose(), fill_value=0)

    nodes_df['consistency_mean'] = stabilities_unstacked.mean(axis=0)
    nodes_df['consistency_min'] = stabilities_unstacked.min(axis=0)
    nodes_df['consistency_max'] = stabilities_unstacked.max(axis=0)
    nodes_df['consistency_std'] = stabilities_unstacked.apply(np.std, axis=0)
    nodes_df['consistency_mean_min_std'] = nodes_df['consistency_mean'] - nodes_df['consistency_std']
    if thres_list is None:
        thres_list = [0.8, 0.9, 1.0]
    for thres in thres_list:
        perc_consistent_neigbors =  (stabilities_unstacked>=thres).sum(axis=0) / (~stabilities_unstacked.isna()).sum(axis=0)
        nodes_df['consistency_neighbors_{:.1f}'.format(thres)] =perc_consistent_neigbors

    return nodes_df

@click.command()
@click.argument('edge_file', type=click.Path(exists=True))
@click.argument('node_file', type=click.Path(exists=True))
@click.option('--sepn', '-sn', default=',')
@click.option('--sepe', '-se', default=',')
@click.option('--idcol', '-i', default='id')
@click.option('--weightcol', '-w', default='Weight')
@click.option('--threshold', '-t', default=0.5)
@click.option('--nriter', '-n', default=100)
@click.option('--out_dir', '-o', default=os.getcwd(), type=click.Path())
def run_consistency(edge_file, node_file, out_dir, sepn, sepe, idcol, weightcol, threshold, nriter):
    print('Load data...')
    edges_df, nodes_df, graph = load_graph(edge_file, node_file, sepn, sepe, idcol, weightcol)
    graph.write_pickle('graph.pkl')
    node_names = graph.vs['name']

    # Get initial partitions
    print('Get initial {} partitions'.format(nriter))
    partitions = nwtools.consensus.get_initial_partitions(graph, weights='weight', nr_partitions=nriter)
    partitions_df = pd.DataFrame([p.membership for p in partitions], columns=graph.vs['name'])
    partitions_df.to_csv(os.path.join(out_dir, 'partitions.csv'), index=False)

    # Get consensusmatrix
    print('Consensus clustering with t={:.2f}'.format(threshold))
    consensus_matrix, consensus_membership = nwtools.consensus.consensus_partition(graph, initial_partition=partitions,
                        nr_partitions=len(partitions),
                        threshold=threshold,
                        weights='weight')
    np.save('consensus.npy', consensus_matrix)

    # Get edge consistency and create edge dataframe
    print('Calculating edge consistency..')
    graph = nwtools.consensus.get_edge_consistency(graph, consensus_matrix)
    edges_att_df = consensus_df = pd.DataFrame({'Source': [min(node_names[e.source], node_names[e.target])
                                                           for e in graph.es],
                                 'Target': [max(node_names[e.source], node_names[e.target])  for e in graph.es],
                                'consensus': graph.es['consensus'],
                                'consistency': graph.es['consistency']})

    edges_att_df = edges_att_df.merge(edges_df,
                                      left_on=['Source', 'Target'],
                                      right_on=['Source', 'Target'])
    edges_att_df.to_csv('edges-consistency.csv', index=False)

    # Get node consistency
    print('Calculating node consistency..')
    nodes_att_df = get_node_consistency(graph, edges_att_df)
    nodes_att_df = nodes_att_df.merge(nodes_df, left_index=True, right_index=True, how='left')
    nodes_att_df['consensus'] = pd.Series(consensus_membership, index=node_names)
    nodes_att_df.to_csv('nodes-consistency.csv')

    print('Done.')

if __name__ == '__main__':
    run_consistency()


import torch
from vedo import Picture, show
from _canny_edge_maker import canny

class UtilityWorker:
    def __init__(self,
                 input_data_clean,
                 prediction,
                 device,
                 warning_m):
        self.prediction = prediction
        self.device = device
        self.input_data_clean = input_data_clean
        self.red = torch.tensor([255, 0, 0]).cpu()
        self.green = torch.tensor([0, 255, 0]).cpu()
        self.checker = torch.tensor([1, 1, 1]).cpu()
        self.threshold = 0.038
        self.warning = warning_m

    def forward(self):
        resume = self.cell_amount_finder()
        prediction_1, prediction_2 = self.prediction_splitter()
        canny_1 = self.canny_edge_maker(prediction_1)
        canny_2 = self.canny_edge_maker(prediction_2)
        final_data = self.canny_edge_applier(canny_1, canny_2)
        self.show_save(self.input_data_clean, resume, self.prediction, final_data)

    def cell_amount_finder(self):
        amount_finder = self.prediction.unique(return_counts=True)[1]
        good_c, bad_c = amount_finder[2], amount_finder[1]

        return bad_c / good_c

    def prediction_splitter(self):
        prediction_1 = torch.where(self.prediction == 1, 1, 0)
        prediction_2 = torch.where(self.prediction == 2, 1, 0)

        return prediction_1, prediction_2

    def canny_edge_maker(self, some_data):
        data = torch.unsqueeze(some_data, 0)
        data = data.expand(3, 1920, 2560).to(torch.float).cpu()
        image = canny(data)
        image = torch.squeeze(image, 0)
        image = torch.movedim(image, 0, -1) / torch.max(image).item()
        image = torch.where(image > self.threshold, 1, 0)

        return image.expand(1920, 2560, 3)

    def canny_edge_applier(self, canny_1, canny_2):
        data = self.input_data_clean.to(torch.int64)
        canny_applier = torch.where(canny_1 == self.checker,
                                    self.red, data)

        return torch.where(canny_2 == self.checker,
                           self.green, canny_applier)

    def show_save(self, victim, resume,
                  prediction_filled,
                  prediction_combined):
        data_clean = victim.to(torch.uint8).cpu().detach().numpy()
        data_mask = torch.unsqueeze(prediction_filled, -1)\
                         .expand(1920, 2560, 3)\
                         .to(device='cpu', dtype=torch.int64)
        data_mask = torch.where(data_mask == self.checker, self.red,
                                data_mask*self.green).detach().numpy()
        diagnosis = 'You are healthy\n' if resume < self.warning else 'Warning\n'
        combi = prediction_combined.to(torch.uint8).detach().numpy()
        p1 = Picture(data_clean)
        p2 = Picture(data_mask).z(1200)
        p3 = Picture(combi).z(2400)
        show((p1, 'Input sample'),
             (p2, '\nPrediction masks'),
             (p3, '\n\nPrediction edge&mask\n\nDiagnose\n'+diagnosis+str(round(resume.item(), 3))),
             __doc__, axes=7, viewup="y", bg='black').close()


from cmd.helpers.ObjectHelper import rot_center
import pygame
import random
from cmd.config.config import (
    RES_X,
    RES_Y,
    BONUS_IMG
)

class BaseBonus:
    """
    Базовый класс для бонусов - хранит состояние, координаты, угол, id и таймер
    """
    def __init__(self, id, object_positions, img, screen, pos_x=0, pos_y=0, type='power'):
        self.id = id
        self.object_positions = object_positions
        self.img = img
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.abs_pos_x = 0
        self.abs_pos_y = 0
        self.screen = screen
        self.age = 0
        self.set_img(img)
        self.type = type

    def move(self,
             left=None,
             right=None,
             up=None,
             down=None):
        if left:
            self.abs_pos_x += left
            self.pos_x += left
        if right:
            self.abs_pos_x -= right
            self.pos_x -= right
        if up:
            self.abs_pos_y += up
            self.pos_y += up
        if down:
            self.abs_pos_y -= down
            self.pos_y -= down

    def set_img(self, img: str = None):
        self.img = pygame.image.load(img)

    def draw_bonus(self):
        self.screen.blit(self.img,
                         (self.abs_pos_x - 10,
                          self.abs_pos_y - 10))

    def spawn(self):
        self.abs_pos_x = self.pos_x
        self.abs_pos_y = self.pos_y

    def spawn_random(self):
        self.pos_x = random.randint(0, RES_X)
        self.pos_y = random.randint(0, RES_Y)
        self.spawn()

    def spawn_coords(self, pos_x, pos_y):
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.spawn()


# -*- coding: utf-8 -*-
# @Time    : 2020-05-01 14:16
# @Author  : speeding_motor

from Model import DarkNet19
from loss import YoloLoss
import pascal
from config import EPOCHS
import tensorflow as tf

def main():
    yolo = DarkNet19()

    batch_data = pascal.generate_batch_data()

    yololoss = YoloLoss()
    optimizer = tf.optimizers.Adam(learning_rate=0.01)

    for epoch in range(EPOCHS):
        i = 0
        for batch in batch_data.as_numpy_iterator():

            batch_names, batch_boxs = pascal.parse_batch_set(batch)
            train_image = pascal.get_train_images(batch_names)
            train_labels = pascal.get_train_labels(batch_boxs)

            with tf.GradientTape() as tape:
                y_pred = yolo(train_image)
                loss = yololoss(y_true=train_labels, y_pred=y_pred)

            grads = tape.gradient(loss, yolo.trainable_variables)
            optimizer.apply_gradients(zip(grads, yolo.trainable_weights))
            i = i + 1
            print("epoch {} batch_index={}  loss={}".format(epoch, i, loss.numpy()))

        if epoch % 1 == 0:
            print("epoch is {}  loss is {}".format(epoch, loss))

if __name__ == '__main__':
    main()


import requests

def course_currency(wallet):
    url = "https://www.cbr-xml-daily.ru/daily_json.js"
    wallet = wallet.upper()
    res = requests.get(url).json()
    wallete = res["Valute"]

    try:
        row = "Валюта:\n{}\nПокупка:\n{}\nПродажа:\n{}".format(wallete[wallet]["Name"],
                                                               wallete[wallet]["Previous"],
                                                               wallete[wallet]["Value"])
    except:
        return "Такой валюты не существует"
    return row


import matplotlib.pyplot as plt
import numpy as np

# 图片大小和编号
# 图线颜色和宽度、格式
x = np.linspace(-1,1,50)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x,y1)

plt.figure(num=3,figsize=(8,5)) #num 图片序号 figsize 图片大小
plt.plot(x,y2)
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.show()


import pygame

import config.colors as color

from config.Resources import Constants
from runnable.ControlButton import ControlButton
from runnable.Runnable import Runnable
import runnable
from models.TextObject import TextObject
from runnable.TestMenu import TestMenu

class PauseMenu(Runnable):
    def handle_player_events(self):
        for event in self.events:
            if event.type is pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.runnable_stack.pop()

    def __init__(self, width, height, runnable_stack, bg_color, background=None, x=0, y=0):
        Runnable.__init__(self, width, height, runnable_stack, x, y)
        # self.menu_surface = pygame.display.set_mode((width, height))
        self.is_menu_running = False
        self.surface.fill(bg_color)
        self.surface.set_alpha(Constants.menu_alpha)
        self.title = TextObject("Pause", Constants.font, 90, color.white)
        self.surface.blit(self.title.Text, (width // 4, 0))
        quit_button = ControlButton("", "quit", width // 4, height // 2, width // 6, width // 6,
                                    color.black, color.black,
                                    lambda: runnable_stack.set_one_task(
                                        runnable.MainMenu.MainMenu(Constants.screen_width, Constants.screen_height, runnable_stack,
                                                 color.brick)),
                                    Constants.quit_button_image)
        resume_button = ControlButton("", "resume", 3 * width // 4 - width // 6, height // 2, width // 6, width // 6,
                                      color.black, color.black, lambda: runnable_stack.pop(),
                                      Constants.resume_button_image)
        self.add_control_button(quit_button)
        self.add_control_button(resume_button)


import csv
import os
import re
import logging
import struct
import pyscca

import base.job
from plugins.common.RVT_filesystem import FileSystem
from base.utils import check_folder, check_directory, save_csv
from base.commands import run_command
from plugins.common.RVT_files import GetFiles

def parse_RFC_file(fname):
    """ Parses RecentFileCache.bcf

    Args:
        fname (str): file path
    """
    magics = [b'\xfe\xff\xee\xff', b'\x11\x22\x00\x00', b'\x03\x00\x00\x00', b'\x01\x00\x00\x00']
    entries = []
    filesize = os.path.getsize(fname)
    if filesize <= 20:
        return ''

    with open(fname, "rb") as fh:
        fh.seek(0)
        for i in range(0, len(magics)):
            header = fh.read(4)
            if not header == magics[i]:
                return ''
        fh.read(4)  # Disregard this value

        while fh.tell() < filesize:
            tmp_buffer = fh.read(4)
            entry_len = (struct.unpack('<i', tmp_buffer)[0]) * 2  # For unicode
            entry = fh.read(entry_len)
            entries.append(entry.decode("utf-16"))
            fh.read(2)  # Disregard last two unicode null terminators as they break in decode

    return entries

def parse_prefetch_file(pf_file):
    """ Parse individual file. Output is placed in 'output' dictionary

    Args:
        pf_file (str): list of filenames
    Returns:
        dict: dict with prefetch file information
    """
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)

    logger.info("Parsing {}".format(pf_file))
    item = {}

    try:
        scca = pyscca.open(pf_file)
        res_loaded = []
        for x in range(scca.get_number_of_file_metrics_entries()):
            res_loaded.append(scca.get_filename(x))
        item["resources loaded"] = res_loaded

        last_run_times = []
        n_runs = 1
        if scca.get_format_version() > 23:
            n_runs = 8

        for x in range(n_runs):
            if scca.get_last_run_time_as_integer(x) > 0:
                last_run_times.append(scca.get_last_run_time(x).strftime("%Y-%m-%d %H:%M:%S"))  # str conversion utilized to change from datetime into human-readable
            else:
                last_run_times.append('')

        item["last run times"] = last_run_times
        item["filename"] = scca.executable_filename
        item["prefetch hash"] = format(scca.prefetch_hash, 'x').upper()
        item["run count"] = str(scca.run_count)

        volumes = []
        for i in range(scca.number_of_volumes):
            volume = [str(scca.get_volume_information(i).device_path), scca.get_volume_information(i).creation_time.strftime(
                "%Y-%m-%d %H:%M:%S"), format(scca.get_volume_information(i).serial_number, 'x').upper()]
            volumes.append(volume)
        item["Volumes"] = volumes

        return item
    except IOError as e:
        logger.error("I/O Error: {}".format(e))
        return -1
    except SystemError as e:
        logger.error("Bad signature for pf file: {}".format(e))
        return -1
    except Exception:
        logger.error("Unexpected error")
        return -1

class Prefetch(base.job.BaseModule):
    """ Parse prefetch """

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.vss = self.myflag('vss')
        self.filesystem = FileSystem(self.config)
        self.parse_Prefetch()
        return []

    def parse_Prefetch(self):
        self.logger().info("Parsing prefetch files")

        base_path = self.myconfig('{}outdir'.format('v' if self.vss else ''))
        check_directory(base_path, create=True)
        prefetch_dir = self.search.search(r"Windows/Prefetch$")

        if not prefetch_dir:
            self.logger().info('Prefetch file not found')
            return []

        for pdir in prefetch_dir:
            partition = pdir.split("/")[-3]
            csv_file = open(os.path.join(base_path, "prefetch_%s.csv" % partition), "w")
            writer = csv.writer(csv_file, delimiter=";", quotechar='"')
            pf_output1 = open(os.path.join(base_path, "prefetch_dump_%s.txt" % partition), "w")
            pdir_path = os.path.join(self.myconfig('casedir'), pdir)
            try:
                file_list = [os.path.join(pdir, file) for file in os.listdir(pdir_path) if file.endswith(".pf")]
            except IOError:
                self.logger().error('Unable to list files in directory {}'.format(pdir_path))
                continue
            except Exception as exc:
                self.logger().error(exc)
                continue

            if len(file_list) == 0:
                continue

            tl_files = self.filesystem.get_macb(file_list, self.vss)

            flag = True
            for file in os.listdir(pdir_path):
                prefetch_file = os.path.join(pdir_path, file)
                prefetch_rel_path = os.path.join(pdir, file)
                if file.endswith(".pf") and os.path.getsize(prefetch_file) > 0:  # Parse only non empty .pf files
                    item = parse_prefetch_file(prefetch_file)
                else:
                    continue

                if item == -1:
                    self.logger().error("Problems parsing {}".format(file))
                    pf_output1.write("Filename:\t\t{}\nBirth time:\t\t{}\nPrefetch Hash:\t\t\nExecutable Filename:\t\nRun count:\t\t\n".format(
                        file, tl_files[prefetch_rel_path][3]))
                    writer.writerow([file, "", "", tl_files[prefetch_rel_path][3], tl_files[prefetch_rel_path][0]] + [""] * 7)
                    continue

                if flag:
                    headers = ["Filename", "Executable", "Run count", "Birth time"] + ["Run time {}".format(str(i)) for i in range(len(item["last run times"]))]
                    writer.writerow(headers)
                    flag = False

                pf_output1.write("Filename:\t\t{}\nCreation time:\t\t{}\nPrefetch Hash:\t\t{}\nExecutable Filename:\t{}\nRun count:\t\t{}\n".format(
                    file, tl_files[prefetch_rel_path][3], item["prefetch hash"], item["filename"], item["run count"]))
                for i, fecha in enumerate(item["last run times"]):
                    pf_output1.write("\tRun time {}:\t\t{}\n".format(str(i), fecha))
                pf_output1.write("\nFilenames\nNumber of Filenames:\t{}\n".format(str(len(item["resources loaded"]))))
                for i in item["resources loaded"]:
                    pf_output1.write("\t{}\n".format(i))
                pf_output1.write("\nVolumes\nNumber of Volumes:\t{}\n".format(str(len(item["Volumes"]))))
                for v in item["Volumes"]:
                    pf_output1.write("\tDevice path:\t{}\n\tCreation time:\t{}\n\tSerial Number:\t{}\n\n".format(v[0], v[1], v[2]))
                pf_output1.write("################################################\n")

                writer.writerow([file, item["filename"], item["run count"], tl_files[prefetch_rel_path][3]] + [i for i in item["last run times"]])
            pf_output1.close()
            csv_file.close()

        self.logger().info("Parsing prefetch files finished")

class RFC(base.job.BaseModule):
    """ Parses RecentFileCache.bcf """

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.logger().info("Parsing RecentFileCache.bcf")
        self.parse_RFC()
        return []

    def parse_RFC(self):
        base_path = self.myconfig('outdir')
        check_folder(base_path)

        rfc_list = list(self.search.search("RecentFileCache.bcf$"))
        if len(rfc_list) == 0:
            self.logger().info("No RecentFileCache.bcf files founded in disk")
            return

        for file in rfc_list:
            self.logger().info("Parsing {}".format(file))
            partition = file.split("/")[2]
            outfile = os.path.join(base_path, "rfc_{}.csv".format(partition))
            try:
                rfc = ({'Application': i} for i in parse_RFC_file(os.path.join(self.myconfig('casedir'), file)))
                save_csv(rfc, config=self.config, outfile=outfile, quoting=0, file_exists='OVERWRITE')
            except Exception:
                self.logger().warning("Problems parsing {}".format(file))

        self.logger().info("Parsing RecentFileCache.bcf finished")

class BAM(base.job.BaseModule):

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.vss = self.myflag('vss')
        self.logger().info("Parsing BAM from registry")
        self.parse_BAM()
        return []

    def parse_BAM(self):
        base_path = self.myconfig('{}outdir'.format('v' if self.vss else ''))
        check_folder(base_path)
        SYSTEM = list(self.search.search(r"windows/System32/config/SYSTEM$"))

        partition_list = set()
        for f in SYSTEM:
            aux = re.search(r"([vp\d]*)/windows/System32/config", f, re.I)
            partition_list.add(aux.group(1))

        bam_file = {p: os.path.join(base_path, "bam_%s.txt" % p) for p in partition_list}
        ripcmd = self.config.get('plugins.common', 'rip', '/opt/regripper/rip.pl')

        for f in SYSTEM:
            with open(bam_file[f.split("/")[2]], 'w') as of:
                of.write("-----------------------------------------------------------------------\n{}\n-----------------------------------------------------------------------\n\n".format(f))
                of.write(run_command([ripcmd, "-r", os.path.join(self.myconfig('casedir'), f), "-p", "bam"], logger=self.logger()))
                of.write("\n\n")

        self.logger().info("Finished extraction of Background Activity Moderator (BAM)")


import singer
from singer import metadata

from funcy import project

from tap_exacttarget.util import sudsobj_to_dict

LOGGER = singer.get_logger()

def _get_catalog_schema(catalog):
    return catalog.get('schema', {}).get('properties')

class DataAccessObject():

    def __init__(self, config, state, auth_stub, catalog):
        self.config = config.copy()
        self.state = state.copy()
        self.catalog = catalog
        self.auth_stub = auth_stub

    @classmethod
    def matches_catalog(cls, catalog):
        return catalog.get('stream') == cls.TABLE

    def generate_catalog(self):
        cls = self.__class__

        mdata = metadata.new()
        metadata.write(mdata, (), 'inclusion', 'available')
        for prop in cls.SCHEMA['properties']: # pylint:disable=unsubscriptable-object
            metadata.write(mdata, ('properties', prop), 'inclusion', 'available')

        return [{
            'tap_stream_id': cls.TABLE,
            'stream': cls.TABLE,
            'key_properties': cls.KEY_PROPERTIES,
            'schema': cls.SCHEMA,
            'metadata': metadata.to_list(mdata)
        }]

    def filter_keys_and_parse(self, obj):
        to_return = sudsobj_to_dict(obj)

        return self.parse_object(to_return)

    def get_catalog_keys(self):
        return list(
            self.catalog.get('schema', {}).get('properties', {}).keys())

    def parse_object(self, obj):
        return project(obj, self.get_catalog_keys())

    def write_schema(self):
        singer.write_schema(
            self.catalog.get('stream'),
            self.catalog.get('schema'),
            key_properties=self.catalog.get('key_properties'))

    def sync(self):
        mdata = metadata.to_map(self.catalog['metadata'])
        if not metadata.get(mdata, (), 'selected'):
            LOGGER.info('{} is not marked as selected, skipping.'
                        .format(self.catalog.get('stream')))
            return None

        LOGGER.info('Syncing stream {} with accessor {}'
                    .format(self.catalog.get('tap_stream_id'),
                            self.__class__.__name__))

        self.write_schema()

        return self.sync_data()

    # OVERRIDE THESE TO IMPLEMENT A NEW DAO:

    SCHEMA = None
    TABLE = None
    KEY_PROPERTIES = None

    def sync_data(self):  # pylint: disable=no-self-use
        raise RuntimeError('sync_data is not implemented!')