diff options
| -rw-r--r-- | app.py | 71 | ||||
| -rw-r--r-- | index.html | 7 |
2 files changed, 66 insertions, 12 deletions
| @@ -2,6 +2,7 @@ import os | |||
| 2 | import subprocess | 2 | import subprocess |
| 3 | import tempfile | 3 | import tempfile |
| 4 | import json | 4 | import json |
| 5 | import csv | ||
| 5 | from flask import Flask, request, jsonify, send_from_directory | 6 | from flask import Flask, request, jsonify, send_from_directory |
| 6 | 7 | ||
| 7 | app = Flask(__name__) | 8 | app = Flask(__name__) |
| @@ -10,6 +11,55 @@ app = Flask(__name__) | |||
| 10 | TEST_ENVIRONMENT = os.getenv("BONSAI_TEST_MODE", "true").lower() == "true" | 11 | TEST_ENVIRONMENT = os.getenv("BONSAI_TEST_MODE", "true").lower() == "true" |
| 11 | 12 | ||
| 12 | 13 | ||
| 14 | def generate_matrix_from_edges(edges_file_path, matrix_file_path): | ||
| 15 | """Generate a square matrix file from edges.csv.""" | ||
| 16 | try: | ||
| 17 | # First pass: find all unique nodes | ||
| 18 | nodes = set() | ||
| 19 | edges = [] | ||
| 20 | |||
| 21 | with open(edges_file_path, "r") as f: | ||
| 22 | reader = csv.reader(f) | ||
| 23 | for row in reader: | ||
| 24 | if len(row) >= 3: | ||
| 25 | source = int(row[0]) | ||
| 26 | target = int(row[1]) | ||
| 27 | weight = float(row[2]) | ||
| 28 | nodes.add(source) | ||
| 29 | nodes.add(target) | ||
| 30 | edges.append((source, target, weight)) | ||
| 31 | |||
| 32 | # Create sorted list of nodes | ||
| 33 | node_list = sorted(list(nodes)) | ||
| 34 | num_nodes = len(node_list) | ||
| 35 | |||
| 36 | # Create node index mapping | ||
| 37 | node_to_index = {node: i for i, node in enumerate(node_list)} | ||
| 38 | |||
| 39 | # Initialize matrix with zeros | ||
| 40 | matrix = [[0.0 for _ in range(num_nodes)] for _ in range(num_nodes)] | ||
| 41 | |||
| 42 | # Fill matrix with edge weights | ||
| 43 | for source, target, weight in edges: | ||
| 44 | source_idx = node_to_index[source] | ||
| 45 | target_idx = node_to_index[target] | ||
| 46 | matrix[source_idx][target_idx] = weight | ||
| 47 | matrix[target_idx][source_idx] = weight # Assuming undirected graph | ||
| 48 | |||
| 49 | # Write matrix to file | ||
| 50 | with open(matrix_file_path, "w") as f: | ||
| 51 | for row in matrix: | ||
| 52 | f.write(" ".join(map(str, row)) + "\n") | ||
| 53 | |||
| 54 | except Exception as e: | ||
| 55 | print(f"Error generating matrix: {e}") | ||
| 56 | # Create a simple 3x3 dummy matrix for testing | ||
| 57 | with open(matrix_file_path, "w") as f: | ||
| 58 | f.write("0.0 0.5 0.0\n") | ||
| 59 | f.write("0.5 0.0 0.3\n") | ||
| 60 | f.write("0.0 0.3 0.0\n") | ||
| 61 | |||
| 62 | |||
| 13 | @app.route("/") | 63 | @app.route("/") |
| 14 | def index(): | 64 | def index(): |
| 15 | """Serve the main HTML page.""" | 65 | """Serve the main HTML page.""" |
| @@ -87,9 +137,7 @@ def generate_files(): | |||
| 87 | print( | 137 | print( |
| 88 | f"WARNING: {error_msg}. Creating dummy files for testing." | 138 | f"WARNING: {error_msg}. Creating dummy files for testing." |
| 89 | ) | 139 | ) |
| 90 | edges_content = ( | 140 | edges_content = "1,2,0.5\n2,3,0.3\n1,3,0.8\n" |
| 91 | "source,target,weight\nnode1,node2,0.5\nnode2,node3,0.3\n" | ||
| 92 | ) | ||
| 93 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" | 141 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" |
| 94 | 142 | ||
| 95 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: | 143 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: |
| @@ -103,9 +151,7 @@ def generate_files(): | |||
| 103 | error_msg = "Bonsai execution timed out after 30 seconds" | 151 | error_msg = "Bonsai execution timed out after 30 seconds" |
| 104 | if TEST_ENVIRONMENT: | 152 | if TEST_ENVIRONMENT: |
| 105 | print(f"WARNING: {error_msg}. Creating dummy files for testing.") | 153 | print(f"WARNING: {error_msg}. Creating dummy files for testing.") |
| 106 | edges_content = ( | 154 | edges_content = "1,2,0.5\n2,3,0.3\n1,3,0.8\n" |
| 107 | "source,target,weight\nnode1,node2,0.5\nnode2,node3,0.3\n" | ||
| 108 | ) | ||
| 109 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" | 155 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" |
| 110 | 156 | ||
| 111 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: | 157 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: |
| @@ -119,9 +165,7 @@ def generate_files(): | |||
| 119 | error_msg = "Bonsai tool not found. Please ensure Bonsai is installed and available." | 165 | error_msg = "Bonsai tool not found. Please ensure Bonsai is installed and available." |
| 120 | if TEST_ENVIRONMENT: | 166 | if TEST_ENVIRONMENT: |
| 121 | print(f"WARNING: {error_msg}. Creating dummy files for testing.") | 167 | print(f"WARNING: {error_msg}. Creating dummy files for testing.") |
| 122 | edges_content = ( | 168 | edges_content = "1,2,0.5\n2,3,0.3\n1,3,0.8\n" |
| 123 | "source,target,weight\nnode1,node2,0.5\nnode2,node3,0.3\n" | ||
| 124 | ) | ||
| 125 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" | 169 | nodes_content = "id,label,x,y\nnode1,Node 1,0,0\nnode2,Node 2,1,1\nnode3,Node 3,2,0\n" |
| 126 | 170 | ||
| 127 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: | 171 | with open(os.path.join(output_dir, "edges.csv"), "w") as f: |
| @@ -139,6 +183,15 @@ def generate_files(): | |||
| 139 | with open(filepath, "r") as f: | 183 | with open(filepath, "r") as f: |
| 140 | files[filename] = f.read() | 184 | files[filename] = f.read() |
| 141 | 185 | ||
| 186 | # Generate matrix file if edges.csv exists | ||
| 187 | edges_path = os.path.join(output_dir, "edges.csv") | ||
| 188 | matrix_path = os.path.join(output_dir, "matrix.txt") | ||
| 189 | if os.path.exists(edges_path): | ||
| 190 | generate_matrix_from_edges(edges_path, matrix_path) | ||
| 191 | if os.path.exists(matrix_path): | ||
| 192 | with open(matrix_path, "r") as f: | ||
| 193 | files["matrix.txt"] = f.read() | ||
| 194 | |||
| 142 | if not files: | 195 | if not files: |
| 143 | return ( | 196 | return ( |
| 144 | jsonify({"error": "No output files were generated by Bonsai"}), | 197 | jsonify({"error": "No output files were generated by Bonsai"}), |
| @@ -86,9 +86,10 @@ | |||
| 86 | </p> | 86 | </p> |
| 87 | 87 | ||
| 88 | <p> | 88 | <p> |
| 89 | The tool generates two CSV files: <code>edges.csv</code> containing | 89 | The tool generates three files: <code>edges.csv</code> containing |
| 90 | the network connections and latencies, and <code>nodes.csv</code> containing | 90 | the network connections and latencies, <code>nodes.csv</code> containing |
| 91 | the node information and positions. | 91 | the node information and positions, and <code>matrix.txt</code> containing |
| 92 | a square adjacency matrix representation of the network. | ||
| 92 | </p> | 93 | </p> |
| 93 | </section> | 94 | </section> |
| 94 | 95 | ||
