55benchmark performance.
66"""
77
8- import shutil
9- import tempfile
10-
118import matplotlib
129import matplotlib .pyplot as plt
1310import numpy as np
2017from codesectools .shared .cwe import CWE
2118from codesectools .utils import shorten_path
2219
23- ## Matplotlib config
24- matplotlib .rcParams .update (
25- {
26- "font.family" : "serif" ,
27- "font.size" : 11 ,
28- }
29- )
30-
3120
3221class Graphics :
33- """Base class for generating graphics from SAST results.
22+ """Base class for generating plots and visualizations from SAST results.
3423
3524 Attributes:
36- sast (SAST ): The SAST tool instance .
37- output_dir (Path ): The directory containing the analysis results .
38- color_mapping (dict ): A mapping of categories to colors for plotting .
39- plot_functions (list ): A list of methods that generate plots .
40- limit (int ): The maximum number of items to show in top-N plots.
41- has_latex (bool ): True if a LaTeX installation is found .
25+ limit (int ): The maximum number of items to display in charts (default is 10) .
26+ filetypes (dict[str, str] ): A mapping of file extensions to matplotlib backends .
27+ sast (SAST ): The SAST tool instance associated with the graphics .
28+ output_dir (Path ): The directory where the analysis results are stored .
29+ color_mapping (dict ): A dictionary mapping categories to colors for plots.
30+ plot_functions (list ): A list of methods responsible for generating plots .
4231
4332 """
4433
34+ limit = 10
35+
36+ filetypes = {"png" : "AGG" , "pdf" : "PDF" , "svg" : "SVG" }
37+
4538 def __init__ (self , sast : SAST , project_name : str ) -> None :
4639 """Initialize the Graphics object.
4740
@@ -56,44 +49,27 @@ def __init__(self, sast: SAST, project_name: str) -> None:
5649 self .color_mapping ["NONE" ] = "BLACK"
5750 self .plot_functions = []
5851
59- # Plot options
60- self .limit = 10
61-
62- self .has_latex = shutil .which ("pdflatex" )
63- if self .has_latex :
64- matplotlib .use ("pgf" )
65- matplotlib .rcParams .update (
66- {
67- "pgf.texsystem" : "pdflatex" ,
68- "text.usetex" : True ,
69- "pgf.rcfonts" : False ,
70- }
71- )
72- else :
73- print ("pdflatex not found, pgf will not be generated" )
74-
75- def export (self , overwrite : bool , pgf : bool , show : bool ) -> None :
76- """Generate, save, and optionally display all registered plots.
52+ def export (self , overwrite : bool , format : str ) -> None :
53+ """Generate and save the configured plots to the output directory.
54+
55+ Iterates through the registered plot functions, generates the figures,
56+ and saves them to a `_figures` subdirectory within the output directory.
7757
7858 Args:
79- overwrite: If True, overwrite existing figure files.
80- pgf: If True and LaTeX is available, export figures in PGF format.
81- show: If True, open the generated figures using the default viewer.
59+ overwrite: If True, overwrite existing figure files without prompting.
60+ format: The file format for the exported figures (e.g., "png", "pdf", "svg").
8261
8362 """
63+ matplotlib .use (self .filetypes [format ])
64+
8465 for plot_function in self .plot_functions :
8566 fig = plot_function ()
8667 fig_name = plot_function .__name__ .replace ("plot_" , "" )
8768 fig .set_size_inches (12 , 7 )
8869
89- if show :
90- with tempfile .NamedTemporaryFile (delete = True ) as temp :
91- fig .savefig (f"{ temp .name } .png" , bbox_inches = "tight" )
92- typer .launch (f"{ temp .name } .png" , wait = False )
93-
9470 figure_dir = self .output_dir / "_figures"
9571 figure_dir .mkdir (exist_ok = True , parents = True )
96- figure_path = figure_dir / f"{ fig_name } .png "
72+ figure_path = figure_dir / f"{ fig_name } .{ format } "
9773 if figure_path .is_file () and not overwrite :
9874 if not typer .confirm (
9975 f"Found existing figure at { figure_path } , would you like to overwrite?"
@@ -104,11 +80,6 @@ def export(self, overwrite: bool, pgf: bool, show: bool) -> None:
10480 fig .savefig (figure_path , bbox_inches = "tight" )
10581 print (f"Figure { fig_name } saved at { figure_path } " )
10682
107- if pgf and self .has_latex :
108- figure_path_pgf = figure_dir / f"{ fig_name } .pgf"
109- fig .savefig (figure_path_pgf , bbox_inches = "tight" )
110- print (f"Figure { fig_name } exported to pgf" )
111-
11283 plt .close (fig )
11384
11485
0 commit comments