Skip to content

Commit 0ed7439

Browse files
committed
consolidate plotting to spectra funcs
1 parent f81ee12 commit 0ed7439

File tree

7 files changed

+47
-112
lines changed

7 files changed

+47
-112
lines changed

examples/sims/plot_simulated_power_spectra.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from fooof.sim.gen import gen_power_spectrum, gen_group_power_spectra
1212

1313
# Import plotting functions
14-
from fooof.plts.spectra import plot_spectrum, plot_spectra
14+
from fooof.plts.spectra import plot_spectra
1515

1616
###################################################################################################
1717
# Creating Simulated Power Spectra
@@ -53,7 +53,7 @@
5353
###################################################################################################
5454

5555
# Plot the simulated power spectrum
56-
plot_spectrum(freqs, powers, log_freqs=True, log_powers=False)
56+
plot_spectra(freqs, powers, log_freqs=True, log_powers=False)
5757

5858
###################################################################################################
5959
# Simulating With Different Parameters
@@ -100,7 +100,7 @@
100100
###################################################################################################
101101

102102
# Plot the new simulated power spectrum
103-
plot_spectrum(freqs, powers, log_powers=True)
103+
plot_spectra(freqs, powers, log_powers=True)
104104

105105
###################################################################################################
106106
# Simulating a Group of Power Spectra

fooof/plts/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
"""Plots sub-module for FOOOF."""
22

3-
from .spectra import plot_spectrum, plot_spectra
3+
from .spectra import plot_spectra
4+
from .spectra import plot_spectra as plot_spectrum

