def test_wavelet_return(): """Test the return shape and dtype. """ X = np.random.randn(1000, 32) rate = 200 Xh, _ = wavelet_transform(X, rate) assert Xh.shape == (X.shape[0], X.shape[1], 40) assert Xh.dtype == np.complex
def test_wavelet_return(filters, hg_only, dim, rate): """Test the return shape and dtype. """ X = np.random.randn(1000, 32) Xh, _, cfs, sds = wavelet_transform(X, rate, filters=filters, hg_only=hg_only) assert Xh.shape == (X.shape[0], X.shape[1], dim) assert Xh.dtype == np.complex
def test_pipeline(): """Test that the NWB pipeline gives equal results to running preprocessing functions by hand. """ num_channels = 64 duration = 10. # seconds sample_rate = 10000. # Hz new_sample_rate = 500. # hz nwbfile, device, electrode_group, electrodes = generate_nwbfile() neural_data = generate_synthetic_data(duration, num_channels, sample_rate) ECoG_ts = ElectricalSeries('ECoG_data', neural_data, electrodes, starting_time=0., rate=sample_rate) nwbfile.add_acquisition(ECoG_ts) electrical_series = nwbfile.acquisition['ECoG_data'] nwbfile.create_processing_module(name='preprocessing', description='Preprocessing.') # Resample rs_data_nwb, rs_series = store_resample( electrical_series, nwbfile.processing['preprocessing'], new_sample_rate) rs_data = resample(neural_data * 1e6, new_sample_rate, sample_rate) assert_array_equal(rs_data_nwb, rs_data) assert_array_equal(rs_series.data[:], rs_data) # Linenoise and CAR car_data_nwb, car_series = store_linenoise_notch_CAR( rs_series, nwbfile.processing['preprocessing']) nth_data = apply_linenoise_notch(rs_data, new_sample_rate) car_data = subtract_CAR(nth_data) assert_array_equal(car_data_nwb, car_data) assert_array_equal(car_series.data[:], car_data) # Wavelet transform tf_data_nwb, tf_series = store_wavelet_transform( car_series, nwbfile.processing['preprocessing'], filters='rat', hg_only=True, abs_only=False) tf_data, _, _, _ = wavelet_transform(car_data, new_sample_rate, filters='rat', hg_only=True) assert_array_equal(tf_data_nwb, tf_data) assert_array_equal(tf_series[0].data[:], abs(tf_data)) assert_array_equal(tf_series[1].data[:], np.angle(tf_data))
plt.plot(t[:500], car_data[:500, 0]) plt.xlabel('Time (s)') plt.ylabel('Amplitude (au)') _ = plt.title('One channel of neural data after re-referencing from resample') # %% # Time-frequency decomposition with wavelets # ------------------------------------------- # Here we decompose the neural time series into 6 different frequency subbands # in the high gamma range using a wavelet transform. The wavelet transform # amplitude is complex valued and here we take the absolute value. # # Note how the bands with center frequency nearest 100 Hz have larger amplitude. tf_data, _, ctr_freq, bw = wavelet_transform(car_data, new_sample_rate, filters='rat', hg_only=True) # Z scoring the amplitude instead of the complex waveform tf_data = abs(tf_data) num_tf_signals = len(ctr_freq) fig, axs = plt.subplots(num_tf_signals, 1, sharex=True, sharey=True, figsize=(15, 10)) fig.subplots_adjust(hspace=0.4) fig.tight_layout() for idx in range(num_tf_signals): sig = tf_data[:, 0, idx]
def test_wavelet_nyquist(filters, hg_only, dim, rate): """Test the return shape and dtype. """ X = np.random.randn(1000, 32) with pytest.raises(ValueError): wavelet_transform(X, rate, filters=filters, hg_only=hg_only)