77from fooof .core .funcs import gaussian_function
88from fooof .core .modutils import safe_import , check_dependency
99from fooof .sim .gen import gen_aperiodic
10- from fooof .plts .utils import check_ax
10+ from fooof .plts .utils import check_ax , savefig
1111from fooof .plts .spectra import plot_spectrum
1212from fooof .plts .settings import PLT_FIGSIZES , PLT_COLORS
13- from fooof .plts .style import check_n_style , style_spectrum_plot
13+ from fooof .plts .style import style_spectrum_plot
1414from fooof .analysis .periodic import get_band_peak_fm
1515from fooof .utils .params import compute_knee_frequency , compute_fwhm
1616
2020###################################################################################################
2121###################################################################################################
2222
23+ @savefig
2324@check_dependency (plt , 'matplotlib' )
24- def plot_annotated_peak_search (fm , plot_style = style_spectrum_plot ):
25+ def plot_annotated_peak_search (fm ):
2526 """Plot a series of plots illustrating the peak search from a flattened spectrum.
2627
2728 Parameters
2829 ----------
2930 fm : FOOOF
3031 FOOOF object, with model fit, data and settings available.
31- plot_style : callable, optional, default: style_spectrum_plot
32- A function to call to apply styling & aesthetics to the plots.
3332 """
3433
3534 # Recalculate the initial aperiodic fit and flattened spectrum that
@@ -46,14 +45,12 @@ def plot_annotated_peak_search(fm, plot_style=style_spectrum_plot):
4645 # This forces the creation of a new plotting axes per iteration
4746 ax = check_ax (None , PLT_FIGSIZES ['spectral' ])
4847
49- plot_spectrum (fm .freqs , flatspec , ax = ax , plot_style = None ,
50- label = 'Flattened Spectrum' , color = PLT_COLORS ['data' ], linewidth = 2.5 )
51- plot_spectrum (fm .freqs , [fm .peak_threshold * np .std (flatspec )]* len (fm .freqs ),
52- ax = ax , plot_style = None , label = 'Relative Threshold' ,
53- color = 'orange' , linewidth = 2.5 , linestyle = 'dashed' )
54- plot_spectrum (fm .freqs , [fm .min_peak_height ]* len (fm .freqs ),
55- ax = ax , plot_style = None , label = 'Absolute Threshold' ,
56- color = 'red' , linewidth = 2.5 , linestyle = 'dashed' )
48+ plot_spectrum (fm .freqs , flatspec , ax = ax , linewidth = 2.5 ,
49+ label = 'Flattened Spectrum' , color = PLT_COLORS ['data' ])
50+ plot_spectrum (fm .freqs , [fm .peak_threshold * np .std (flatspec )]* len (fm .freqs ), ax = ax ,
51+ label = 'Relative Threshold' , color = 'orange' , linewidth = 2.5 , linestyle = 'dashed' )
52+ plot_spectrum (fm .freqs , [fm .min_peak_height ]* len (fm .freqs ), ax = ax ,
53+ label = 'Absolute Threshold' , color = 'red' , linewidth = 2.5 , linestyle = 'dashed' )
5754
5855 maxi = np .argmax (flatspec )
5956 ax .plot (fm .freqs [maxi ], flatspec [maxi ], '.' ,
@@ -65,18 +62,18 @@ def plot_annotated_peak_search(fm, plot_style=style_spectrum_plot):
6562 if ind < fm .n_peaks_ :
6663
6764 gauss = gaussian_function (fm .freqs , * fm .gaussian_params_ [ind , :])
68- plot_spectrum (fm .freqs , gauss , ax = ax , plot_style = None ,
69- label = 'Gaussian Fit' , color = PLT_COLORS ['periodic' ],
70- linestyle = ':' , linewidth = 3.0 )
65+ plot_spectrum (fm .freqs , gauss , ax = ax , label = 'Gaussian Fit' ,
66+ color = PLT_COLORS ['periodic' ], linestyle = ':' , linewidth = 3.0 )
7167
7268 flatspec = flatspec - gauss
7369
74- check_n_style ( plot_style , ax , False , True )
70+ style_spectrum_plot ( ax , False , True )
7571
7672
73+ @savefig
7774@check_dependency (plt , 'matplotlib' )
78- def plot_annotated_model (fm , plt_log = False , annotate_peaks = True , annotate_aperiodic = True ,
79- ax = None , plot_style = style_spectrum_plot ):
75+ def plot_annotated_model (fm , plt_log = False , annotate_peaks = True ,
76+ annotate_aperiodic = True , ax = None ):
8077 """Plot a an annotated power spectrum and model, from a FOOOF object.
8178
8279 Parameters
@@ -91,8 +88,6 @@ def plot_annotated_model(fm, plt_log=False, annotate_peaks=True, annotate_aperio
9188 Whether to annotate the aperiodic components of the model fit.
9289 ax : matplotlib.Axes, optional
9390 Figure axes upon which to plot.
94- plot_style : callable, optional, default: style_spectrum_plot
95- A function to call to apply styling & aesthetics to the plots.
9691
9792 Raises
9893 ------
@@ -112,7 +107,7 @@ def plot_annotated_model(fm, plt_log=False, annotate_peaks=True, annotate_aperio
112107
113108 # Create the baseline figure
114109 ax = check_ax (ax , PLT_FIGSIZES ['spectral' ])
115- fm .plot (plot_peaks = 'dot-shade-width' , plt_log = plt_log , ax = ax , plot_style = None ,
110+ fm .plot (plot_peaks = 'dot-shade-width' , plt_log = plt_log , ax = ax ,
116111 data_kwargs = {'lw' : lw1 , 'alpha' : 0.6 },
117112 aperiodic_kwargs = {'lw' : lw1 , 'zorder' : 10 },
118113 model_kwargs = {'lw' : lw1 , 'alpha' : 0.5 },
@@ -133,7 +128,7 @@ def plot_annotated_model(fm, plt_log=False, annotate_peaks=True, annotate_aperio
133128 # See: https://github.com/matplotlib/matplotlib/issues/12820. Fixed in 3.2.1.
134129 bug_buff = 0.000001
135130
136- if annotate_peaks :
131+ if annotate_peaks and fm . n_peaks_ :
137132
138133 # Extract largest peak, to annotate, grabbing gaussian params
139134 gauss = get_band_peak_fm (fm , fm .freq_range , attribute = 'gaussian_params' )
@@ -219,7 +214,7 @@ def plot_annotated_model(fm, plt_log=False, annotate_peaks=True, annotate_aperio
219214 color = PLT_COLORS ['aperiodic' ], fontsize = fontsize )
220215
221216 # Apply style to plot & tune grid styling
222- check_n_style ( plot_style , ax , plt_log , True )
217+ style_spectrum_plot ( ax , plt_log , True )
223218 ax .grid (True , alpha = 0.5 )
224219
225220 # Add labels to plot in the legend
0 commit comments