Skip to content

Commit 7b0d2de

Browse files
committed
add option to pass custom freqs & power spectrum to plot_fm
1 parent f41b2b5 commit 7b0d2de

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

fooof/plts/fm.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
@savefig
2626
@style_plot
2727
@check_dependency(plt, 'matplotlib')
28-
def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, plt_log=False, add_legend=True,
29-
save_fig=False, file_name=None, file_path=None, ax=None, data_kwargs=None,
30-
model_kwargs=None, aperiodic_kwargs=None, peak_kwargs=None, **plot_kwargs):
28+
def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, freqs=None, power_spectrum=None,
29+
freq_range=None, plt_log=False, add_legend=True, save_fig=False, file_name=None,
30+
file_path=None, ax=None, data_kwargs=None, model_kwargs=None, aperiodic_kwargs=None,
31+
peak_kwargs=None, **plot_kwargs):
3132
"""Plot the power spectrum and model fit results from a FOOOF object.
3233
3334
Parameters
@@ -39,6 +40,14 @@ def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, plt_log=False, add_legend=
3940
Can also be a combination of approaches, separated by '-', for example: 'shade-line'.
4041
plot_aperiodic : boolean, optional, default: True
4142
Whether to plot the aperiodic component of the model fit.
43+
freqs : 1d array, optional
44+
Frequency values of the power spectrum to plot, in linear space.
45+
If provided, this overrides the values in the model object.
46+
power_spectrum : 1d array, optional
47+
Power values to plot, in linear space.
48+
If provided, this overrides the values in the model object.
49+
freq_range : list of [float, float], optional
50+
Frequency range to plot, defined in linear space.
4251
plt_log : boolean, optional, default: False
4352
Whether to plot the frequency values in log10 spacing.
4453
add_legend : boolean, optional, default: False
@@ -64,16 +73,22 @@ def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, plt_log=False, add_legend=
6473

6574
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
6675

76+
# Check inputs for what to plot
77+
custom_spectrum = (np.any(freqs) and np.any(power_spectrum))
78+
6779
# Log settings - note that power values in FOOOF objects are already logged
6880
log_freqs = plt_log
6981
log_powers = False
7082

7183
# Plot the data, if available
72-
if fm.has_data:
84+
if fm.has_data or custom_spectrum:
7385
data_defaults = {'color' : PLT_COLORS['data'], 'linewidth' : 2.0,
7486
'label' : 'Original Spectrum' if add_legend else None}
7587
data_kwargs = check_plot_kwargs(data_kwargs, data_defaults)
76-
plot_spectra(fm.freqs, fm.power_spectrum, log_freqs, log_powers, ax=ax, **data_kwargs)
88+
plot_spectra(freqs if custom_spectrum else fm.freqs,
89+
power_spectrum if custom_spectrum else fm.power_spectrum,
90+
log_freqs, log_powers if not custom_spectrum else True,
91+
freq_range, ax=ax, **data_kwargs)
7792

7893
# Add the full model fit, and components (if requested)
7994
if fm.has_model:

fooof/tests/plts/test_fm.py

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

3+
import numpy as np
4+
35
from fooof.tests.tutils import plot_test
46
from fooof.tests.settings import TEST_PLOTS_PATH
57

@@ -17,6 +19,20 @@ def test_plot_fm(tfm, skip_if_no_mpl):
1719
plot_fm(tfm, save_fig=True, file_path=TEST_PLOTS_PATH,
1820
file_name='test_plot_fm.png')
1921

22+
@plot_test
23+
def test_plot_fm(tfm, skip_if_no_mpl):
24+
25+
# Extract broader range of data available in the object
26+
custom_freqs = tfm.freqs
27+
custom_power_spectrum = np.power(10, tfm.power_spectrum)
28+
29+
# Make sure model has been fit - set custom frequency range
30+
tfm.fit(custom_freqs, custom_power_spectrum, freq_range=[5, 35])
31+
32+
plot_fm(tfm, freqs=custom_freqs, power_spectrum=custom_power_spectrum,
33+
freq_range=[1, 55], save_fig=True, file_path=TEST_PLOTS_PATH,
34+
file_name='test_plot_fm_custom.png')
35+
2036
@plot_test
2137
def test_plot_fm_add_peaks(tfm, skip_if_no_mpl):
2238

0 commit comments

Comments
 (0)