fooof/plts/annotate.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from fooof.core.modutils import safe_import, check_dependency
99
from fooof.sim.gen import gen_aperiodic
1010
from fooof.plts.utils import check_ax
11-
from fooof.plts.spectra import plot_spectrum
11+
from fooof.plts.spectra import plot_spectra
1212
from fooof.plts.settings import PLT_FIGSIZES, PLT_COLORS
1313
from fooof.plts.style import check_n_style, style_spectrum_plot
1414
from fooof.analysis.periodic import get_band_peak_fm
@@ -46,14 +46,14 @@ def plot_annotated_peak_search(fm, plot_style=style_spectrum_plot):
4646
# This forces the creation of a new plotting axes per iteration
4747
ax = check_ax(None, PLT_FIGSIZES['spectral'])
4848

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')
49+
plot_spectra(fm.freqs, flatspec, ax=ax, plot_style=None,
50+
label='Flattened Spectrum', color=PLT_COLORS['data'], linewidth=2.5)
51+
plot_spectra(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_spectra(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')
5757

5858
maxi = np.argmax(flatspec)
5959
ax.plot(fm.freqs[maxi], flatspec[maxi], '.',
@@ -65,9 +65,9 @@ def plot_annotated_peak_search(fm, plot_style=style_spectrum_plot):
6565
if ind < fm.n_peaks_:
6666

6767
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)
68+
plot_spectra(fm.freqs, gauss, ax=ax, plot_style=None,
69+
label='Gaussian Fit', color=PLT_COLORS['periodic'],
70+
linestyle=':', linewidth=3.0)
7171

7272
flatspec = flatspec - gauss
7373

fooof/plts/error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55
from fooof.core.modutils import safe_import, check_dependency
6-
from fooof.plts.spectra import plot_spectrum
6+
from fooof.plts.spectra import plot_spectra
77
from fooof.plts.settings import PLT_FIGSIZES
88
from fooof.plts.style import check_n_style, style_spectrum_plot
99
from fooof.plts.utils import check_ax
@@ -41,7 +41,7 @@ def plot_spectral_error(freqs, error, shade=None, log_freqs=False,
4141

4242
plt_freqs = np.log10(freqs) if log_freqs else freqs
4343

44-
plot_spectrum(plt_freqs, error, plot_style=None, ax=ax, linewidth=3, **plot_kwargs)
44+
plot_spectra(plt_freqs, error, plot_style=None, ax=ax, linewidth=3, **plot_kwargs)
4545

4646
if np.any(shade):
4747
ax.fill_between(plt_freqs, error-shade, error+shade, alpha=0.25)

fooof/plts/fm.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from fooof.sim.gen import gen_periodic
1414
from fooof.utils.data import trim_spectrum
1515
from fooof.utils.params import compute_fwhm
16-
from fooof.plts.spectra import plot_spectrum
16+
from fooof.plts.spectra import plot_spectra
1717
from fooof.plts.settings import PLT_FIGSIZES, PLT_COLORS
1818
from fooof.plts.utils import check_ax, check_plot_kwargs
1919
from fooof.plts.style import check_n_style, style_spectrum_plot
@@ -73,24 +73,24 @@ def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, plt_log=False, add_legend=
7373
data_kwargs = check_plot_kwargs(data_kwargs, \
7474
{'color' : PLT_COLORS['data'], 'linewidth' : 2.0,
7575
'label' : 'Original Spectrum' if add_legend else None})
76-
plot_spectrum(fm.freqs, fm.power_spectrum, log_freqs, log_powers,
77-
ax=ax, plot_style=None, **data_kwargs)
76+
plot_spectra(fm.freqs, fm.power_spectrum, log_freqs, log_powers,
77+
ax=ax, plot_style=None, **data_kwargs)
7878

7979
# Add the full model fit, and components (if requested)
8080
if fm.has_model:
8181
model_kwargs = check_plot_kwargs(model_kwargs, \
8282
{'color' : PLT_COLORS['model'], 'linewidth' : 3.0, 'alpha' : 0.5,
8383
'label' : 'Full Model Fit' if add_legend else None})
84-
plot_spectrum(fm.freqs, fm.fooofed_spectrum_, log_freqs, log_powers,
85-
ax=ax, plot_style=None, **model_kwargs)
84+
plot_spectra(fm.freqs, fm.fooofed_spectrum_, log_freqs, log_powers,
85+
ax=ax, plot_style=None, **model_kwargs)
8686

8787
# Plot the aperiodic component of the model fit
8888
if plot_aperiodic:
8989
aperiodic_kwargs = check_plot_kwargs(aperiodic_kwargs, \
9090
{'color' : PLT_COLORS['aperiodic'], 'linewidth' : 3.0, 'alpha' : 0.5,
9191
'linestyle' : 'dashed', 'label' : 'Aperiodic Fit' if add_legend else None})
92-
plot_spectrum(fm.freqs, fm._ap_fit, log_freqs, log_powers,
93-
ax=ax, plot_style=None, **aperiodic_kwargs)
92+
plot_spectra(fm.freqs, fm._ap_fit, log_freqs, log_powers,
93+
ax=ax, plot_style=None, **aperiodic_kwargs)
9494

9595
# Plot the periodic components of the model fit
9696
if plot_peaks:

fooof/plts/spectra.py

Lines changed: 16 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,6 @@
1919
###################################################################################################
2020
###################################################################################################
2121

22-
@check_dependency(plt, 'matplotlib')
23-
def plot_spectrum(freqs, power_spectrum, log_freqs=False, log_powers=False,
24-
ax=None, plot_style=style_spectrum_plot, **plot_kwargs):
25-
"""Plot a power spectrum.
26-
27-
Parameters
28-
----------
29-
freqs : 1d array
30-
Frequency values, to be plotted on the x-axis.
31-
power_spectrum : 1d array
32-
Power values, to be plotted on the y-axis.
33-
log_freqs : bool, optional, default: False
34-
Whether to plot the frequency axis in log spacing.
35-
log_powers : bool, optional, default: False
36-
Whether to plot the power axis in log spacing.
37-
ax : matplotlib.Axes, optional
38-
Figure axis upon which to plot.
39-
plot_style : callable, optional, default: style_spectrum_plot
40-
A function to call to apply styling & aesthetics to the plot.
41-
**plot_kwargs
42-
Keyword arguments to be passed to the plot call.
43-
"""
44-
45-
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
46-
47-
# Set plot data & labels, logging if requested
48-
plt_freqs = np.log10(freqs) if log_freqs else freqs
49-
plt_powers = np.log10(power_spectrum) if log_powers else power_spectrum
50-
51-
# Create the plot
52-
plot_kwargs = check_plot_kwargs(plot_kwargs, {'linewidth' : 2.0})
53-
ax.plot(plt_freqs, plt_powers, **plot_kwargs)
54-
55-
check_n_style(plot_style, ax, log_freqs, log_powers)
56-
5722

5823
@check_dependency(plt, 'matplotlib')
5924
def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, labels=None,
@@ -82,51 +47,28 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, labels
8247

8348
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
8449

85-
# Make inputs iterable if need to be passed multiple times to plot each spectrum
86-
freqs = repeat(freqs) if isinstance(freqs, np.ndarray) and freqs.ndim == 1 else freqs
87-
labels = repeat(labels) if not isinstance(labels, list) else labels
88-
89-
for freq, power_spectrum, label in zip(freqs, power_spectra, labels):
90-
plot_spectrum(freq, power_spectrum, log_freqs, log_powers, label=label,
91-
plot_style=None, ax=ax, **plot_kwargs)
92-
93-
check_n_style(plot_style, ax, log_freqs, log_powers)
94-
50+
# Create the plot
51+
plot_kwargs = check_plot_kwargs(plot_kwargs, {'linewidth' : 2.0})
9552

96-
@check_dependency(plt, 'matplotlib')
97-
def plot_spectrum_shading(freqs, power_spectrum, shades, shade_colors='r', add_center=False,
98-
ax=None, plot_style=style_spectrum_plot, **plot_kwargs):
99-
"""Plot a power spectrum with a shaded frequency region (or regions).
53+
# Make inputs iterable if need to be passed multiple times to plot each spectrum
54+
plt_powers = np.reshape(power_spectra, (1, -1)) if np.ndim(power_spectra) == 1 else \
55+
power_spectra
56+
plt_freqs = repeat(freqs) if isinstance(freqs, np.ndarray) and freqs.ndim == 1 else freqs
10057

101-
Parameters
102-
----------
103-
freqs : 1d array
104-
Frequency values, to be plotted on the x-axis.
105-
power_spectrum : 1d array
106-
Power values, to be plotted on the y-axis.
107-
shades : list of [float, float] or list of list of [float, float]
108-
Shaded region(s) to add to plot, defined as [lower_bound, upper_bound].
109-
shade_colors : str or list of string
110-
Color(s) to plot shades.
111-
add_center : bool, optional, default: False
112-
Whether to add a line at the center point of the shaded regions.
113-
ax : matplotlib.Axes, optional
114-
Figure axes upon which to plot.
115-
plot_style : callable, optional, default: style_spectrum_plot
116-
A function to call to apply styling & aesthetics to the plot.
117-
**plot_kwargs
118-
Keyword arguments to be passed to the plot call.
119-
"""
58+
# Set labels
59+
labels = plot_kwargs.pop('label') if 'label' in plot_kwargs.keys() and labels is None else labels
60+
labels = repeat(labels) if not isinstance(labels, list) else labels
12061

121-
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
62+
# Plot
63+
for freqs, powers, label in zip(plt_freqs, plt_powers, labels):
12264

123-
plot_spectrum(freqs, power_spectrum, plot_style=None, ax=ax, **plot_kwargs)
65+
# Set plot data & labels, logging if requested
66+
freqs = np.log10(freqs) if log_freqs else freqs
67+
powers = np.log10(powers) if log_powers else powers
12468

125-
add_shades(ax, shades, shade_colors, add_center, plot_kwargs.get('log_freqs', False))
69+
ax.plot(freqs, powers, label=label, **plot_kwargs)
12670

127-
check_n_style(plot_style, ax,
128-
plot_kwargs.get('log_freqs', False),
129-
plot_kwargs.get('log_powers', False))
71+
check_n_style(plot_style, ax, log_freqs, log_powers)
13072

13173

13274
@check_dependency(plt, 'matplotlib')

fooof/tests/plts/test_spectra.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@
1010
###################################################################################################
1111

1212
@plot_test
13-
def test_plot_spectrum(tfm, skip_if_no_mpl):
13+
def test_plot_spectra(tfm, tfg, skip_if_no_mpl):
1414

15-
plot_spectrum(tfm.freqs, tfm.power_spectrum)
16-
17-
# Test with logging both axes
18-
plot_spectrum(tfm.freqs, tfm.power_spectrum, True, True)
19-
20-
@plot_test
21-
def test_plot_spectra(tfg, skip_if_no_mpl):
15+
# Test with 1d inputs - 1d freq array and 1d power spectrum
16+
plot_spectra(tfm.freqs, tfm.power_spectrum)
2217

2318
# Test with 1d inputs - 1d freq array and list of 1d power spectra
2419
plot_spectra(tfg.freqs, [tfg.power_spectra[0, :], tfg.power_spectra[1, :]])
@@ -34,12 +29,9 @@ def test_plot_spectra(tfg, skip_if_no_mpl):
3429
plot_spectra(tfg.freqs, [tfg.power_spectra[0, :], tfg.power_spectra[1, :]], labels=['A', 'B'])
3530

3631
@plot_test
37-
def test_plot_spectrum_shading(tfm, skip_if_no_mpl):
32+
def test_plot_spectra_shading(tfm, tfg, skip_if_no_mpl):
3833

39-
plot_spectrum_shading(tfm.freqs, tfm.power_spectrum, shades=[8, 12], add_center=True)
40-
41-
@plot_test
42-
def test_plot_spectra_shading(tfg, skip_if_no_mpl):
34+
plot_spectra_shading(tfm.freqs, tfm.power_spectrum, shades=[8, 12], add_center=True)
4335

4436
plot_spectra_shading(tfg.freqs, [tfg.power_spectra[0, :], tfg.power_spectra[1, :]],
4537
shades=[8, 12], add_center=True)

0 commit comments

Comments
 (0)