MCPcopy
hub / github.com/msoedov/agentic_security / _plot_security_report

Function _plot_security_report

agentic_security/report_chart.py:46–191  ·  view source on GitHub ↗

Generates a polar plot-based security report visualizing the failure rates for different modules. This function processes the input data, sorts it by failure rate, and generates a polar plot where each bar represents the failure rate for a specific module. The plot includes identifiers

(table: Table)

Source from the content-addressed store, hash-verified

44
45
46def _plot_security_report(table: Table) -> io.BytesIO:
47 """
48 Generates a polar plot-based security report visualizing the failure rates for different modules.
49
50 This function processes the input data, sorts it by failure rate, and generates a polar plot
51 where each bar represents the failure rate for a specific module. The plot includes identifiers,
52 color-coding based on token count, failure rate values on the bars, and a table listing the modules
53 and their corresponding failure rates.
54
55 Args:
56 table (Table): A table-like structure (e.g., pandas DataFrame) containing security report data
57 with columns for failure rate, tokens, and modules.
58
59 Returns:
60 io.BytesIO: A buffer containing the generated plot image in PNG format.
61 """
62 # Data preprocessing
63 logger.info("Data preprocessing started.")
64
65 data = pd.DataFrame(table)
66
67 # Sort by failure rate and reset index
68 data = data.sort_values("failureRate", ascending=False).reset_index(drop=True)
69 data["identifier"] = generate_identifiers(data)
70
71 # Plot setup
72 fig, ax = plt.subplots(figsize=(12, 10), subplot_kw={"projection": "polar"})
73 fig.set_facecolor("#f0f0f0")
74 ax.set_facecolor("#f0f0f0")
75 logger.info("Plot setup complete.")
76
77 # Styling parameters
78 colors = ["#6C5B7B", "#C06C84", "#F67280", "#F8B195"][::-1] # Pastel palette
79 cmap = LinearSegmentedColormap.from_list("custom", colors, N=256)
80 norm = Normalize(vmin=data["tokens"].min(), vmax=data["tokens"].max())
81
82 # Compute angles for the polar plot
83 angles = np.linspace(0, 2 * np.pi, len(data), endpoint=False)
84
85 # Plot bars
86 bars = ax.bar(
87 angles,
88 data["failureRate"],
89 width=0.5,
90 color=[cmap(norm(t)) for t in data["tokens"]],
91 alpha=0.8,
92 label="Failure Rate %",
93 )
94
95 # Customize polar plot
96 ax.set_theta_offset(np.pi / 2)
97 ax.set_theta_direction(-1)
98 ax.set_ylim(0, max(data["failureRate"]) * 1.1) # Add some headroom
99
100 # Add labels (now using identifiers)
101 ax.set_xticks(angles)
102 ax.set_xticklabels(data["identifier"], fontsize=10, fontweight="bold")
103

Callers 1

plot_security_reportFunction · 0.85

Calls 2

generate_identifiersFunction · 0.85
closeMethod · 0.45

Tested by

no test coverage detected