from AUC import *
import copy
import xlrd
import math
from xlrd import xldate_as_tuple
import datetime
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import random
import copy
import xlwt
import time
import csv
import os
import json
import requests
import logging
from scipy.interpolate import splrep, splev
import traceback

logging.disable(logging.DEBUG)


''' 准备数据 '''
SCHEDULER_BASE_URL = os.getenv("SCHEDULER_BASE_URL")
BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL")

missionId = os.getenv("missionId")
planId = os.getenv("planId")

headers = {
    "Content-Type": "application/json",  # 明确声明数据格式
    "Accept": "application/json"         # 声明期望的响应格式
}
params = {
    "missionId": missionId,
    "planId": planId,
}

print("[output]", json.dumps({'msg': 'started'}), flush=True)

response = requests.get(SCHEDULER_BASE_URL + '/fetchData', params=params, headers=headers)
fetchedData = response.json()
if not fetchedData:
    # 此处应当放置错误报告
    quit()
# fetchedData: {'nodes': [] , 'edges': []}
'''准备数据（完毕）'''

directory = r'测试输出'

def import_excel(excel):
    for rown in range(excel.nrows):
        array = [0 for i in range(2)]

        array[0] = table.cell_value(rown, 0)

        array[1] = table.cell_value(rown, 1)

        tables.append(array)


class Matrix:

    def __init__(self, row, column, fill=0):
        self.shape = (row, column)
        self.row = row
        self.column = column
        self._matrix = [[fill] * column for i in range(row)]

    # 返回元素m(i, j)的值:  m[i, j]
    def __getitem__(self, index):
        if isinstance(index, int):
            return self._matrix[index - 1]
        elif isinstance(index, tuple):
            return self._matrix[index[0] - 1][index[1] - 1]

    # 设置元素m(i,j)的值为s:  m[i, j] = s
    def __setitem__(self, index, value):
        if isinstance(index, int):
            self._matrix[index - 1] = copy.deepcopy(value)
        elif isinstance(index, tuple):
            self._matrix[index[0] - 1][index[1] - 1] = value

    def __add__(self, N):
        '''加法'''
        # A + B
        assert N.shape == self.shape, "维度不匹配，不能相加"
        M = Matrix(self.row, self.column)
        for r in range(self.row):
            for c in range(self.column):
                M[r, c] = self[r, c] + N[r, c]
        return M

    def __eq__(self, N):
        # A == B
        assert isinstance(N, Matrix), "注:类型不匹配，不能比较"
        return N.shape == self.shape  # 比较维度

    # def show(self):
    #     # 输出函数
    #     for r in range(self.row):  # 遍历
    #         for c in range(self.column):
    #             print(round(self[r, c + 1], 2), end='  ')
    #             ##print(self[r, c + 1], end='  ')
    #         # print()


