Skip to content

Commit 8b4c93e

Browse files
committed
move yshade func
1 parent cc23f95 commit 8b4c93e

File tree

4 files changed

+74
-76
lines changed

4 files changed

+74
-76
lines changed

fooof/plts/error.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,56 +52,3 @@ def plot_spectral_error(freqs, error, shade=None, log_freqs=False, ax=None, **pl
5252

5353
style_spectrum_plot(ax, log_freqs, True)
5454
ax.set_ylabel('Absolute Error')
55-
56-
57-
@savefig
58-
@style_plot
59-
@check_dependency(plt, 'matplotlib')
60-
def plot_error_shade(freqs, power_spectra, shade=None, scale=1, log_freqs=False,
61-
log_powers=False, ax=None, **plot_kwargs):
62-
"""Plot standard deviation or error as a shaded region around the mean spectrum.
63-
64-
Parameters
65-
----------
66-
freqs : 1d array
67-
Frequency values, to be plotted on the x-axis.
68-
power_spectra : 1d or 2d array
69-
Power values, to be plotted on the y-axis. ``shade`` must be provided if 1d.
70-
shade : 1d array, optional, default: None
71-
Powers to shade above/below the mean spectrum. None defaults to one standard deviation.
72-
scale : int, optional, default: 1
73-
Factor to multiply the the standard deviation, or ``shade``, by.
74-
log_freqs : bool, optional, default: False
75-
Whether to plot the frequency axis in log spacing.
76-
log_powers : bool, optional, default: False
77-
Whether to plot the power axis in log spacing.
78-
ax : matplotlib.Axes, optional
79-
Figure axes upon which to plot.
80-
plot_style : callable, optional, default: style_spectrum_plot
81-
A function to call to apply styling & aesthetics to the plot.
82-
**plot_kwargs
83-
Keyword arguments to be passed to `plot_spectra` or to the plot call.
84-
"""
85-
86-
if shade is None and power_spectra.ndim != 2:
87-
raise ValueError('Power spectra must be 2d if shade is not given.')
88-
89-
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
90-
91-
# Set plot data & labels, logging if requested
92-
plt_freqs = np.log10(freqs) if log_freqs else freqs
93-
plt_powers = np.log10(power_spectra) if log_powers else power_spectra
94-
95-
# Plot mean
96-
powers_mean = np.mean(plt_powers, axis=0) if plt_powers.ndim == 2 else plt_powers
97-
ax.plot(plt_freqs, powers_mean)
98-
99-
# Shade +/- scale * (standard deviation or shade)
100-
shade = scale * np.std(plt_powers, axis=0) if shade is None else scale * shade
101-
upper_shade = powers_mean + shade
102-
lower_shade = powers_mean - shade
103-
104-
alpha = plot_kwargs.pop('alpha', 0.25)
105-
ax.fill_between(plt_freqs, lower_shade, upper_shade, alpha=alpha, **plot_kwargs)
106-
107-
style_spectrum_plot(ax, log_freqs, log_powers)

fooof/plts/spectra.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,56 @@ def plot_spectra_shading(freqs, power_spectra, shades, shade_colors='r',
172172

173173
style_spectrum_plot(ax, plot_kwargs.get('log_freqs', False),
174174
plot_kwargs.get('log_powers', False))
175+
176+
177+
@savefig
178+
@style_plot
179+
@check_dependency(plt, 'matplotlib')
180+
def plot_spectra_yshade(freqs, power_spectra, shade=None, scale=1, log_freqs=False,
181+
log_powers=False, ax=None, **plot_kwargs):
182+
"""Plot standard deviation or error as a shaded region around the mean spectrum.
183+
184+
Parameters
185+
----------
186+
freqs : 1d array
187+
Frequency values, to be plotted on the x-axis.
188+
power_spectra : 1d or 2d array
189+
Power values, to be plotted on the y-axis. ``shade`` must be provided if 1d.
190+
shade : 1d array, optional, default: None
191+
Powers to shade above/below the mean spectrum. None defaults to one standard deviation.
192+
scale : int, optional, default: 1
193+
Factor to multiply the the standard deviation, or ``shade``, by.
194+
log_freqs : bool, optional, default: False
195+
Whether to plot the frequency axis in log spacing.
196+
log_powers : bool, optional, default: False
197+
Whether to plot the power axis in log spacing.
198+
ax : matplotlib.Axes, optional
199+
Figure axes upon which to plot.
200+
plot_style : callable, optional, default: style_spectrum_plot
201+
A function to call to apply styling & aesthetics to the plot.
202+
**plot_kwargs
203+
Keyword arguments to be passed to `plot_spectra` or to the plot call.
204+
"""
205+
206+
if shade is None and power_spectra.ndim != 2:
207+
raise ValueError('Power spectra must be 2d if shade is not given.')
208+
209+
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
210+
211+
# Set plot data & labels, logging if requested
212+
plt_freqs = np.log10(freqs) if log_freqs else freqs
213+
plt_powers = np.log10(power_spectra) if log_powers else power_spectra
214+
215+
# Plot mean
216+
powers_mean = np.mean(plt_powers, axis=0) if plt_powers.ndim == 2 else plt_powers
217+
ax.plot(plt_freqs, powers_mean)
218+
219+
# Shade +/- scale * (standard deviation or shade)
220+
shade = scale * np.std(plt_powers, axis=0) if shade is None else scale * shade
221+
upper_shade = powers_mean + shade
222+
lower_shade = powers_mean - shade
223+
224+
alpha = plot_kwargs.pop('alpha', 0.25)
225+
ax.fill_between(plt_freqs, lower_shade, upper_shade, alpha=alpha, **plot_kwargs)
226+
227+
style_spectrum_plot(ax, log_freqs, log_powers)

fooof/tests/plts/test_error.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Tests for fooof.plts.error."""
22

3-
from pytest import raises, mark, param
4-
53
import numpy as np
64

75
from fooof.tests.tutils import plot_test
@@ -20,24 +18,3 @@ def test_plot_spectral_error(skip_if_no_mpl):
2018

2119
plot_spectral_error(fs, errs, save_fig=True, file_path=TEST_PLOTS_PATH,
2220
file_name='test_plot_spectral_error.png')
23-
24-
25-
26-
@plot_test
27-
def test_plot_error_shade(skip_if_no_mpl, tfg):
28-
29-
freqs = tfg.freqs
30-
powers = tfg.power_spectra
31-
32-
# Invalid 1d array, without shade
33-
with raises(ValueError):
34-
plot_error_shade(freqs, powers[0])
35-
36-
# Valid 1d array with shade
37-
plot_error_shade(freqs, np.mean(powers, axis=0), shade=np.std(powers, axis=0),
38-
save_fig=True, file_path=TEST_PLOTS_PATH,
39-
file_name='test_plot_spectral_error_shade1.png')
40-
41-
# 2d array
42-
plot_error_shade(freqs, powers, save_fig=True, file_path=TEST_PLOTS_PATH,
43-
file_name='test_plot_spectral_error_shade2.png')

fooof/tests/plts/test_spectra.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for fooof.plts.spectra."""
22

3+
from pytest import raises
4+
35
import numpy as np
46

57
from fooof.tests.tutils import plot_test
@@ -59,3 +61,22 @@ def test_plot_spectra_shading(tfg, skip_if_no_mpl):
5961
shades=[8, 12], add_center=True, log_freqs=True, log_powers=True,
6062
labels=['A', 'B'], save_fig=True, file_path=TEST_PLOTS_PATH,
6163
file_name='test_plot_spectra_shading_kwargs.png')
64+
65+
@plot_test
66+
def test_plot_spectra_yshade(skip_if_no_mpl, tfg):
67+
68+
freqs = tfg.freqs
69+
powers = tfg.power_spectra
70+
71+
# Invalid 1d array, without shade
72+
with raises(ValueError):
73+
plot_spectra_yshade(freqs, powers[0])
74+
75+
# Valid 1d array with shade
76+
plot_spectra_yshade(freqs, np.mean(powers, axis=0), shade=np.std(powers, axis=0),
77+
save_fig=True, file_path=TEST_PLOTS_PATH,
78+
file_name='test_plot_spectra_yshade1.png')
79+
80+
# 2d array
81+
plot_spectra_yshade(freqs, powers, save_fig=True, file_path=TEST_PLOTS_PATH,
82+
file_name='test_plot_spectra_yshade2.png')

0 commit comments

Comments
 (0)