Skip to content

Commit

Permalink
drop times from combined plot (& compute internally)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Feb 15, 2024
1 parent c497d1c commit 6ce3df2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 12 additions & 7 deletions neurodsp/plts/combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import matplotlib.pyplot as plt

from neurodsp.spectral import compute_spectrum
from neurodsp.utils.data import create_times
from neurodsp.spectral.utils import trim_spectrum
from neurodsp.plts.spectral import plot_power_spectra
from neurodsp.plts.time_series import plot_time_series
Expand All @@ -12,24 +13,27 @@
###################################################################################################

@savefig
def plot_timeseries_and_spectrum(times, sig, fs, f_range=None, spectrum_kwargs=None,
ts_kwargs=None, psd_kwargs=None, **plt_kwargs):
def plot_timeseries_and_spectrum(sig, fs, ts_range=None, f_range=None, spectrum_kwargs=None,
start_val=None, ts_kwargs=None, psd_kwargs=None, **plt_kwargs):
"""Plot a timeseries together with it's associated power spectrum.
Parameters
----------
times : 1d array, or None
Time definition(s) for the time series to be plotted.
If None, time series will be plotted in terms of samples instead of time.
sigs : 1d array
sig : 1d array
Time series to plot.
fs : float
Sampling rate, in Hz.
ts_range : list of [float, float], optional
The time range to restrict the time series to.
For visualization only - the power spectrum is computed over the entire time series.
f_range : list of [float, float], optional
The frequency range to restrict the power spectrum to.
spectrum_kwargs : dict, optional
Keyword arguments for computing the power spectrum.
See `compute_spectrum` for details.
start_val : float, optional
The starting value for the time definition for the time series.
If not provided, defaults to zero.
ts_kwargs : dict, optional
Keyword arguments for customizing the time series plot.
psd_kwargs : dict, optional
Expand All @@ -53,7 +57,8 @@ def plot_timeseries_and_spectrum(times, sig, fs, f_range=None, spectrum_kwargs=N
ax1 = fig.add_axes([0.0, 0.6, 1.3, 0.5])
ax2 = fig.add_axes([1.5, 0.6, 0.6, 0.5])

plot_time_series(times, sig, ax=ax1, **plt_kwargs,
times = create_times(len(sig) / fs, fs, start_val=start_val)
plot_time_series(times, sig, xlim=ts_range, ax=ax1, **plt_kwargs,
**ts_kwargs if ts_kwargs else {})

freqs, psd = compute_spectrum(sig, fs, **spectrum_kwargs if spectrum_kwargs else {})
Expand Down
8 changes: 2 additions & 6 deletions neurodsp/tests/plts/test_combined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tests for neurodsp.plts.combined."""

from neurodsp.utils import create_times

from neurodsp.tests.settings import TEST_PLOTS_PATH, N_SECONDS, FS
from neurodsp.tests.tutils import plot_test

Expand All @@ -13,14 +11,12 @@
@plot_test
def test_plot_timeseries_and_spectrum(tsig_comb):

times = create_times(N_SECONDS, FS)

plot_timeseries_and_spectrum(times, tsig_comb, FS,
plot_timeseries_and_spectrum(tsig_comb, FS,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_combined_ts_psd.png')

# Test customizations
plot_timeseries_and_spectrum(times, tsig_comb, FS,
plot_timeseries_and_spectrum(tsig_comb, FS,
f_range=(3, 50), color='blue',
spectrum_kwargs={'nperseg' : 500},
ts_kwargs={'xlim' : [0, 5]},
Expand Down

0 comments on commit 6ce3df2

Please sign in to comment.