Skip to content

Commit

Permalink
enforce consistent xlims on time & event param plots
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Mar 26, 2024
1 parent 34db090 commit 682c4a2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
7 changes: 7 additions & 0 deletions specparam/objs/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def n_peaks_(self):
if self.has_model else None


@property
def n_time_windows(self):
"""How many time windows are included in the model object."""

return self.spectrogram.shape[1] if self.has_data else 0


def _reset_time_results(self):
"""Set, or reset, time results to be empty."""

Expand Down
8 changes: 5 additions & 3 deletions specparam/plts/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ def plot_event_model(event_model, **plot_kwargs):
figsize=plot_kwargs.pop('figsize', [10, 4 + 4 * n_bands]))
axes = cycle(axes)

xlim = [0, time_model.n_time_windows]

# 01: aperiodic params
alabels = ['offset', 'knee', 'exponent'] if has_knee else ['offset', 'exponent']
for alabel in alabels:
plot_param_over_time_yshade(\
None, event_model.event_time_results[alabel],
label=alabel, drop_xticks=True, add_xlabel=False,
label=alabel, drop_xticks=True, add_xlabel=False, xlim=xlim,
title='Aperiodic Parameters' if alabel == 'offset' else None,
color=PARAM_COLORS[alabel], ax=next(axes))
next(axes).axis('off')
Expand All @@ -69,7 +71,7 @@ def plot_event_model(event_model, **plot_kwargs):
for plabel in ['cf', 'pw', 'bw']:
plot_param_over_time_yshade(\
None, event_model.event_time_results[pe_labels[plabel][band_ind]],
label=plabel.upper(), drop_xticks=True, add_xlabel=False,
label=plabel.upper(), drop_xticks=True, add_xlabel=False, xlim=xlim,
title='Periodic Parameters - ' + band_labels[band_ind] if plabel == 'cf' else None,
color=PARAM_COLORS[plabel], ax=next(axes))
next(axes).axis('off')
Expand All @@ -81,4 +83,4 @@ def plot_event_model(event_model, **plot_kwargs):
drop_xticks=False if glabel == 'r_squared' else True,
add_xlabel=True if glabel == 'r_squared' else False,
title='Goodness of Fit' if glabel == 'error' else None,
color=PARAM_COLORS[glabel], ax=next(axes))
color=PARAM_COLORS[glabel], xlim=xlim, ax=next(axes))
5 changes: 4 additions & 1 deletion specparam/plts/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def plot_yshade(x_vals, y_vals, average='mean', shade='std', scale=1., color=Non

@check_dependency(plt, 'matplotlib')
def plot_param_over_time(times, param, label=None, title=None, add_legend=True, add_xlabel=True,
drop_xticks=False, ax=None, **plot_kwargs):
xlim=None, drop_xticks=False, ax=None, **plot_kwargs):
"""Plot a parameter over time.
Parameters
Expand Down Expand Up @@ -228,6 +228,9 @@ def plot_param_over_time(times, param, label=None, title=None, add_legend=True,
if drop_xticks:
ax.set_xticks([], [])

if xlim:
ax.set_xlim(xlim)

if label and add_legend:
ax.legend(loc='upper left', framealpha=plot_kwargs.pop('legend_framealpha', 0.9))

Expand Down
8 changes: 5 additions & 3 deletions specparam/plts/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def plot_time_model(time_model, **plot_kwargs):
figsize=plot_kwargs.pop('figsize', [10, 4 + 2 * n_bands]))
axes = cycle(axes)

xlim = [0, time_model.n_time_windows]

# 01: aperiodic parameters
ap_params = [time_model.time_results['offset'],
time_model.time_results['exponent']]
Expand All @@ -63,7 +65,7 @@ def plot_time_model(time_model, **plot_kwargs):
ap_labels.insert(1, 'Knee')
ap_colors.insert(1, PARAM_COLORS['knee'])

plot_params_over_time(None, ap_params, labels=ap_labels, add_xlabel=False,
plot_params_over_time(None, ap_params, labels=ap_labels, add_xlabel=False, xlim=xlim,
colors=ap_colors, title='Aperiodic Parameters', ax=next(axes))

# 02: periodic parameters
Expand All @@ -73,14 +75,14 @@ def plot_time_model(time_model, **plot_kwargs):
[time_model.time_results[pe_labels['cf'][band_ind]],
time_model.time_results[pe_labels['pw'][band_ind]],
time_model.time_results[pe_labels['bw'][band_ind]]],
labels=['CF', 'PW', 'BW'], add_xlabel=False,
labels=['CF', 'PW', 'BW'], add_xlabel=False, xlim=xlim,
colors=[PARAM_COLORS['cf'], PARAM_COLORS['pw'], PARAM_COLORS['bw']],
title='Periodic Parameters - ' + band_labels[band_ind], ax=next(axes))

# 03: goodness of fit
plot_params_over_time(None,
[time_model.time_results['error'],
time_model.time_results['r_squared']],
labels=['Error', 'R-squared'],
labels=['Error', 'R-squared'], xlim=xlim,
colors=[PARAM_COLORS['error'], PARAM_COLORS['r_squared']],
title='Goodness of Fit', ax=next(axes))

0 comments on commit 682c4a2

Please sign in to comment.