Skip to content

Commit

Permalink
Merge pull request #267 from fooof-tools/grep
Browse files Browse the repository at this point in the history
[MNT] - Update reports (add settings)
  • Loading branch information
TomDonoghue authored Jun 29, 2023
2 parents 3649bbe + 2642a8a commit 208fc09
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
48 changes: 34 additions & 14 deletions fooof/core/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
###################################################################################################

@check_dependency(plt, 'matplotlib')
def save_report_fm(fm, file_name, file_path=None, plt_log=False):
def save_report_fm(fm, file_name, file_path=None, plt_log=False, add_settings=True):
"""Generate and save out a PDF report for a power spectrum model fit.
Parameters
Expand All @@ -35,39 +35,44 @@ def save_report_fm(fm, file_name, file_path=None, plt_log=False):
Path to directory to save to. If None, saves to current directory.
plt_log : bool, optional, default: False
Whether or not to plot the frequency axis in log space.
add_settings : bool, optional, default: True
Whether to add a print out of the model settings to the end of the report.
"""

# Define grid settings based on what is to be plotted
n_rows = 3 if add_settings else 2
height_ratios = [0.5, 1.0, 0.25] if add_settings else [0.45, 1.0]

# Set up outline figure, using gridspec
_ = plt.figure(figsize=REPORT_FIGSIZE)
grid = gridspec.GridSpec(3, 1, height_ratios=[0.45, 1.0, 0.25])
grid = gridspec.GridSpec(n_rows, 1, hspace=0.25, height_ratios=height_ratios)

# First - text results
ax0 = plt.subplot(grid[0])
results_str = gen_results_fm_str(fm)
ax0.text(0.5, 0.7, results_str, REPORT_FONT, ha='center', va='center')
ax0.set_frame_on(False)
ax0.set_xticks([])
ax0.set_yticks([])
ax0.set(xticks=[], yticks=[])

# Second - data plot
ax1 = plt.subplot(grid[1])
fm.plot(plt_log=plt_log, ax=ax1)

# Third - FOOOF settings
ax2 = plt.subplot(grid[2])
settings_str = gen_settings_str(fm, False)
ax2.text(0.5, 0.1, settings_str, REPORT_FONT, ha='center', va='center')
ax2.set_frame_on(False)
ax2.set_xticks([])
ax2.set_yticks([])
if add_settings:
ax2 = plt.subplot(grid[2])
settings_str = gen_settings_str(fm, False)
ax2.text(0.5, 0.1, settings_str, REPORT_FONT, ha='center', va='center')
ax2.set_frame_on(False)
ax2.set(xticks=[], yticks=[])

# Save out the report
plt.savefig(fpath(file_path, fname(file_name, SAVE_FORMAT)))
plt.close()


@check_dependency(plt, 'matplotlib')
def save_report_fg(fg, file_name, file_path=None):
def save_report_fg(fg, file_name, file_path=None, add_settings=True):
"""Generate and save out a PDF report for a group of power spectrum models.
Parameters
Expand All @@ -78,19 +83,26 @@ def save_report_fg(fg, file_name, file_path=None):
Name to give the saved out file.
file_path : str, optional
Path to directory to save to. If None, saves to current directory.
add_settings : bool, optional, default: True
Whether to add a print out of the model settings to the end of the report.
"""

# Define grid settings based on what is to be plotted
n_rows = 4 if add_settings else 3
height_ratios = [1.0, 1.0, 1.0, 0.5] if add_settings else [0.8, 1.0, 1.0]

# Initialize figure
_ = plt.figure(figsize=REPORT_FIGSIZE)
grid = gridspec.GridSpec(3, 2, wspace=0.4, hspace=0.25, height_ratios=[0.8, 1.0, 1.0])
grid = gridspec.GridSpec(n_rows, 2, wspace=0.4, hspace=0.25, height_ratios=height_ratios)

# First / top: text results
ax0 = plt.subplot(grid[0, :])
results_str = gen_results_fg_str(fg)
ax0.text(0.5, 0.7, results_str, REPORT_FONT, ha='center', va='center')
ax0.set_frame_on(False)
ax0.set_xticks([])
ax0.set_yticks([])
ax0.set(xticks=[], yticks=[])

# Second - data plots

# Aperiodic parameters plot
ax1 = plt.subplot(grid[1, 0])
Expand All @@ -104,6 +116,14 @@ def save_report_fg(fg, file_name, file_path=None):
ax3 = plt.subplot(grid[2, :])
plot_fg_peak_cens(fg, ax3)

# Third - Model settings
if add_settings:
ax4 = plt.subplot(grid[3, :])
settings_str = gen_settings_str(fg, False)
ax4.text(0.5, 0.1, settings_str, REPORT_FONT, ha='center', va='center')
ax4.set_frame_on(False)
ax4.set(xticks=[], yticks=[])

# Save out the report
plt.savefig(fpath(file_path, fname(file_name, SAVE_FORMAT)))
plt.close()
4 changes: 2 additions & 2 deletions fooof/objs/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,9 @@ def plot(self, plot_peaks=None, plot_aperiodic=True, plt_log=False,


@copy_doc_func_to_method(save_report_fm)
def save_report(self, file_name, file_path=None, plt_log=False):
def save_report(self, file_name, file_path=None, plt_log=False, add_settings=True):

save_report_fm(self, file_name, file_path, plt_log)
save_report_fm(self, file_name, file_path, plt_log, add_settings)


@copy_doc_func_to_method(save_fm)
Expand Down
4 changes: 2 additions & 2 deletions fooof/objs/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ def plot(self, save_fig=False, file_name=None, file_path=None, **plot_kwargs):


@copy_doc_func_to_method(save_report_fg)
def save_report(self, file_name, file_path=None):
def save_report(self, file_name, file_path=None, add_settings=True):

save_report_fg(self, file_name, file_path)
save_report_fg(self, file_name, file_path, add_settings)


@copy_doc_func_to_method(save_fg)
Expand Down
2 changes: 1 addition & 1 deletion fooof/plts/fg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def plot_fg(fg, save_fig=False, file_name=None, file_path=None, **plot_kwargs):
if not fg.has_model:
raise NoModelError("No model fit results are available, can not proceed.")

fig = plt.figure(figsize=PLT_FIGSIZES['group'])
fig = plt.figure(figsize=plot_kwargs.pop('figsize', PLT_FIGSIZES['group']))
gs = gridspec.GridSpec(2, 2, wspace=0.4, hspace=0.25, height_ratios=[1, 1.2])

# Apply scatter kwargs to all subplots
Expand Down
2 changes: 1 addition & 1 deletion fooof/plts/fm.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, plt_log=False, add_legend=
the y-axis (power) is plotted in log spacing by default.
"""

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

# Log settings - note that power values in FOOOF objects are already logged
log_freqs = plt_log
Expand Down

0 comments on commit 208fc09

Please sign in to comment.