From 538e7b778462df209c74d47d0de08f69678aad33 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sat, 24 Oct 2026 20:31:25 +0300 Subject: [PATCH] clean up after lab1 --- main.py | 87 +++++++++------------------------------------------------ 1 file changed, 13 insertions(+), 74 deletions(-) diff --git a/main.py b/main.py index 0017551..2c11c5e 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import matplotlib.pyplot as plt import networkx as nx import pandas as pd +write_files = False + nn_total_levels = 0 def nn(cluster_no, node_name): @@ -17,10 +19,16 @@ def join_nodes(g, a, b, target_degree = 6): g.add_edge(a, b) def start_file(p): + if not write_files: + return + index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C'] open(p, 'w').write(','.join(index) + '\n') def write_file(p, data): + if not write_files: + return + index = ['i', 'N', 'E', 'S', 'D', 'D_avg', 'T', 'C'] 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): 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 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)] - ''' - for p in edgelist: - if abs(lvl(pa) - lvl(pb)) < lvl_min: - edgelist.remove((pa, pb)) - ''' 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))] - ''' - 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: 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_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 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() def main(): + global write_files + + write_files = False + i = 0 start_file('data.csv') @@ -218,25 +171,12 @@ def main(): r = analyze(g) 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) write_file('data.csv', r) if g.number_of_nodes() > 1000: break - #print(r) - #nx.draw(g, with_labels = True) - #plt.show() - - #d.to_csv('data.csv') - def test(): g = build_network(15) print(analyze(g)) @@ -251,8 +191,7 @@ def test2(): print(set([v for i, v in g.degree()])) def test3(): - g = build_network(3) - #print(g.adj['0-ls']) + g = build_cluster(0) visualize(g) if __name__=="__main__":