Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNT] - Update reports (add settings) #267

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -642,9 +642,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