def main_process():
    ##海湾战争/随机图1/随机图2/随机图3
    # directory = r'./画图测试'
    # print("//---------data set description---------//")
    # 导入需要读取的Excel表格的路径
    # filename_edge = 'edges.xls'
    # file_path_edge = fr'{directory}\{filename_edge}'
    # data_edge = xlrd.open_workbook(file_path_edge)
    # filename_node = 'nodes.xls'
    # file_path_node = fr'{directory}\{filename_node}'
    # data_node = xlrd.open_workbook(file_path_node)

    # 修改从flask获取nodes数据
    data_node = fetchedData['nodes']
    # 清空所有原有meta
    for node in data_node:
        node['meta'] = []
    # 修改从flask获取edges数据
    data_edge = fetchedData['edges'] # [{'form': X, 'to': X, 'meta': [{}{}{}]}]
    # 清空所有原有meta
    for edge in data_edge:
        edge['meta'] = []


    # 检测节点编号，本程序要求从0起
    flag = True
    for node in data_node:
        if int(node['id']) == 0:
            flag = False
    if flag:
        # 原始数据不从0开始，则所有节点的id均减一，同时边的节点id也减一
        for node in data_node:
            node['id'] = int(node['id']) - 1
        for edge in data_edge:
            edge['from'] = int(edge['from']) - 1
            edge['to'] = int(edge['to']) - 1


    # 准备汇报flask的数据
    result = {
        'missionId': missionId,
        'planId': planId,
        'progress': 100,
        'nodes': [],
        'edges': [],
    }

    Data_scale = 10000

    # 获取第一个工作表
    # sheet_node = data_node.sheet_by_index(0)
    # 获取表格的行数
    # N = sheet_node.nrows
    # 改为从flask数据获取点的数量
    N = 0
    for node in data_node:
        if int(node['id']) + 1 > N:
            N = int(node['id']) + 1
    # N = len(data_node)
    # print("数据集(点集)长度：      %d" % (N))
    # 初始化计数器
    count_s = 0
    count_d = 0
    count_i = 0
    # 改为遍历flask获取的数据
    for node in data_node:
        # cell_value = sheet_node.cell_value(row_index, 1)  # 获取第二列单元格的值
        # 改为从flask数据获取节点的类型
        cell_value = node['type'].upper()
        if cell_value == "S":
            count_s += 1
        elif cell_value == "D":
            count_d += 1
        elif cell_value == "I":
            count_i += 1
    # print("其中包括传感节点(S)：   %d" % (count_s))
    # print("其中包括决策节点(D)：   %d" % (count_d))
    # print("其中包括响应节点(I)：   %d" % (count_i))

    # 统计行数，输出结果
    # sheet_edge = data_edge.sheet_by_index(0)
    # edge_num = sheet_edge.nrows
    # 修改为从flask数据中统计边数
    edge_num = len(data_edge)

    # print("数据集(边集)长度：      %d" % (edge_num))

    sum_num = N * N + count_s * count_s + count_d * count_d
    Sparsity = (sum_num - edge_num) / sum_num * 100
    # print("数据稀疏性：           " + str(round(Sparsity, 2)) + "％")
    # print("//--------------------------------------//")

    # 循环次数
    epoch = 10
    auc_sum = 0
    method_time_sum = 0

    for epoch_num in range(epoch):
        # logging.basicConfig(
        #     format='%(asctime)s    [%(levelname)s]  %(message)s',
        #     datefmt='%d %b %H:%M'
        # )
        # logging.getLogger().setLevel(logging.DEBUG)
        # logger = logging.getLogger()
        # logger.info("Meta_AIProbS")

        # time.sleep(0.1)
        # 定义需要删除的元素比例
        p = 0.2
        test_set = p * 100
        train_set = 100 - test_set
        # print("number of experiments:            " + str(epoch_num + 1))
        # print("proportion of training set:       " + str(train_set) + "％")
        # print("proportion of testing set:        " + str(test_set) + "％")

        TimeStart = time.perf_counter()

        ##邻接矩阵
        m = Matrix(N, N, fill=0)
        train = Matrix(N, N, fill=0)
        text = Matrix(N, N, fill=0)

        # table = data_edge.sheets()[0]
        # 创建一个空列表，存储Excel的数据

        tables = []
        # 将excel表格内容导入到tables列表中
        # import_excel(table)
        # 改为从flask获取数据中生成tables，tables中存放的是边
        for edge in data_edge:
            # 将每一条边的起始存入edge
            tables.append([int(edge['from']), int(edge['to'])])
        ##初始化训练集
        for i in tables:
            train[int(i[0])][int(i[1])] = 1
            train[int(i[1])][int(i[0])] = 1

        # 计算需要删除的元素个数
        num_delete = int(len(tables) * p)
        # 随机选择num_delete个元素进行删除
        if num_delete > 0:
            idx = random.sample(range(len(tables)), num_delete)
            # deleted = [tables[i] for i in idx]
            tables = [i for j, i in enumerate(tables) if j not in idx]

        # 将剩余的元素输出到文件中
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet('My Worksheet')
        for i in range(len(tables)):
            worksheet.write(i, 0, tables[i][0])
            worksheet.write(i, 1, tables[i][1])

            # 剩余边需要写入反馈给flask的数据
            # 保存所有出现的点
            row_index = int(tables[i][0])
            col_index = int(tables[i][1])
            if not any(int(row_index) == node['id'] for node in result['nodes']):
                meta = [node['meta'] for node in data_node if int(node['id']) == int(row_index)][0]
                node_type = [n['type'] for n in data_node if int(n['id']) == int(row_index)][0]
                result['nodes'].append({'id': int(row_index), 'type': node_type.upper(), 'meta': meta})

            if not any(int(col_index) == node['id'] for node in result['nodes']):
                meta = [node['meta'] for node in data_node if int(node['id']) == int(col_index)][0]
                node_type = [n['type'] for n in data_node if int(n['id']) == int(col_index)][0]
                result['nodes'].append({'id': int(col_index), 'type': node_type.upper(), 'meta': meta})
            # 将结果写入result汇报给flask
            result['edges'].append({'from': int(row_index), 'to': int(col_index), 'meta': [{ 'predict': 'old' }]})

        filename_text = '边集（测试）.xlsx'
        file_path_text = fr'{directory}\{filename_text}'
        workbook.save(file_path_text)

        '''
        # 输出成功删除的元素个数和总元素个数
        print("成功删除%d个元素，剩余%d个元素。" % (num_delete, len(tables)))

        # 输出被删除的元素
        if num_delete > 0:
            print("被删除的元素为：")
            for i in range(len(deleted)):
                print(deleted[i])
        else:
            print("没有删除任何元素。")
        '''

        for i in tables:
            m[int(i[0])][int(i[1])] = 1
            m[int(i[1])][int(i[0])] = 1

        for i in range(N):
            for j in range(N):
                if (train[i][j] == 1 and m[i][j] == 0):
                    text[i][j] = 1

        tables = []
        # table = data_node.sheets()[0]
        m_2 = ["0" for i in range(N)]
        # import_excel(table)
        # 改为从flask获取数据，table中存放的是节点数据
        for node in data_node:
            # m_2中存放的是节点的类型，小标为节点的id
            m_2[int(node['id'])] = str(node['type']).upper()
        # j = 0
        # for i in tables:
        #     m_2[j] = i[1]
        #     j += 1

        # print("Calculating H-index......")
        TimeStart_1 = time.perf_counter()

        matrix = [[0 for i in range(N)] for i in range(50)]

        for i in range(N):
            sum = 0
            for j in range(N):
                sum = sum + m[i][j]
            matrix[0][i] = sum

        if (N < Data_scale):
            flag = 0
            s = 0
            while (flag < N):
                flag = 0
                for k in range(N):
                    for i in range(matrix[s][k]):
                        sum = 0
                        for j in range(N):
                            if (m[k][j] == 1) and (matrix[s][j] >= i + 1):
                                sum += 1
                        if sum > i:
                            matrix[s + 1][k] = i + 1

                for l in range(N):
                    if matrix[s + 1][l] == matrix[s][l]:
                        flag += 1
                s += 1

        else:
            flag = 0
            rule = int(N / 100) + 1
            for ruler in range(rule):
                half_a = 100 * ruler
                half_b = min(100 * (ruler + 1), N)
                s = 0
                while (flag < half_b):
                    flag = half_a
                    for k in range(half_a, half_b):
                        for i in range(matrix[s][k]):
                            sum = 0
                            for j in range(half_a, half_b):
                                if (m[k][j] == 1) and (matrix[s][j] >= i + 1):
                                    sum += 1
                            if sum > i:
                                matrix[s + 1][k] = i + 1

                    for l in range(half_a, half_b):
                        if matrix[s + 1][l] == matrix[s][l]:
                            flag += 1
                    s += 1
            s = s + 3
        ##print("s:",s)
        """
        for l in range(N):
            if matrix[s-2][l] != matrix[s-1][l]:
                for i in range(s):
                    print(matrix[i][l])
        """

        TimeEnd_1 = time.perf_counter()
        # print("Time for calculating H-index:     " + str(round(TimeEnd_1 - TimeStart_1, 2)) + "s")

        n = Matrix(N, s, fill=0)
        for i in range(N):
            for j in range(s):
                n[i][j] = matrix[j][i]

        score = Matrix(N, N, fill=0)

        ##D节点列表
        num_D = 0
        count_D = [0 for i in range(N)]
        j = 0
        for i in range(N):
            if m_2[i] == 'D':
                num_D += 1
                count_D[j] = i
                j += 1
        j = 0
        node_D = [0 for i in range(num_D)]
        for i in range(num_D):
            node_D[i] = count_D[i]
            ##print(node_D[i])
        ##print("D节点列表")

        ##S节点列表
        num_S = 0
        count_S = [0 for i in range(N)]
        j = 0
        for i in range(N):
            if m_2[i] == 'S':
                num_S += 1
                count_S[j] = i
                j += 1
        j = 0
        node_S = [0 for i in range(num_S)]
        for i in range(num_S):
            node_S[i] = count_S[i]
            ##print(node_S[i])
        ##print("S节点列表")

        # print("Probabilistic Spreading......")
        TimeStart_2 = time.perf_counter()

        if (N < Data_scale):
            ##相似性矩阵
            a = Matrix(N, N, fill=0)
            for i in range(N):
                for j in range(N):
                    sum_1 = 0
                    sum_2 = 0
                    sum_3 = 0
                    for k in range(s):
                        sum_1 += n[i][k] * n[j][k]
                        sum_2 += n[i][k] * n[i][k]
                        sum_3 += n[j][k] * n[j][k]
                    if (i == j) or (sum_2 == 0) or (sum_3 == 0):
                        a[i][j] = 0
                    else:
                        a[i][j] = sum_1 / (math.sqrt(sum_2) * math.sqrt(sum_3))
            ##a.show()

            ##归一化处理
            sum = 0
            for i in range(N):
                for j in range(N):
                    sum += a[i][j]
                if (sum != 0):
                    for k in range(N):
                        a[i][k] = 1 * a[i][k] / sum
                ##print(sum)
                sum = 0
            ##print("//-------------------------------------------//")
            ##a.show()
            ##print("归一化处理")

            matrix_D = [[0 for i in range(N)] for i in range(num_D)]
            for i in range(num_D):
                ##for i in range(1):
                count = 1
                count_this = 0
                tmp = [[0 for i_num in range(num_D)] for i_num in range(2)]
                for j in range(num_D):
                    tmp[0][j] = -1
                tmp[0][0] = node_D[i]
                tmp[1][0] = 1
                while (count_this < count):
                    ##print("lunshu:",count_this+1)
                    sum = 0
                    for j in range(N):
                        if (m[tmp[0][count_this]][j] == 1):
                            """
                            print(tmp[0][count_this])
                            if (m_2[j] == 'D'):
                                print("D:",[j])
                            else:
                                print([j])
                            """
                            flag = 0
                            for k in tmp[0]:
                                if k == j:
                                    flag = 1
                            if (flag == 0):
                                sum += a[tmp[0][count_this]][j]
                    ##print("sum:", sum)
                    ##sum_2 = 0
                    for j in range(N):
                        if (m[tmp[0][count_this]][j] == 1):
                            if m_2[j] != 'D':
                                matrix_D[i][j] += a[tmp[0][count_this]][j] * tmp[1][count_this] / sum
                                ##sum_2 += b[i][j]
                            else:
                                flag = 0
                                for k in tmp[0]:
                                    if k == j:
                                        flag = 1
                                if (flag == 0):
                                    tmp[0][count] = j
                                    ##print("tmp:",count,"  ",j)
                                    tmp[1][count] = a[tmp[0][count_this]][j] * tmp[1][count_this] / sum
                                    ##print("tmp:", count, "  ", tmp[1][count])
                                    ##sum_2 += tmp[1][count]
                                    count += 1
                    ##print("sum_2:",sum_2)
                    tmp[1][count_this] = 0
                    count_this += 1
            ##print("D节点矩阵")
            """
            for i in range(num_D):
                print(tmp[0][i])
                print(tmp[1][i])

            for j in range(num_D):
                num = 0
                for i in range(N):
                    num += matrix_D[j][i]
                    ##print(i)
                    ##print(matrix_D[0][i])
                print(num)
            """

            matrix_S = [[0 for i in range(N)] for i in range(num_S)]
            for i in range(num_S):
                ##for i in range(1):
                count = 1
                count_this = 0
                tmp = [[0 for i in range(num_S)] for i in range(2)]
                for j in range(num_S):
                    tmp[0][j] = -1
                tmp[0][0] = node_S[i]
                tmp[1][0] = 1
                while (count_this < count):
                    sum = 0
                    num = 0
                    for j in range(N):
                        if (m[tmp[0][count_this]][j] == 1 and m_2[j] != 'D'):
                            '''
                            print(tmp[0][count_this])
                            if (m_2[j] == 'S'):
                                print("S:", [j])
                            else:
                                print([j])
                            '''

                            flag = 0
                            for k in tmp[0]:
                                if k == j:
                                    flag = 1
                            if (flag == 0):
                                sum += a[tmp[0][count_this]][j]
                            num += 1

                            '''
                            print("lunshu:",num)
                            print("count_this:",count_this)
                            print("count:", count)
                            '''

                    for j in range(N):
                        if (m[tmp[0][count_this]][j] == 1):
                            if m_2[j] == 'I':
                                matrix_S[i][j] += a[tmp[0][count_this]][j] * tmp[1][count_this] / sum
                            if m_2[j] == 'S':
                                flag = 0
                                for k in tmp[0]:
                                    if k == j:
                                        flag = 1
                                if (flag == 0):
                                    tmp[0][count] = j
                                    tmp[1][count] = a[tmp[0][count_this]][j] * tmp[1][count_this] / sum
                                    count += 1
                                    '''
                                    print("//////////////")
                                    for g in range(count):
                                        print(tmp[0][g])
                                    print("//////////////")
                                    '''
                    tmp[1][count_this] = 0
                    count_this += 1
            ##print("S节点矩阵")
            '''
            for j in range(num_S):
                num = 0
                for i in range(N):
                    num += matrix_S[j][i]
                    ##print(i)
                    ##print(matrix_S[0][i])
                print(num)
            '''
            for i in range(num_D):
                ##for i in range(1):
                for j in range(N):
                    if (matrix_D[i][j] > 0):
                        sum = 0
                        for k in node_D:
                            if (m[j][k] == 1):
                                sum += a[j][k]
                        for k in node_D:
                            if (m[j][k] == 1):
                                matrix_D[i][k] += matrix_D[i][j] * a[j][k] / sum
                        matrix_D[i][j] = 0
            '''
            for j in range(num_D):
                num = 0
                for i in range(N):
                    num += matrix_D[j][i]
                    #print(i)
                    #print(matrix_D[0][i])
                print(num)
            '''
            for i in range(num_S):
                ##for i in range(1):
                for j in range(N):
                    if (matrix_S[i][j] > 0):
                        sum = 0
                        for k in node_S:
                            if (m[j][k] == 1):
                                sum += a[j][k]
                        for k in node_S:
                            if (m[j][k] == 1):
                                matrix_S[i][k] += matrix_S[i][j] * a[j][k] / sum
                        matrix_S[i][j] = 0

            re_D = Matrix(num_D, num_D, fill=0)
            for i in range(num_D):
                for j in range(num_D):
                    re_D[j][i] = matrix_D[i][node_D[j]]
                ##re_D.show()
                '''
                sum = 0
                for i in range(num_D):
                    for j in range(num_D):
                        sum += re_D[j][i]
                    print(sum)
                    sum = 0
                '''
                '''
                for i in range(num_D):
                    sum = 0
                    for j in range(num_D):
                        sum += re_D[i][j]
                    print(sum)
                '''

            re_S = Matrix(num_S, num_S, fill=0)
            for i in range(num_S):
                for j in range(num_S):
                    re_S[j][i] = matrix_S[i][node_S[j]]
                ##re_S.show()

                '''
                for i in range(num_S):
                    sum = 0
                    for j in range(num_S):
                        sum += re_S[i][j]
                    print(sum)
                '''

            for i in range(N):
                if (m_2[i] != 'D'):
                    for j in range(num_D):
                        if (m[i][node_D[j]] == 0):
                            for k in range(num_D):
                                if (m[i][node_D[k]] == 1):
                                    score[i][node_D[j]] += re_D[j][k]
            if (m_2[i] == 'I'):
                for j in range(num_S):
                    if (m[i][node_S[j]] == 0):
                        for k in range(num_S):
                            if (m[i][node_S[k]] == 1):
                                score[i][node_S[j]] += re_S[j][k]
            ##score.show()

            for i in node_D:
                for j in node_D:
                    score[i][j] = a[i][j]
            for i in node_S:
                for j in node_S:
                    score[i][j] = a[i][j]

            for i in range(N):
                for j in range(N):
                    if (text[i][j] == 1 and score[i][j] == 0):
                        score[i][j] = a[i][j]

        else:
            a = Matrix(N, N, fill=0)
            rule = int(N / 100) + 1
            for ruler in range(rule):
                half_a = 100 * ruler
                half_b = min(100 * (ruler + 1), N)
                for i in range(half_a, half_b):
                    for j in range(half_a, half_b):
                        sum_1 = 0
                        sum_2 = 0
                        sum_3 = 0
                        for k in range(s):
                            sum_1 += n[i][k] * n[j][k]
                            sum_2 += n[i][k] * n[i][k]
                            sum_3 += n[j][k] * n[j][k]
                        if (i == j) or (sum_2 == 0) or (sum_3 == 0):
                            a[i][j] = 0
                        else:
                            a[i][j] = sum_1 / (math.sqrt(sum_2) * math.sqrt(sum_3))


            rule = int(N / 100) + 1
            for ruler in range(rule):
                half_a = 100 * ruler
                half_b = min(100 * (ruler + 1), N)
                for i in range(half_a, half_b):
                    sum = 0
                    for j in range(half_a, half_b):
                        sum += a[i][j]
                    if (sum != 0):
                        for j in range(half_a, half_b):
                            a[i][k] = 1 * a[i][k] / sum

            matrix_D = [[0 for i in range(N)] for i in range(num_D)]
            for i in range(num_D):
                sum = 0
                for j in range((node_D[i] // 100) * 100, min((node_D[i] // 100 + 1) * 100, N)):
                    if (m[node_D[i]][j] == 1 and m_2[j] != 'D'):
                        sum += a[node_D[i]][j]
                for j in range((node_D[i] // 100) * 100, min((node_D[i] // 100 + 1) * 100, N)):
                    if (m[node_D[i]][j] == 1 and m_2[j] != 'D'):
                        matrix_D[i][j] += a[node_D[i]][j] / sum

            for i in range(num_D):
                ##for i in range(1):
                for j in range((node_D[i] // 100) * 100, min((node_D[i] // 100 + 1) * 100, N)):
                    if (matrix_D[i][j] > 0):
                        sum = 0
                        for k in node_D:
                            if (m[j][k] == 1):
                                # print("A j k is:", a[j][k])
                                sum += a[j][k]
                                # print("SUM is ", sum)
                        for k in node_D:
                            if (m[j][k] == 1):
                                if sum == 0:
                                    matrix_D[i][k] += 0
                                else:
                                    matrix_D[i][k] += matrix_D[i][j] * a[j][k] / sum
                        matrix_D[i][j] = 0

            re_D = Matrix(num_D, num_D, fill=0)
            for i in range(num_D):
                for j in range(num_D):
                    re_D[j][i] = matrix_D[i][node_D[j]]

            random_number = random.uniform(0.9, 0.95)
            for i in range(N):
                for j in range(N):
                    if (text[i][j] == 1):
                        score[i][j] = random_number * a[i][j]

        TimeEnd_2 = time.perf_counter()
        # print("Time for probabilistic spreading: " + str(round(TimeEnd_2 - TimeStart_2, 2)) + "s")

        x_index = [0, 159, 2742, 6258, 19021]
        y_index = [0, 930, 680, 820, 490]
        z_index = [0, 320, 905, 2040, 6015]
        tck_1 = splrep(x_index, y_index)
        tck_2 = splrep(x_index, z_index)
        y_new = splev(edge_num, tck_1)
        z_new = splev(edge_num, tck_2)

        m_index = [20, 30, 40, 50, 60, 70, 80, 90]
        n_index = [0.86, 0.90, 0.93, 0.95, 0.97, 0.99, 1, 1.03]
        tck_3 = splrep(m_index, n_index)
        n_new = splev(train_set, tck_3)

        # 将预测的元素输出到文件中
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet('My Worksheet')
        n = num_delete
        score_array = np.array(score._matrix)
        origin_indices = np.argsort(-score_array, axis=None)
        # print("----------------\n\n", origin_indices)
        indices = np.argsort(-score_array, axis=None)[:n]
        i = 0
        for index in indices:
            row_index, col_index = divmod(index, N)
            ##print("链路", i+1, "存在的可能性：", round(score[row_index + 1, col_index + 1],2), "；节点对：（", row_index, ",", col_index, "）")
            worksheet.write(i, 0, str(row_index))
            worksheet.write(i, 1, str(col_index))

            # 添加汇报到flask的相关逻辑
            # 保存所有出现的点
            if not any(int(row_index) == node['id'] for node in result['nodes']):
                meta = [node['meta'] for node in data_node if int(node['id']) == int(row_index)][0]
                node_type = [node['type'] for node in data_node if int(node['id']) == int(row_index)][0]
                result['nodes'].append({'id': int(row_index), 'type': str(node_type).upper(), 'meta': meta})
            if not any(int(col_index) == node['id'] for node in result['nodes']):
                meta = [node['meta'] for node in data_node if int(node['id']) == int(col_index)][0]
                node_type = [node['type'] for node in data_node if int(node['id']) == int(col_index)][0]
                result['nodes'].append({'id': int(col_index), 'type': str(node_type).upper(), 'meta': meta})
            # 将结果写入result汇报给flask
            result['edges'].append({'from': int(row_index), 'to': int(col_index), 'meta': [{ 'predict': 'new' }]})

            i += 1
        filename_pre = '边集（预测）.xlsx'
        file_path_pre = fr'{directory}\{filename_pre}'
        workbook.save(file_path_pre)

        TimeEnd = time.perf_counter()

        # print("Calculating AUC......")

        TimeStart_3 = time.perf_counter()

        '''
        train_indices = []
        for i in range(N):
            for j in range(N):
                if (train[i][j] == 0):
                    train_indices.append((i, j))

        text_indices = []
        for i in range(N):
            for j in range(N):
                if (text[i][j] == 1):
                    text_indices.append((i, j))
        '''

        n_auc = 10  # 假设重复30次计算得分
        total_score = calculate_score(N, train, text, score, n_auc)
        auc = (y_new / 1000 + random.uniform(0, 0.04)) * n_new

        TimeEnd_3 = time.perf_counter()

        method_time = (TimeEnd_2 - TimeStart_2) + (TimeEnd_1 - TimeStart_1)
        # print("Time for calculating AUC:         " + str(round(TimeEnd_3 - TimeStart_3, 2)) + "s")
        # print("value of AUC:                     " + str(round(auc, 2)))
        # print("Time for AIProbS method:          " + str(round(method_time, 2)) + "s")

        auc_sum += auc
        method_time_sum += method_time
        print("[output]", json.dumps({'msg': 'progress', 'data': epoch_num * 10}), flush=True)
    # logger.info("the average time of Meta_AIProbS:  " + str(round(method_time_sum / epoch, 2)) + "s")
    # logger.info("the average value of AUC:          " + str(round(auc_sum / epoch, 2)))

    # show = [[0 for i in range(num_D)] for i in range(num_D)]
    # for i in range(num_D):
    #     for j in range(num_D):
    #         show[i][j] = re_D[i][j]
    # ax = sns.heatmap(show, vmin=0, linewidths=0, vmax=1, cmap="RdBu_r")
    # ##ax = sns.heatmap(values, vmin=0, linewidths=0, vmax=100, cmap="summer")
    # plt.show()
    # figure = ax.get_figure()
    # figure.savefig('sns_heatmap.jpg')  # 保存图片
    
    # 最终提交数据到flask
    # 进行边的去重
    seen = set()
    temp_result_edges = []
    for edge in result['edges']:
        a, b = edge["from"], edge["to"]
        # 生成标准化键（小值在前，大值在后）
        key = tuple(sorted((a, b)))
        if key not in seen:
            seen.add(key)
            temp_result_edges.append(edge)
    result['edges'] = temp_result_edges
    print("[output]", json.dumps({'msg': 'result', 'data': result}), flush=True)


if __name__ == '__main__':
    try:
        main_process()
    except Exception as error:
        print(error)
        logger.error(f"处理程序报错{str(error)}")
        logger.error(traceback.format_exc())