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
onnx
- 粉丝: 1w+
- 资源: 5626
最新资源
- 互助学习小程序的设计与实现+ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的电影交流平台--论文-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于微信小程序的高校餐厅食品留样管理系统-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 公交信息在线查询系统+ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的二手物品交易平台ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的快递管理平台的设计与实现ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的考研资料分享系统的设计与实现springboot-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于JAVA的微信食堂线上订餐小程序的设计与实现ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的课堂点名系统springboot-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 婚庆摄影小程序ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信的疫情期间学生请假与销假系统的设计与实现ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于java+ssm+mysql的农家乐内部管理系统开题报告.docx
- 基于微信小程序的美容院管理系统-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于微信小程序的青少年素质教育培训系统-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于微信平台的旅游出行必备商城小程序+ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于微信小程序的汽车销售系统的设计与实现springboot-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