Skip to content

Commit 6f185a6

Browse files
committed
add save_report_fg_i
this function saves a detailed report for a single model fit within a FOOOFGroup object
1 parent ba0d1a3 commit 6f185a6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

fooof/core/reports.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,59 @@ def save_report_fg(fg, file_name, file_path=None):
120120
# Save out the report
121121
plt.savefig(fpath(file_path, fname(file_name, SAVE_FORMAT)))
122122
plt.close()
123+
124+
125+
def save_report_fg_i(fg, i_model, file_name, file_path=None, plot_peaks=None,
126+
plot_aperiodic=True, plt_log=True, add_legend=True, data_kwargs=None,
127+
model_kwargs=None, aperiodic_kwargs=None, peak_kwargs=None):
128+
"""Generate and save out a PDF report for a single model fit within a FOOOFGroup object.
129+
130+
Parameters
131+
----------
132+
fg : FOOOFGroup
133+
Object with results from fitting a group of power spectra.
134+
i_model : int
135+
Index of the model for which to generate a report.
136+
file_name : str
137+
Name to give the saved out file.
138+
file_path : str, optional
139+
Path to directory to save to. If None, saves to current directory.
140+
plot_peaks : None or {'shade', 'dot', 'outline', 'line'}, optional
141+
What kind of approach to take to plot peaks. If None, peaks are not specifically plotted.
142+
Can also be a combination of approaches, separated by '-', for example: 'shade-line'.
143+
plot_aperiodic : boolean, optional, default: True
144+
Whether to plot the aperiodic component of the model fit.
145+
plt_log : bool, optional, default: False
146+
Whether or not to plot the frequency axis in log space.
147+
add_legend : boolean, optional, default: False
148+
Whether to add a legend describing the plot components.
149+
data_kwargs, model_kwargs, aperiodic_kwargs, peak_kwargs : None or dict, optional
150+
Keyword arguments to pass into the plot call for each plot element.
151+
"""
152+
153+
# imports
154+
import numpy as np
155+
from fooof import FOOOF
156+
from fooof.sim.gen import gen_aperiodic, gen_periodic
157+
158+
# create fooof object and add settings
159+
fm = FOOOF()
160+
fm.add_settings(fg.get_settings())
161+
162+
# Copy results for model of interest and additional data needed for plotting
163+
fm.add_results(fg[i_model])
164+
fm.power_spectrum = fg.power_spectra[i_model]
165+
fm.freq_range = fg.freq_range
166+
fm.freq_res = fg.freq_res
167+
fm.freqs = fg.freqs
168+
169+
# generate and perioidc/aperiodic fits from parameters
170+
fm._ap_fit = gen_aperiodic(fg.freqs, fg[i_model].aperiodic_params)
171+
fm._peak_fit = gen_periodic(fg.freqs, np.ndarray.flatten(fg[i_model].gaussian_params))
172+
fm.fooofed_spectrum_ = fm._ap_fit + fm._peak_fit
173+
174+
# save report
175+
save_report_fm(fm, file_name, file_path=file_path, plot_peaks=plot_peaks,
176+
plot_aperiodic=plot_aperiodic, plt_log=plt_log, add_legend=add_legend,
177+
data_kwargs=data_kwargs, model_kwargs=model_kwargs,
178+
aperiodic_kwargs=aperiodic_kwargs, peak_kwargs=peak_kwargs)

0 commit comments

Comments
 (0)