masterthesis/codes/node-ranking/reduce_edges.py
Valentin Brandl 0d216a53ff Foo
2022-04-19 18:38:01 +02:00

53 lines
1.3 KiB
Python

#!/usr/bin/env python3
from datetime import datetime
from random import sample, seed
import rank_with_churn
import glob
from node_ranking import (
# page_rank,
# sensor_rank,
# find_rank,
parse_csv,
# csv_loader,
build_graph,
# Node,
# RankedNode,
)
def load_data(path):
data = []
with open(path, 'r') as f:
for line in f.readlines():
data.append(parse_csv(line))
return data
def edges_from(g, node):
return list(filter(lambda e: e[1] == node, g.edges()))
def remove_edges(path, percentage):
when = datetime.fromtimestamp(float(path.split('/')[-1][:-4]))
print(f'{when=}, {percentage=}')
edges = load_data(path)
g = build_graph(edges)
edges = edges_from(g, rank_with_churn.KNOWN)
for_removal = sample(edges, int(len(edges) * percentage))
for edge in for_removal:
g.remove_edge(edge[0], edge[1])
with open(f'./edges_reduced/{percentage:.02f}/{when.timestamp()}.txt', 'w') as f:
for [s, d] in g.edges():
row = f'{s.node.ip},{s.node.port},{d.node.ip},{d.node.port}\n'
f.write(row)
percentages = [0.0, 0.3, 0.5, 0.75]
# percentages = [0.0]
def main():
for percentage in percentages:
for file in glob.glob('./edges/*.txt'):
remove_edges(file, percentage)
if __name__ == '__main__':
main()