clean up after lab1

This commit is contained in:
2026-10-24 20:31:25 +03:00
parent 1be21c4021
commit 538e7b7784
+13 -74
View File
@@ -2,6 +2,8 @@ import matplotlib.pyplot as plt
import networkx as nx import networkx as nx
import pandas as pd import pandas as pd
write_files = False
nn_total_levels = 0 nn_total_levels = 0
def nn(cluster_no, node_name): def nn(cluster_no, node_name):
@@ -17,10 +19,16 @@ def join_nodes(g, a, b, target_degree = 6):
g.add_edge(a, b) g.add_edge(a, b)
def start_file(p): def start_file(p):
if not write_files:
return
index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C'] index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C']
open(p, 'w').write(','.join(index) + '\n') open(p, 'w').write(','.join(index) + '\n')
def write_file(p, data): def write_file(p, data):
if not write_files:
return
index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C'] index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C']
open(p, 'a').write(','.join([str(data[i]) for i in index]) + '\n') open(p, 'a').write(','.join([str(data[i]) for i in index]) + '\n')
@@ -112,50 +120,10 @@ def visualize(g):
def draw_edges_between(a, b, lvl_min = 0, lvl_max = False, color = 'black', width = 1.0, debug = False, direct = False): def draw_edges_between(a, b, lvl_min = 0, lvl_max = False, color = 'black', width = 1.0, debug = False, direct = False):
edgelist = [e for e in g.edges() if all([True if (a in i) or (b in i) else False for i in e])] edgelist = [e for e in g.edges() if all([True if (a in i) or (b in i) else False for i in e])]
if debug:
print(edgelist)
if lvl_max.__class__ == bool: if lvl_max.__class__ == bool:
if debug:
print("Running 1 branch")
edgelist = [(pa, pb) for pa, pb in edgelist if not (abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min)] edgelist = [(pa, pb) for pa, pb in edgelist if not (abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min)]
'''
for p in edgelist:
if abs(lvl(pa) - lvl(pb)) < lvl_min:
edgelist.remove((pa, pb))
'''
else: else:
if debug:
print(f"Running 2 branch on {edgelist}")
edgelist = [(pa, pb) for pa, pb in edgelist if not ((abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min) or (abs(lvl(pa) - lvl(pb)) > lvl_max and nn_total_levels - abs(lvl(pa) - lvl(pb)) > lvl_max))] edgelist = [(pa, pb) for pa, pb in edgelist if not ((abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min) or (abs(lvl(pa) - lvl(pb)) > lvl_max and nn_total_levels - abs(lvl(pa) - lvl(pb)) > lvl_max))]
'''
for pa, pb in edgelist:
if debug:
print(f"Processing ({pa}, {pb})")
if abs(lvl(pa) - lvl(pb)) < lvl_min or abs(lvl(pa) - lvl(pb)) > lvl_max:
edgelist.remove((pa, pb))
if debug:
print(f'Discarding {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})')
elif debug:
print(f'Leaving {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})')
for pa, pb in edgelist:
if debug:
print(f"Processing ({pa}, {pb})")
if abs(lvl(pa) - lvl(pb)) < lvl_min or abs(lvl(pa) - lvl(pb)) > lvl_max:
edgelist.remove((pa, pb))
if debug:
print(f'Discarding {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})')
elif debug:
print(f'Leaving {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})')
'''
if debug:
print(edgelist)
if direct: if direct:
nx.draw_networkx_edges(g, exp_pos, edgelist = edgelist, width = width, edge_color = color) nx.draw_networkx_edges(g, exp_pos, edgelist = edgelist, width = width, edge_color = color)
@@ -181,31 +149,16 @@ def visualize(g):
nx.draw_networkx_nodes(g, exp_pos, node_size = 600) nx.draw_networkx_nodes(g, exp_pos, node_size = 600)
nx.draw_networkx_labels(g, exp_pos, font_size = 8) nx.draw_networkx_labels(g, exp_pos, font_size = 8)
#nx.draw_networkx_edges(g, exp_pos, edgelist = [e for e in g.edges() if all([True if ('ls' in i) or ('rs' in i) else False for i in e])], connectionstyle='arc3,rad=0.2', arrows=True)
# cluster # cluster
draw_edges_between('', '', lvl_max = 0, color = '#888888', width = 0.5, direct = True) draw_edges_between('', '', lvl_max = 0, color = '#888888', width = 0.5, direct = True)
# S => 4
draw_edges_between('ls', 'rs', lvl_max = 1, color = '#33AAAA', width = 2)
draw_edges_between('lu', 'lu', lvl_max = 1, color = '#AAAA33', width = 1)
draw_edges_between('ru', 'ru', lvl_max = 1, color = '#AAAA33', width = 1)
draw_edges_between('ld', 'ld', lvl_max = 1, color = '#AAAA33', width = 1)
draw_edges_between('rd', 'rd', lvl_max = 1, color = '#AAAA33', width = 1)
# S => 6
draw_edges_between('lu', 'lu', lvl_min = 2, color = '#FF0000', width = 1.5)
draw_edges_between('ld', 'ld', lvl_min = 2, color = '#FFFF00', width = 1.5)
draw_edges_between('ru', 'ru', lvl_min = 2, color = '#00FF00', width = 1.5)
draw_edges_between('rd', 'rd', lvl_min = 2, color = '#00FFFF', width = 1.5)
draw_edges_between('ls', 'ls', lvl_min = 2, color = '#0000FF', width = 1.5)
draw_edges_between('rs', 'rs', lvl_min = 2, color = '#FF00FF', width = 1.5)
draw_edges_between('m' , 'm' , lvl_min = 2, color = '#000000', width = 1.5)
plt.show() plt.show()
def main(): def main():
global write_files
write_files = False
i = 0 i = 0
start_file('data.csv') start_file('data.csv')
@@ -218,25 +171,12 @@ def main():
r = analyze(g) r = analyze(g)
r.update({"i": i}) r.update({"i": i})
'''
if d.__class__ == None.__class__:
d = pd.DataFrame(r, index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C'])
else:
d.add(r)
'''
print(r) print(r)
write_file('data.csv', r) write_file('data.csv', r)
if g.number_of_nodes() > 1000: if g.number_of_nodes() > 1000:
break break
#print(r)
#nx.draw(g, with_labels = True)
#plt.show()
#d.to_csv('data.csv')
def test(): def test():
g = build_network(15) g = build_network(15)
print(analyze(g)) print(analyze(g))
@@ -251,8 +191,7 @@ def test2():
print(set([v for i, v in g.degree()])) print(set([v for i, v in g.degree()]))
def test3(): def test3():
g = build_network(3) g = build_cluster(0)
#print(g.adj['0-ls'])
visualize(g) visualize(g)
if __name__=="__main__": if __name__=="__main__":