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、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
本科毕设基于DAG区块链的联邦学习框架实现去中心化和个性化源码+使用说明.zip (77个子文件)
__init__.py 0B
tip_selector_identifiers.py 141B
analysis
__init__.py 48B
tangle_analysator.py 1KB
poisoning-debug.ipynb 35KB
node.py 144B
graph.py 28KB
poets-findcluster.ipynb 1.05MB
femnist-findcluster.ipynb 1.04MB
clusters.py 3KB
__pycache__
graph.cpython-37.pyc 25KB
tangle_analysator.cpython-37.pyc 1KB
node.cpython-37.pyc 448B
__init__.cpython-37.pyc 201B
core
__init__.py 213B
tip_selection
__init__.py 213B
tip_selector.py 8KB
accuracy_tip_selector.py 4KB
max_depth_tip_selector.py 1KB
lazy_accuracy_tip_selector.py 1KB
__pycache__
accuracy_tip_selector.cpython-37.pyc 4KB
lazy_accuracy_tip_selector.cpython-37.pyc 2KB
tip_selector.cpython-37.pyc 5KB
__init__.cpython-37.pyc 418B
max_depth_tip_selector.cpython-37.pyc 1KB
transaction_store.py 1KB
node.py 12KB
tangle.py 3KB
transaction.py 269B
malicious_node.py 2KB
__pycache__
tangle.cpython-37.pyc 3KB
transaction_store.cpython-37.pyc 855B
node.cpython-37.pyc 10KB
malicious_node.cpython-37.pyc 2KB
transaction.cpython-37.pyc 645B
poison_type.cpython-37.pyc 402B
__init__.cpython-37.pyc 424B
poison_type.py 97B
run.py 9KB
environment.yml 2KB
使用说明.md 430B
lab
__init__.py 187B
lab_transaction_store.py 4KB
lab.py 11KB
main.py 889B
dataset.py 5KB
utils
utils.py 9KB
__init__.py 61B
language_utils.py 4KB
options.py 5KB
sampling.py 4KB
train_utils.py 5KB
test.py 5KB
Update.py 6KB
tip_selector_factory.py 2KB
__main__.py 118B
models
__init__.py 61B
language_utils.py 4KB
Nets.py 9KB
args.py 281B
config
poisoning_configuration.py 2KB
__init__.py 323B
run_configuration.py 1KB
model_configuration.py 4KB
tip_selector_configuration.py 4KB
node_configuration.py 1KB
lab_configuration.py 965B
__pycache__
run_configuration.cpython-37.pyc 1KB
tip_selector_configuration.cpython-37.pyc 3KB
lab_configuration.cpython-37.pyc 1KB
node_configuration.cpython-37.pyc 1KB
poisoning_configuration.cpython-37.pyc 2KB
__init__.cpython-37.pyc 540B
model_configuration.cpython-37.pyc 2KB
.gitignore 16B
__pycache__
__init__.cpython-39.pyc 150B
__init__.cpython-37.pyc 146B
共 77 条
- 1
资源评论
- m0_705486322024-05-29资源很赞,希望多一些这类资源。
- sudbdy1232024-05-17这个资源对我启发很大,受益匪浅,学到了很多,谢谢分享~onnx2024-05-22欢迎交流学习。。
- weixin_513859512024-05-10资源内容总结地很全面,值得借鉴,对我来说很有用,解决了我的燃眉之急。onnx2024-05-10谢谢认可~~~~~
- wyf13132024-05-06资源有很好的参考价值,总算找到了自己需要的资源啦。onnx2024-05-10enen ,互相学习下
- 南下1632024-03-07总算找到了想要的资源,搞定遇到的大问题,赞赞赞!onnx2024-04-29感谢支持和认可~~
onnx
- 粉丝: 9645
- 资源: 5598
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功