masterthesis/codes/node-ranking/plot_with_churn.py
2022-04-15 17:21:44 +02:00

82 lines
2.2 KiB
Python

#!/usr/bin/env python3
from datetime import datetime
import numpy as np
import glob
import json
import matplotlib.pyplot as plt
def load_json(path):
with open(path, 'r') as f:
return json.load(f)
# foo/0.5/10/1618959600.0.json
def plot(path):
data = {}
# edges = 0
# initial_rank = 0.0
for file in glob.glob(path):
path_split = file.split('/')
initial_rank = float(path_split[1])
edges = float(path_split[2])
when = datetime.fromtimestamp(float(path_split[-1][:-5]))
data[when] = load_json(file)
x = []
sr_avg = []
sr_avg_filtered = []
sr_known = []
sr_perc_50 = []
pr_avg = []
pr_avg_filtered = []
pr_known = []
pr_perc_50 = []
pr_known = []
for when, d in sorted(data.items(), key=lambda kv: kv[0]):
x.append(when)
sr_avg.append(d['sr_avg'])
sr_avg_filtered.append(d['sr_avg_filtered'])
sr_known.append(d['sr_known'])
sr_perc_50.append(d['sr_perc_50'])
pr_avg.append(d['pr_avg'])
pr_avg_filtered.append(d['pr_avg_filtered'])
pr_known.append(d['pr_known'])
pr_perc_50.append(d['pr_perc_50'])
fig, ax = plt.subplots()
plt.yticks(np.arange(0, 0.5, step=0.05))
ax.plot(x, sr_avg_filtered, label='Average SR')
ax.plot(x, sr_known, label='SR of Sensor')
# ax.plot(x, sr_perc_50, label='sr_perc_50')
ax.set_title(f'SensorRank after adding {edges * 100}% edges')
fig.autofmt_xdate()
fig.legend()
plt.savefig(f'perc.bak/{initial_rank}/{edges}/sr.png')
fig, ax = plt.subplots()
plt.yticks(np.arange(0, 0.5, step=0.05))
ax.plot(x, pr_avg_filtered, label='Average PR')
ax.plot(x, pr_known, label='PR of Sensor')
# ax.plot(x, pr_perc_50, label='pr_perc_50')
ax.set_title(f'PageRank after adding {edges * 100}% edges')
fig.autofmt_xdate()
fig.legend()
plt.savefig(f'perc.bak/{initial_rank}/{edges}/pr.png')
def main():
# for perc in [0.75, 0.9, 1.0, 1.5, 2.0, 2.5]:
# for perc in [0.5, 0.75, 0.8, 1.0, 1.5, 2.0, 2.5]:
for perc in [0.75, 0.9, 1.0, 1.5, 2.0, 2.5]:
plot(f'perc.bak/0.5/{perc}/*.json')
if __name__ == '__main__':
main()