Skip to content

Commit

Permalink
Merge pull request #255 from neurodsp-tools/pltfix
Browse files Browse the repository at this point in the history
[MNT] - Update tests & small plot bug fixes
  • Loading branch information
TomDonoghue authored Mar 15, 2021
2 parents 01645e2 + 4c657da commit 1202ce2
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 156 deletions.
6 changes: 3 additions & 3 deletions neurodsp/plts/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def plot_instantaneous_measure(times, sigs, measure='phase', ax=None, **kwargs):

if measure == 'phase':
plot_time_series(times, sigs, ax=ax, ylabel='Phase (rad)', **kwargs)
plt.yticks([-np.pi, 0, np.pi], ['-$\pi$', 0, '$\pi$'])
ax = ax if ax else plt.gca()
ax.set_yticks([-np.pi, 0, np.pi])
ax.set_yticklabels([r'-$\pi$', 0, r'$\pi$'])
elif measure == 'amplitude':
plot_time_series(times, sigs, ax=ax, ylabel='Amplitude', **kwargs)
elif measure == 'frequency':
Expand Down Expand Up @@ -145,7 +147,5 @@ def plot_bursts(times, sig, bursting, ax=None, **kwargs):
>>> plot_bursts(times, sig, is_burst, labels=['Raw Data', 'Detected Bursts'])
"""

ax = check_ax(ax, (15, 3))

bursts = ma.array(sig, mask=np.invert(bursting))
plot_time_series(times, [sig, bursts], ax=ax, **kwargs)
2 changes: 1 addition & 1 deletion neurodsp/sim/aperiodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def sim_synaptic_current(n_seconds, fs, n_neurons=1000, firing_rate=2.,
sig = sim_poisson_pop((n_seconds + t_ker), fs, n_neurons, firing_rate,
mean=None, variance=None)
ker = sim_synaptic_kernel(t_ker, fs, tau_r, tau_d)
sig = np.convolve(sig, ker, 'valid')[:-1]
sig = np.convolve(sig, ker, 'valid')[:compute_nsamples(n_seconds, fs)]

return sig

Expand Down
28 changes: 18 additions & 10 deletions neurodsp/tests/aperiodic/test_dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

from neurodsp.sim import sim_powerlaw

from neurodsp.tests.settings import FS, FS_HIGH
from neurodsp.tests.settings import N_SECONDS

from neurodsp.aperiodic.dfa import (compute_fluctuations, compute_rescaled_range,
compute_detrended_fluctuation)
from neurodsp.aperiodic.dfa import *

###################################################################################################
###################################################################################################
Expand All @@ -27,22 +26,31 @@ def test_compute_fluctuations(tsig):
with raises(ValueError):
t_scales, flucs, exp = compute_fluctuations(tsig, 500, method='nope.')

def test_compute_fluctuations_dfa(tsig_white, tsig_brown):
"""Tests fluctuation analysis for the DFA method."""
def test_compute_fluctuations_dfa():

# Tests for DFA method
# Note: these tests need a high sampling rate, so we use local simulations

# Test white noise: expected DFA of 0.5
t_scales, flucs, exp = compute_fluctuations(tsig_white, FS_HIGH, method='dfa')
fs = 1000
white = sim_powerlaw(N_SECONDS, fs, exponent=0)
t_scales, flucs, exp = compute_fluctuations(white, fs, method='dfa')
assert np.isclose(exp, 0.5, atol=0.1)

# Test brown noise: expected DFA of 1.5
t_scales, flucs, exp = compute_fluctuations(tsig_brown, FS_HIGH, method='dfa')
brown = sim_powerlaw(N_SECONDS, fs, exponent=-2)
t_scales, flucs, exp = compute_fluctuations(brown, fs, method='dfa')
assert np.isclose(exp, 1.5, atol=0.1)

def test_compute_fluctuations_rs(tsig_white):
"""Tests fluctuation analysis for the RS method."""
def test_compute_fluctuations_rs():

# Tests for RS method
# Note: these tests need a high sampling rate, so we use local simulations

# Test white noise: expected RS of 0.5
t_scales, flucs, exp = compute_fluctuations(tsig_white, FS_HIGH, method='rs')
fs = 1000
white = sim_powerlaw(N_SECONDS, fs, exponent=0)
t_scales, flucs, exp = compute_fluctuations(white, fs, method='rs')
assert np.isclose(exp, 0.5, atol=0.1)

def test_compute_rescaled_range(tsig):
Expand Down
2 changes: 1 addition & 1 deletion neurodsp/tests/aperiodic/test_irasa.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from neurodsp.sim import sim_combined
from neurodsp.spectral import compute_spectrum, trim_spectrum

from neurodsp.tests.settings import FS, N_SECONDS_LONG, EXP1
from neurodsp.tests.settings import FS, EXP1

from neurodsp.aperiodic.irasa import *

Expand Down
24 changes: 8 additions & 16 deletions neurodsp/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

from neurodsp.sim import sim_oscillation, sim_powerlaw, sim_combined
from neurodsp.utils.sim import set_random_seed
from neurodsp.tests.settings import (N_SECONDS, N_SECONDS_LONG,
FS, FS_HIGH, FREQ_SINE, FREQ1, EXP1,
from neurodsp.tests.settings import (N_SECONDS, FS, FREQ_SINE, FREQ1, EXP1,
BASE_TEST_FILE_PATH, TEST_PLOTS_PATH)

###################################################################################################
Expand All @@ -32,29 +31,22 @@ def tsig2d():
@pytest.fixture(scope='session')
def tsig_sine():

yield sim_oscillation(N_SECONDS, FS, freq=FREQ_SINE, variance=None, mean=None)

@pytest.fixture(scope='session')
def tsig_sine_long():

yield sim_oscillation(N_SECONDS_LONG, FS, freq=FREQ_SINE, variance=None, mean=None)
yield sim_oscillation(N_SECONDS, FS, freq=FREQ_SINE, variance=None, mean=None)

@pytest.fixture(scope='session')
def tsig_comb():

components = {'sim_powerlaw': {'exponent' : EXP1},
'sim_oscillation': {'freq' : FREQ1}}
yield sim_combined(n_seconds=N_SECONDS_LONG, fs=FS, components=components)
yield sim_combined(n_seconds=N_SECONDS, fs=FS, components=components)

@pytest.fixture(scope='session')
def tsig_white():

yield sim_powerlaw(N_SECONDS_LONG, FS_HIGH, exponent=0)
def tsig_burst():

@pytest.fixture(scope='session')
def tsig_brown():

yield sim_powerlaw(N_SECONDS_LONG, FS_HIGH, exponent=-2)
components = {'sim_powerlaw': {'exponent' : EXP1},
'sim_bursty_oscillation': {'freq' : FREQ1}}
yield sim_combined(n_seconds=N_SECONDS, fs=FS,
components=components, component_variances=[0.5, 1])

@pytest.fixture(scope='session', autouse=True)
def check_dir():
Expand Down
8 changes: 4 additions & 4 deletions neurodsp/tests/filt/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import raises, warns

from neurodsp.tests.settings import FS
from neurodsp.tests.settings import FS, F_RANGE

from neurodsp.filt.filter import *
from neurodsp.filt.filter import _iir_checks
Expand All @@ -12,14 +12,14 @@

def test_filter_signal(tsig):

out = filter_signal(tsig, FS, 'bandpass', (8, 12), filter_type='fir')
out = filter_signal(tsig, FS, 'bandpass', F_RANGE, filter_type='fir')
assert out.shape == tsig.shape

out = filter_signal(tsig, FS, 'bandpass', (8, 12), filter_type='iir', butterworth_order=3)
out = filter_signal(tsig, FS, 'bandpass', F_RANGE, filter_type='iir', butterworth_order=3)
assert out.shape == tsig.shape

with raises(ValueError):
out = filter_signal(tsig, FS, 'bandpass', (8, 12), filter_type='bad')
out = filter_signal(tsig, FS, 'bandpass', F_RANGE, filter_type='bad')

def test_iir_checks():

Expand Down
15 changes: 9 additions & 6 deletions neurodsp/tests/plts/test_filt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@ def test_plot_filter_properties():
coefs = design_fir_filter(FS, 'bandpass', (8, 12), 3)
f_db, db = compute_frequency_response(coefs, a_vals=1, fs=FS)

plot_filter_properties(f_db, db, FS, coefs, save_fig=True,
file_name='test_plot_filter_properties.png', file_path=TEST_PLOTS_PATH)
plot_filter_properties(f_db, db, FS, coefs,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_filter_properties.png')

@plot_test
def test_plot_frequency_response():

coefs = design_fir_filter(FS, 'bandpass', (8, 12), 3)
f_db, db = compute_frequency_response(coefs, a_vals=1, fs=FS)

plot_frequency_response(f_db, db, save_fig=True, file_name='test_plot_frequency_response.png',
file_path=TEST_PLOTS_PATH)
plot_frequency_response(f_db, db,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_frequency_response.png')

@plot_test
def test_plot_impulse_response():

coefs = design_fir_filter(FS, 'bandpass', (8, 12), 3)

plot_impulse_response(FS, coefs, save_fig=True, file_name='test_plot_impulse_response.png',
file_path=TEST_PLOTS_PATH)
plot_impulse_response(FS, coefs,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_impulse_response.png')
12 changes: 8 additions & 4 deletions neurodsp/tests/plts/test_rhythm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
def test_plot_swm_pattern():

data = np.arange(0, 2, 0.1)
plot_swm_pattern(data, save_fig=True, file_name='test_plot_swm_pattern.png',
file_path=TEST_PLOTS_PATH)

plot_swm_pattern(data,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_swm_pattern.png')

@plot_test
def test_plot_lagged_coherence():

data = np.arange(0, 2, 0.1)
plot_lagged_coherence(data, data, save_fig=True, file_name='test_plot_lagged_coherence.png',
file_path=TEST_PLOTS_PATH)

plot_lagged_coherence(data, data,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_lagged_coherence.png')
61 changes: 39 additions & 22 deletions neurodsp/tests/plts/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import numpy as np

from neurodsp.spectral.variance import compute_spectral_hist
from neurodsp.spectral.power import compute_spectrum
from neurodsp.spectral.variance import compute_spectral_hist, compute_scv, compute_scv_rs

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

from neurodsp.plts.spectral import *
Expand All @@ -14,40 +14,57 @@
###################################################################################################

@plot_test
def test_plot_power_spectra():
def test_plot_power_spectra(tsig_comb, tsig_burst):

freqs, powers = np.array([1, 2, 3, 4]), np.array([10, 20, 30, 40])
plot_power_spectra(freqs, powers)
plot_power_spectra([freqs, freqs], [powers, powers], labels=['a', 'b'], colors=['k', 'r'],
freqs1, powers1 = compute_spectrum(tsig_comb, FS)
freqs2, powers2 = compute_spectrum(tsig_burst, FS)

plot_power_spectra(freqs1, powers1,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_power_spectra-1.png')

plot_power_spectra([freqs1, freqs2], [powers1, powers2],
labels=['first', 'second'], colors=['k', 'r'],
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_power_spectra.png')
file_name='test_plot_power_spectra-2.png')

@plot_test
def test_plot_scv():
def test_plot_scv(tsig_comb):

freqs, scv = compute_scv(tsig_comb, FS)

plot_scv(freqs, scv,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_scv.png')

freqs, scv = np.array([1, 2, 3, 4]), np.array([10, 20, 30, 40])
plot_scv(freqs, scv, save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_plot_scv.png')
def test_other_scv_plots(tsig_comb):

freqs, t_inds, scv_rs = compute_scv_rs(tsig_comb, FS, nperseg=FS/2, method='rolling')

_plot_scv_rs_lines(freqs, scv_rs)
_plot_scv_rs_matrix(freqs, t_inds, scv_rs)

@plot_test
def test_plot_scv_rs_lines():
def _plot_scv_rs_lines(freqs, scv_rs):

freqs, scv_rs = np.array([1, 2, 3]), np.array([[2, 3, 4], [2, 3, 4], [3, 4, 5]])
plot_scv_rs_lines(freqs, scv_rs, save_fig=True, file_path=TEST_PLOTS_PATH,
plot_scv_rs_lines(freqs, scv_rs,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_scv_rs_lines.png')

@plot_test
def test_plot_scv_rs_matrix():
def _plot_scv_rs_matrix(freqs, t_inds, scv_rs):

freqs, times = np.array([1, 2, 3]), np.array([1, 2, 3])
scv_rs = np.array([[2, 3, 4], [2, 3, 4], [3, 4, 5]])
plot_scv_rs_matrix(freqs, times, scv_rs, save_fig=True, file_path=TEST_PLOTS_PATH,
plot_scv_rs_matrix(freqs, t_inds, scv_rs,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_scv_rs_matrix.png')

@plot_test
def test_plot_spectral_hist(tsig):
def test_plot_spectral_hist(tsig_comb):

freqs, power_bins, spectral_hist = compute_spectral_hist(tsig, fs=1000)
spectrum_freqs, spectrum = compute_spectrum(tsig, fs=1000)
plot_spectral_hist(freqs, power_bins, spectral_hist, spectrum=spectrum,
spectrum_freqs=spectrum_freqs, save_fig=True, file_path=TEST_PLOTS_PATH,
freqs, power_bins, spectral_hist = compute_spectral_hist(tsig_comb, fs=FS)
spectrum_freqs, spectrum = compute_spectrum(tsig_comb, fs=FS)

plot_spectral_hist(freqs, power_bins, spectral_hist,
spectrum=spectrum, spectrum_freqs=spectrum_freqs,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_spectral_hist.png')
57 changes: 35 additions & 22 deletions neurodsp/tests/plts/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from pytest import raises
import numpy as np

from neurodsp.tests.settings import TEST_PLOTS_PATH
from neurodsp.utils import create_times
from neurodsp.burst import detect_bursts_dual_threshold
from neurodsp.timefrequency import amp_by_time, phase_by_time, freq_by_time

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

from neurodsp.plts.time_series import *
Expand All @@ -12,44 +16,53 @@
###################################################################################################

@plot_test
def test_plot_time_series(tsig):
def test_plot_time_series(tsig_comb, tsig_burst):

times = np.arange(0, len(tsig), 1)
times = create_times(N_SECONDS, FS)

# Check single time series plot
plot_time_series(times, tsig)
plot_time_series(times, tsig_comb,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_time_series-1.png')

# Check multi time series plot, with colors & labels
plot_time_series(times, [tsig, tsig[::-1]], labels=['signal', 'signal reversed'],
colors=['k', 'r'], save_fig=True, file_name='test_plot_time_series.png',
file_path=TEST_PLOTS_PATH)
# Check multi time series plot, labels
plot_time_series(times, [tsig_comb, tsig_comb[::-1]],
labels=['signal', 'signal reversed'],
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_time_series-2.png')

# Test 2D arrays
times_2d = np.vstack((times, times))
tsig_2d = np.vstack((tsig, tsig))
plot_time_series(times_2d, tsig_2d)
plot_time_series(times, np.array([tsig_comb, tsig_burst]),
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_time_series-2arr.png')

@plot_test
def test_plot_instantaneous_measure(tsig):
def test_plot_instantaneous_measure(tsig_comb):

times = np.arange(0, len(tsig), 1)
times = create_times(N_SECONDS, FS)

plot_instantaneous_measure(times, tsig, 'phase', save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_instantaneous_measure_phase.png')
plot_instantaneous_measure(times, tsig, 'amplitude', save_fig=True, file_path=TEST_PLOTS_PATH,
plot_instantaneous_measure(times, amp_by_time(tsig_comb, FS, F_RANGE), 'amplitude',
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_instantaneous_measure_amplitude.png')
plot_instantaneous_measure(times, tsig, 'frequency', save_fig=True, file_path=TEST_PLOTS_PATH,

plot_instantaneous_measure(times, phase_by_time(tsig_comb, FS, F_RANGE), 'phase',
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_instantaneous_measure_phase.png')

plot_instantaneous_measure(times, freq_by_time(tsig_comb, FS, F_RANGE), 'frequency',
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_instantaneous_measure_frequency.png')

# Check the error for bad measure
with raises(ValueError):
plot_instantaneous_measure(times, tsig, 'BAD')
plot_instantaneous_measure(times, tsig_comb, 'BAD')

@plot_test
def test_plot_bursts(tsig):
def test_plot_bursts(tsig_burst):

times = np.arange(0, len(tsig), 1)
bursts = np.array([True] * len(tsig))
times = create_times(N_SECONDS, FS)
bursts = detect_bursts_dual_threshold(tsig_burst, FS, (0.75, 1.5), F_RANGE)

plot_bursts(times, tsig, bursts, save_fig=True, file_path=TEST_PLOTS_PATH,
plot_bursts(times, tsig_burst, bursts,
save_fig=True, file_path=TEST_PLOTS_PATH,
file_name='test_plot_bursts.png')
Loading

0 comments on commit 1202ce2

Please sign in to comment.