|
| 1 | +"""Benchmarks for graph-based ECT computations""" |
| 2 | +import numpy as np |
| 3 | +import time |
| 4 | +from ect import ECT, EmbeddedGraph |
| 5 | + |
| 6 | + |
| 7 | +def create_test_shape(num_points=1000, complexity=1): |
| 8 | + """Create test shape with varying complexity""" |
| 9 | + t = np.linspace(0, 2*np.pi, num_points) |
| 10 | + x = np.cos(t) |
| 11 | + y = np.sin(t) |
| 12 | + |
| 13 | + for i in range(2, complexity + 2): |
| 14 | + x += (1/i) * np.cos(i*t) |
| 15 | + y += (1/i) * np.sin(i*t) |
| 16 | + |
| 17 | + return np.column_stack([x, y]) |
| 18 | + |
| 19 | + |
| 20 | +def benchmark_graph_ect(num_runs=5): |
| 21 | + """Benchmark ECT computation on graphs""" |
| 22 | + results = {} |
| 23 | + |
| 24 | + configs = [ |
| 25 | + (100, 1), |
| 26 | + (1000, 1), |
| 27 | + (100, 3), |
| 28 | + (1000, 3), |
| 29 | + (10000, 3), |
| 30 | + ] |
| 31 | + |
| 32 | + for points, complexity in configs: |
| 33 | + shape = create_test_shape(points, complexity) |
| 34 | + G = EmbeddedGraph() |
| 35 | + G.add_cycle(shape) |
| 36 | + |
| 37 | + times = [] |
| 38 | + print( |
| 39 | + f"\nTesting shape with {points} points and complexity {complexity}") |
| 40 | + |
| 41 | + for _ in range(num_runs): |
| 42 | + start_time = time.time() |
| 43 | + myect = ECT(num_dirs=360, num_thresh=360) |
| 44 | + myect.calculateECT(G) |
| 45 | + times.append(time.time() - start_time) |
| 46 | + |
| 47 | + results[f'points_{points}_complexity_{complexity}'] = { |
| 48 | + 'mean_time': float(np.mean(times)), |
| 49 | + 'std_time': float(np.std(times)), |
| 50 | + 'min_time': float(np.min(times)), |
| 51 | + 'max_time': float(np.max(times)) |
| 52 | + } |
| 53 | + |
| 54 | + return results |
| 55 | + |
| 56 | + |
| 57 | +def benchmark_g_omega(num_runs=5): |
| 58 | + """Benchmark g_omega computation""" |
| 59 | + results = {} |
| 60 | + |
| 61 | + sizes = [100, 500, 1000] |
| 62 | + for size in sizes: |
| 63 | + shape = create_test_shape(size) |
| 64 | + G = EmbeddedGraph() |
| 65 | + G.add_cycle(shape) |
| 66 | + |
| 67 | + times = [] |
| 68 | + for _ in range(num_runs): |
| 69 | + start_time = time.time() |
| 70 | + for theta in np.linspace(0, 2*np.pi, 360): |
| 71 | + G.g_omega(theta) |
| 72 | + times.append(time.time() - start_time) |
| 73 | + |
| 74 | + results[f'size_{size}'] = { |
| 75 | + 'mean_time': float(np.mean(times)), |
| 76 | + 'std_time': float(np.std(times)) |
| 77 | + } |
| 78 | + |
| 79 | + return results |
0 commit comments