|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example Python script demonstrating how to plot trace series exported from Rust. |
| 4 | +
|
| 5 | +This script reads the JSON/CSV files generated by the plot_traces example |
| 6 | +and creates visualizations using matplotlib. |
| 7 | +
|
| 8 | +Requirements: |
| 9 | + pip install matplotlib polars |
| 10 | +
|
| 11 | +Usage: |
| 12 | + python examples/plot_example.py |
| 13 | +""" |
| 14 | + |
| 15 | +import json |
| 16 | +import polars as pl |
| 17 | +import matplotlib.pyplot as plt |
| 18 | + |
| 19 | + |
| 20 | +def plot_bw_trace_from_json(filename, title): |
| 21 | + """Plot bandwidth trace from JSON file.""" |
| 22 | + with open(filename, "r") as f: |
| 23 | + data = json.load(f) |
| 24 | + |
| 25 | + # Extract data points |
| 26 | + times = [] |
| 27 | + bandwidths = [] |
| 28 | + |
| 29 | + for point in data: |
| 30 | + start_time = point["start_time"] |
| 31 | + duration = point["duration"] |
| 32 | + bw_mbps = point["value"]["bps"] / 1_000_000 |
| 33 | + |
| 34 | + # Create step plot by adding points at start and end of each segment |
| 35 | + times.append(start_time) |
| 36 | + bandwidths.append(bw_mbps) |
| 37 | + times.append(start_time + duration) |
| 38 | + bandwidths.append(bw_mbps) |
| 39 | + |
| 40 | + plt.figure(figsize=(10, 5)) |
| 41 | + plt.plot(times, bandwidths, linewidth=2) |
| 42 | + plt.xlabel("Time (seconds)") |
| 43 | + plt.ylabel("Bandwidth (Mbps)") |
| 44 | + plt.title(title) |
| 45 | + plt.grid(True, alpha=0.3) |
| 46 | + plt.tight_layout() |
| 47 | + return plt |
| 48 | + |
| 49 | + |
| 50 | +def plot_bw_trace_from_csv(filename, title): |
| 51 | + """Plot bandwidth trace from CSV file.""" |
| 52 | + df = pl.read_csv(filename) |
| 53 | + |
| 54 | + # Create step plot |
| 55 | + times = [] |
| 56 | + bandwidths = [] |
| 57 | + |
| 58 | + for row in df.iter_rows(named=True): |
| 59 | + start_time = row["start_time_secs"] |
| 60 | + duration = row["duration_secs"] |
| 61 | + bw_mbps = row["bandwidth_bps"] / 1_000_000 |
| 62 | + |
| 63 | + times.append(start_time) |
| 64 | + bandwidths.append(bw_mbps) |
| 65 | + times.append(start_time + duration) |
| 66 | + bandwidths.append(bw_mbps) |
| 67 | + |
| 68 | + plt.figure(figsize=(10, 5)) |
| 69 | + plt.plot(times, bandwidths, linewidth=2) |
| 70 | + plt.xlabel("Time (seconds)") |
| 71 | + plt.ylabel("Bandwidth (Mbps)") |
| 72 | + plt.title(title) |
| 73 | + plt.grid(True, alpha=0.3) |
| 74 | + plt.tight_layout() |
| 75 | + return plt |
| 76 | + |
| 77 | + |
| 78 | +def plot_delay_per_packet_from_csv(filename, title): |
| 79 | + """Plot per-packet delay from CSV file.""" |
| 80 | + df = pl.read_csv(filename) |
| 81 | + |
| 82 | + plt.figure(figsize=(10, 5)) |
| 83 | + plt.plot( |
| 84 | + df["packet_index"].to_list(), |
| 85 | + (df["delay_secs"] * 1000).to_list(), |
| 86 | + "o-", |
| 87 | + linewidth=2, |
| 88 | + ) |
| 89 | + plt.xlabel("Packet Index") |
| 90 | + plt.ylabel("Delay (ms)") |
| 91 | + plt.title(title) |
| 92 | + plt.grid(True, alpha=0.3) |
| 93 | + plt.tight_layout() |
| 94 | + return plt |
| 95 | + |
| 96 | + |
| 97 | +def create_multi_plot(): |
| 98 | + """Create a multi-panel plot showing different trace types.""" |
| 99 | + fig, axes = plt.subplots(2, 2, figsize=(15, 10)) |
| 100 | + |
| 101 | + # Plot 1: Static bandwidth |
| 102 | + with open("static_bw.json", "r") as f: |
| 103 | + data = json.load(f) |
| 104 | + times, bandwidths = [], [] |
| 105 | + for point in data: |
| 106 | + start = point["start_time"] |
| 107 | + dur = point["duration"] |
| 108 | + bw = point["value"]["bps"] / 1_000_000 |
| 109 | + times.extend([start, start + dur]) |
| 110 | + bandwidths.extend([bw, bw]) |
| 111 | + axes[0, 0].plot(times, bandwidths, linewidth=2) |
| 112 | + axes[0, 0].set_xlabel("Time (s)") |
| 113 | + axes[0, 0].set_ylabel("Bandwidth (Mbps)") |
| 114 | + axes[0, 0].set_title("Static Bandwidth Trace") |
| 115 | + axes[0, 0].grid(True, alpha=0.3) |
| 116 | + |
| 117 | + # Plot 2: Sawtooth bandwidth |
| 118 | + df = pl.read_csv("sawtooth_bw.csv") |
| 119 | + times, bandwidths = [], [] |
| 120 | + for row in df.iter_rows(named=True): |
| 121 | + start = row["start_time_secs"] |
| 122 | + dur = row["duration_secs"] |
| 123 | + bw = row["bandwidth_bps"] / 1_000_000 |
| 124 | + times.extend([start, start + dur]) |
| 125 | + bandwidths.extend([bw, bw]) |
| 126 | + axes[0, 1].plot(times, bandwidths, linewidth=2, color="orange") |
| 127 | + axes[0, 1].set_xlabel("Time (s)") |
| 128 | + axes[0, 1].set_ylabel("Bandwidth (Mbps)") |
| 129 | + axes[0, 1].set_title("Sawtooth Bandwidth Trace") |
| 130 | + axes[0, 1].grid(True, alpha=0.3) |
| 131 | + |
| 132 | + # Plot 3: Normal bandwidth |
| 133 | + df = pl.read_csv("normal_bw.csv") |
| 134 | + times, bandwidths = [], [] |
| 135 | + for row in df.iter_rows(named=True): |
| 136 | + start = row["start_time_secs"] |
| 137 | + dur = row["duration_secs"] |
| 138 | + bw = row["bandwidth_bps"] / 1_000_000 |
| 139 | + times.extend([start, start + dur]) |
| 140 | + bandwidths.extend([bw, bw]) |
| 141 | + axes[1, 0].plot(times, bandwidths, linewidth=2, color="green") |
| 142 | + axes[1, 0].set_xlabel("Time (s)") |
| 143 | + axes[1, 0].set_ylabel("Bandwidth (Mbps)") |
| 144 | + axes[1, 0].set_title("Normalized Bandwidth Trace") |
| 145 | + axes[1, 0].grid(True, alpha=0.3) |
| 146 | + |
| 147 | + # Plot 4: Repeated pattern |
| 148 | + df = pl.read_csv("repeated_bw.csv") |
| 149 | + times, bandwidths = [], [] |
| 150 | + for row in df.iter_rows(named=True): |
| 151 | + start = row["start_time_secs"] |
| 152 | + dur = row["duration_secs"] |
| 153 | + bw = row["bandwidth_bps"] / 1_000_000 |
| 154 | + times.extend([start, start + dur]) |
| 155 | + bandwidths.extend([bw, bw]) |
| 156 | + axes[1, 1].plot(times, bandwidths, linewidth=2, color="red") |
| 157 | + axes[1, 1].set_xlabel("Time (s)") |
| 158 | + axes[1, 1].set_ylabel("Bandwidth (Mbps)") |
| 159 | + axes[1, 1].set_title("Repeated Pattern Trace") |
| 160 | + axes[1, 1].grid(True, alpha=0.3) |
| 161 | + |
| 162 | + plt.tight_layout() |
| 163 | + return plt |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + print("Creating plots from exported trace data...") |
| 168 | + |
| 169 | + # Create individual plots |
| 170 | + print("1. Plotting static bandwidth trace from JSON...") |
| 171 | + plt1 = plot_bw_trace_from_json("static_bw.json", "Static Bandwidth Trace") |
| 172 | + plt1.savefig("plot_static_bw.png", dpi=150) |
| 173 | + print(" Saved to plot_static_bw.png") |
| 174 | + |
| 175 | + print("2. Plotting sawtooth bandwidth trace from CSV...") |
| 176 | + plt2 = plot_bw_trace_from_csv("sawtooth_bw.csv", "Sawtooth Bandwidth Trace") |
| 177 | + plt2.savefig("plot_sawtooth_bw.png", dpi=150) |
| 178 | + print(" Saved to plot_sawtooth_bw.png") |
| 179 | + |
| 180 | + print("3. Plotting per-packet delay from CSV...") |
| 181 | + plt3 = plot_delay_per_packet_from_csv("per_packet_delay.csv", "Per-Packet Delay") |
| 182 | + plt3.savefig("plot_per_packet_delay.png", dpi=150) |
| 183 | + print(" Saved to plot_per_packet_delay.png") |
| 184 | + |
| 185 | + print("4. Creating multi-panel plot...") |
| 186 | + plt4 = create_multi_plot() |
| 187 | + plt4.savefig("plot_multi.png", dpi=150) |
| 188 | + print(" Saved to plot_multi.png") |
| 189 | + |
| 190 | + print("\nAll plots created successfully!") |
| 191 | + print("You can view the plots by opening the PNG files in your image viewer.") |
0 commit comments