import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import ast
import os
from collections import Counter
import csv
from scipy.interpolate import make_interp_spline, BSpline
from sknetwork.clustering import Louvain, modularity
from .node import Node
class Graph:
def __init__(self, data, generation, analysis_output_dir=None):
self.nodes = [Node(n["id"], n["parents"], n["metadata"]) for n in data["nodes"] if
n["metadata"]["time"] <= generation]
self.generation = generation
self.analysis_output_dir = analysis_output_dir
def print_statistics(self, include_reference_statistics=True):
#### Helper methods for printing
def _print(text):
if self.analysis_output_dir:
analysis_filepath = os.path.join(self.analysis_output_dir, "statistics.txt")
with open(analysis_filepath, "a+") as statistics_file:
print(text, file=statistics_file)
else:
print(text)
def _print_multiple_statistics_lines(labels, data, text):
for l, d in zip(labels, data):
_print(text % (l, d))
_print("")
#### Get statistics data and print it
statistics = self._get_statistics_data(include_reference_statistics)
_print(f"Average accuracy in the last 5 rounds: {statistics['average_accuracy_last_5_rounds']}")
_print('')
_print("Average clients per round: %f" % statistics["average_clients_per_round"])
_print("")
_print("Average parents per round (not including round 1): %f" % statistics["average_parents_per_round"])
_print("")
# Pureness
_print_multiple_statistics_lines(
*statistics["average_pureness_per_round_approvals"],
"Average pureness (approvals) for %s per round: %f")
if (include_reference_statistics):
_print_multiple_statistics_lines(
*statistics["average_pureness_per_round_ref_tx"],
"Average pureness (ref_tx) for %s per round: %f")
# Information gain
_print_multiple_statistics_lines(
*statistics["information_gain_per_round_approvals"],
"Average information gain (approvals) for %s per round: %f")
if (include_reference_statistics):
_print_multiple_statistics_lines(
*statistics["information_gain_per_round_ref_tx"],
"Average information gain (ref_tx) for %s per round: %f")
def plot_transactions_per_round(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
data = self._get_num_transactions_per_round()
self._line_plot(
title='Number of transactions per round',
data_arrays=[data],
y_label="Number of transactions",
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_parents_per_round(self, plot_first_round=False, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
data = self._get_mean_parents_per_round(plot_first_round)
self._line_plot(
title='Mean number of parents per round (%s round 1)' % ("including" if plot_first_round else "excluding"),
data_arrays=[data],
y_label="Mean number of parents",
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_accuracy_boxplot(self, print_avg_acc=False, plot_axis_labels=True, plot_for_paper=False, ymax=1.0, ylabel=""):
data = self._prepare_acc_data()
plt.boxplot(data)
if print_avg_acc:
plt.plot([i for i in range(1, self.generation + 1)], [np.mean(x) for x in data])
# Settings for plot
plt.title('Accuracy per round')
# Fix y axis data range to [0, 1]
plt.ylim([0, ymax])
if plot_axis_labels:
plt.xlabel("Round")
plt.xticks([i for i in range(1, self.generation + 1)],
[i if i % 10 == 0 else '' for i in range(1, self.generation + 1)])
plt.ylabel(ylabel)
def save_or_plot_fig(format="png"):
if self.analysis_output_dir:
analysis_filepath = os.path.join(self.analysis_output_dir, f"accuracy_per_round.{format}")
plt.savefig(analysis_filepath, format=format)
else:
plt.show()
save_or_plot_fig()
if plot_for_paper:
# Remove title
plt.title("")
save_or_plot_fig(format="pdf")
# Clear canvas for next diagram
plt.clf()
def plot_information_gain_ref_tx(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
labels, data_arrays = self._get_information_gain_ref_tx()
# Plot data
self._line_plot(
title='Information gain (reference tx)',
data_arrays=data_arrays,
labels=labels,
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_information_gain_approvals(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
labels, data_arrays = self._get_information_gain_approvals()
# Plot data
self._line_plot(
title='Information gain (approvals)',
data_arrays=data_arrays,
labels=labels,
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_pureness_ref_tx(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
labels, data_arrays = self._prepare_reference_pureness(compare_to_ref_tx=True)
self._line_plot(
title='Cluster pureness (reference transaction)',
data_arrays=data_arrays,
labels=labels,
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_pureness_approvals(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
labels, data_arrays = self._prepare_reference_pureness()
self._line_plot(
title='Cluster pureness (approvals)',
data_arrays=data_arrays,
labels=labels,
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_avg_age_difference_ref_tx(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
avg_age_difference_per_round = self._prepare_data_avg_age_difference_to_ref_tx()
self._line_plot(
title='Average age difference to reference transaction per round',
data_arrays=[avg_age_difference_per_round],
y_label='Age in rounds',
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_modularity_per_round(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
m = self._prepare_modularity()
self._line_plot(
title='Modularity per round',
data_arrays=[[x for x, _, _, _ in m]],
y_label='modularity',
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_num_modules_per_round(self, smooth_line=False, plot_axis_labels=True, plot_for_paper=False):
m = self._prepare_modularity()
self._line_plot(
title='Modules per round',
data_arrays=[[x for _, x, _, _ in m]],
y_label='#modules',
smooth_line=smooth_line,
plot_axis_labels=plot_axis_labels,
plot_for_paper=plot_for_paper)
def plot_misclassification_per_round(se
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 本科毕设基于DAG区块链的联邦学习框架实现去中心化和个性化源码+使用说明.zip 本科毕设。基于DAG区块链的联邦学习框架,实现了去中心化和个性化。 ## 安装 使用 conda 安装 环境 ```bash cd dagfl conda env create -f environment.yml ``` ## 运行测试 要运行测试,运行以下命令 ```bash cd .. python dagfl/run.py ``` 在 run.py 改变参数,如数据集的目录等。程序会自动下载所需的数据集。结果会放在在experiments目录下。 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论


























收起资源包目录


























































































共 77 条
- 1

onnx
- 粉丝: 1w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 湖南省2019年对口招生考试计算机应用类专业综合知识和答案复习进程(1).doc
- 互联网背景下图书资料信息化管理的实施分析(1).docx
- 城市轨道交通信号设备铁路信号基础(1).pptx
- 本科毕业设计论文--基于matlab的2psk系统设计课程设计(1).doc
- 第3章--电子商务的框架与电子商务系统的组成(1).ppt
- 220kv变电站综合自动化110kv第八回出线保护配置及整定计算(建筑电气毕业设计说明论文100页)(1).doc
- 基于物联网技术的物流企业转型升级研究(1).docx
- 互联网+时代大学英语教师职业能力可持续发展研究(1).docx
- 基于gis的物联网监测信息查询与可视化毕业(论文)设计论文(1).doc
- 本科毕业设计--基于单片机的温控风扇的设计(1).doc
- 人工智能介绍(3)(1).ppt
- 计算机远程维护-毕业设计论文(1).doc
- plc课程设计电镀自动生产线控制大学论文(1)(1).doc
- 计算机二级C语言章节练习题及答案(1).doc
- 2019年常用软件的学习总结范文(1).doc
- java乾美美容院管理系统(源码、数据库文档、说明文档、论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页