Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b942280

Browse files
authored
Merge pull request #257 from mwprestonjr/flexible_report
extend reports
2 parents 6a1ba83 + 454fcbd commit b942280

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

fooof/core/reports.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
###################################################################################################
2323

2424
@check_dependency(plt, 'matplotlib')
25-
def save_report_fm(fm, file_name, file_path=None, plt_log=False, add_settings=True):
25+
def save_report_fm(fm, file_name, file_path=None, plt_log=False, add_settings=True, **plot_kwargs):
2626
"""Generate and save out a PDF report for a power spectrum model fit.
2727
2828
Parameters
@@ -37,6 +37,8 @@ def save_report_fm(fm, file_name, file_path=None, plt_log=False, add_settings=Tr
3737
Whether or not to plot the frequency axis in log space.
3838
add_settings : bool, optional, default: True
3939
Whether to add a print out of the model settings to the end of the report.
40+
plot_kwargs : keyword arguments
41+
Keyword arguments to pass into the plot method.
4042
"""
4143

4244
# Define grid settings based on what is to be plotted
@@ -56,7 +58,7 @@ def save_report_fm(fm, file_name, file_path=None, plt_log=False, add_settings=Tr
5658

5759
# Second - data plot
5860
ax1 = plt.subplot(grid[1])
59-
fm.plot(plt_log=plt_log, ax=ax1)
61+
fm.plot(plt_log=plt_log, ax=ax1, **plot_kwargs)
6062

6163
# Third - FOOOF settings
6264
if add_settings:

fooof/objs/fit.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def add_results(self, fooof_result):
378378
self._check_loaded_results(fooof_result._asdict())
379379

380380

381-
def report(self, freqs=None, power_spectrum=None, freq_range=None, plt_log=False):
381+
def report(self, freqs=None, power_spectrum=None, freq_range=None, plt_log=False, **plot_kwargs):
382382
"""Run model fit, and display a report, which includes a plot, and printed results.
383383
384384
Parameters
@@ -392,14 +392,16 @@ def report(self, freqs=None, power_spectrum=None, freq_range=None, plt_log=False
392392
If not provided, fits across the entire given range.
393393
plt_log : bool, optional, default: False
394394
Whether or not to plot the frequency axis in log space.
395+
**plot_kwargs
396+
Keyword arguments to pass into the plot method.
395397
396398
Notes
397399
-----
398400
Data is optional, if data has already been added to the object.
399401
"""
400402

401403
self.fit(freqs, power_spectrum, freq_range)
402-
self.plot(plt_log=plt_log)
404+
self.plot(plt_log=plt_log, **plot_kwargs)
403405
self.print_results(concise=False)
404406

405407

@@ -648,9 +650,10 @@ def plot(self, plot_peaks=None, plot_aperiodic=True, plt_log=False,
648650

649651

650652
@copy_doc_func_to_method(save_report_fm)
651-
def save_report(self, file_name, file_path=None, plt_log=False, add_settings=True):
653+
def save_report(self, file_name, file_path=None, plt_log=False,
654+
add_settings=True, **plot_kwargs):
652655

653-
save_report_fm(self, file_name, file_path, plt_log, add_settings)
656+
save_report_fm(self, file_name, file_path, plt_log, add_settings, **plot_kwargs)
654657

655658

656659
@copy_doc_func_to_method(save_fm)

fooof/objs/group.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,30 @@ def print_results(self, concise=False):
542542
print(gen_results_fg_str(self, concise))
543543

544544

545+
def save_model_report(self, index, file_name, file_path=None, plt_log=False,
546+
add_settings=True, **plot_kwargs):
547+
""""Save out an individual model report for a specified model fit.
548+
549+
Parameters
550+
----------
551+
index : int
552+
Index of the model fit to save out.
553+
file_name : str
554+
Name to give the saved out file.
555+
file_path : str, optional
556+
Path to directory to save to. If None, saves to current directory.
557+
plt_log : bool, optional, default: False
558+
Whether or not to plot the frequency axis in log space.
559+
add_settings : bool, optional, default: True
560+
Whether to add a print out of the model settings to the end of the report.
561+
plot_kwargs : keyword arguments
562+
Keyword arguments to pass into the plot method.
563+
"""
564+
565+
self.get_fooof(ind=index, regenerate=True).save_report(\
566+
file_name, file_path, plt_log, **plot_kwargs)
567+
568+
545569
def to_df(self, peak_org):
546570
"""Convert and extract the model results as a pandas object.
547571

fooof/tests/objs/test_group.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
They serve rather as 'smoke tests', for if anything fails completely.
77
"""
88

9+
import os
10+
911
import numpy as np
1012
from numpy.testing import assert_equal
1113

@@ -17,7 +19,7 @@
1719

1820
pd = safe_import('pandas')
1921

20-
from fooof.tests.settings import TEST_DATA_PATH
22+
from fooof.tests.settings import TEST_DATA_PATH, TEST_REPORTS_PATH
2123
from fooof.tests.tutils import default_group_params, plot_test
2224

2325
from fooof.objs.group import *
@@ -212,6 +214,13 @@ def test_fg_print(tfg):
212214
tfg.print_results()
213215
assert True
214216

217+
def test_save_model_report(tfg):
218+
219+
file_name = 'test_group_model_report'
220+
tfg.save_model_report(0, file_name, TEST_REPORTS_PATH)
221+
222+
assert os.path.exists(os.path.join(TEST_REPORTS_PATH, file_name + '.pdf'))
223+
215224
def test_get_results(tfg):
216225
"""Check get results method."""
217226

0 commit comments

Comments
 (0)