def test_signal_resample(): signal = np.cos(np.linspace(start=0, stop=20, num=50)) downsampled_interpolation = nk.signal_resample(signal, method="interpolation", sampling_rate=1000, desired_sampling_rate=500) downsampled_numpy = nk.signal_resample(signal, method="numpy", sampling_rate=1000, desired_sampling_rate=500) downsampled_pandas = nk.signal_resample(signal, method="pandas", sampling_rate=1000, desired_sampling_rate=500) downsampled_fft = nk.signal_resample(signal, method="FFT", sampling_rate=1000, desired_sampling_rate=500) downsampled_poly = nk.signal_resample(signal, method="poly", sampling_rate=1000, desired_sampling_rate=500) # Upsample upsampled_interpolation = nk.signal_resample(downsampled_interpolation, method="interpolation", sampling_rate=500, desired_sampling_rate=1000) upsampled_numpy = nk.signal_resample(downsampled_numpy, method="numpy", sampling_rate=500, desired_sampling_rate=1000) upsampled_pandas = nk.signal_resample(downsampled_pandas, method="pandas", sampling_rate=500, desired_sampling_rate=1000) upsampled_fft = nk.signal_resample(downsampled_fft, method="FFT", sampling_rate=500, desired_sampling_rate=1000) upsampled_poly = nk.signal_resample(downsampled_poly, method="poly", sampling_rate=500, desired_sampling_rate=1000) # Check rez = pd.DataFrame({ "Interpolation": upsampled_interpolation - signal, "Numpy": upsampled_numpy - signal, "Pandas": upsampled_pandas - signal, "FFT": upsampled_fft - signal, "Poly": upsampled_poly - signal }) assert np.allclose(np.mean(rez.mean()), 0.0001, atol=0.0001)
def test_hrv_frequency(): # Test frequency domain ecg1 = nk.ecg_simulate(duration=60, sampling_rate=2000, heart_rate=70, random_state=42) _, peaks1 = nk.ecg_process(ecg1, sampling_rate=2000) hrv1 = nk.hrv_frequency(peaks1, sampling_rate=2000) ecg2 = nk.signal_resample(ecg1, sampling_rate=2000, desired_sampling_rate=500) _, peaks2 = nk.ecg_process(ecg2, sampling_rate=500) hrv2 = nk.hrv_frequency(peaks2, sampling_rate=500) assert np.allclose(hrv1["HRV_HF"] - hrv2["HRV_HF"], 0, atol=1.5) assert np.isnan(hrv1["HRV_LF"][0]) assert np.isnan(hrv2["HRV_LF"][0]) assert np.isnan(hrv1["HRV_VLF"][0]) assert np.isnan(hrv2["HRV_LF"][0]) # Test warning on too short duration with pytest.warns(nk.misc.NeuroKitWarning, match=r"The duration of recording is too short.*"): ecg3 = nk.ecg_simulate(duration=10, sampling_rate=2000, heart_rate=70, random_state=42) _, peaks3 = nk.ecg_process(ecg3, sampling_rate=2000) nk.hrv_frequency(peaks3, sampling_rate=2000, silent=False)
def resampling_data_signal(data: Dict[str, Dict[str, np.array]], sampling_rate: int, desired_sampling_rate: int, method: str = 'interpolation') -> Dict[str, Dict[str, object]]: output = defaultdict(dict) for participant_id, dataset in data.items(): for task_id, signal_data in dataset.items(): resampled_signal = nk.signal_resample(signal_data, method = method, sampling_rate = sampling_rate, desired_sampling_rate = desired_sampling_rate) output[participant_id][task_id] = resampled_signal return output
def process_eda(eda, show_fig=False): """ Resample EDA signal from 4 Hz to 64 Hz. Compute EDA signal features (more info: https://neurokit2.readthedocs.io/en/latest/functions.html#module-neurokit2.eda). Parameters ---------- eda : dict [timestamp : value] EDA signal. show_fig : bool set if plot specific features. Returns ------- eda_signals : DataFrame eda_info : dict """ eda_signal = nk.signal_resample(eda['value'], sampling_rate=4, desired_sampling_rate=64) eda_signals, eda_info = nk.eda_process(eda_signal, sampling_rate=64) if show_fig: plt.plot(eda_signals['EDA_Phasic'], label='fázická složka') plt.plot(eda_signals['EDA_Tonic'], label='tónická složka') plt.plot(eda_signals['EDA_Raw'], label='původní') plt.xlabel('Vzorek [n]') plt.ylabel('EDA [uS]') plt.legend() plt.show() return eda_signals, eda_info
def test_ecg_hrv(): ecg_slow = nk.ecg_simulate(duration=60, sampling_rate=1000, heart_rate=70, random_state=42) ecg_fast = nk.ecg_simulate(duration=60, sampling_rate=1000, heart_rate=110, random_state=42) ecg_slow, _ = nk.ecg_process(ecg_slow, sampling_rate=1000) ecg_fast, _ = nk.ecg_process(ecg_fast, sampling_rate=1000) ecg_slow_hrv = nk.ecg_hrv(ecg_slow, sampling_rate=1000) ecg_fast_hrv = nk.ecg_hrv(ecg_fast, sampling_rate=1000) assert ecg_fast_hrv["HRV_RMSSD"][0] < ecg_slow_hrv["HRV_RMSSD"][0] assert ecg_fast_hrv["HRV_MeanNN"][0] < ecg_slow_hrv["HRV_MeanNN"][0] assert ecg_fast_hrv["HRV_SDNN"][0] < ecg_slow_hrv["HRV_SDNN"][0] assert ecg_fast_hrv["HRV_CVNN"][0] < ecg_slow_hrv["HRV_CVNN"][0] assert ecg_fast_hrv["HRV_CVSD"][0] < ecg_slow_hrv["HRV_CVSD"][0] assert ecg_fast_hrv["HRV_MedianNN"][0] < ecg_slow_hrv["HRV_MedianNN"][0] assert ecg_fast_hrv["HRV_MadNN"][0] < ecg_slow_hrv["HRV_MadNN"][0] assert ecg_fast_hrv["HRV_MCVNN"][0] < ecg_slow_hrv["HRV_MCVNN"][0] assert ecg_fast_hrv["HRV_pNN50"][0] == ecg_slow_hrv["HRV_pNN50"][0] assert ecg_fast_hrv["HRV_pNN20"][0] < ecg_slow_hrv["HRV_pNN20"][0] assert ecg_fast_hrv["HRV_TINN"][0] < ecg_slow_hrv["HRV_TINN"][0] assert ecg_fast_hrv["HRV_HTI"][0] > ecg_slow_hrv["HRV_HTI"][0] assert ecg_fast_hrv["HRV_ULF"][0] == ecg_slow_hrv["HRV_ULF"][0] == 0 assert all(elem in [ 'HRV_RMSSD', 'HRV_MeanNN', 'HRV_SDNN', 'HRV_SDSD', 'HRV_CVNN', 'HRV_CVSD', 'HRV_MedianNN', 'HRV_MadNN', 'HRV_MCVNN', 'HRV_pNN50', 'HRV_pNN20', 'HRV_TINN', 'HRV_HTI', 'HRV_ULF', 'HRV_VLF', 'HRV_LF', 'HRV_HF', 'HRV_VHF', 'HRV_LFHF', 'HRV_LFn', 'HRV_HFn', 'HRV_LnHF', 'HRV_SD1', 'HRV_SD2', 'HRV_SD2SD1', 'HRV_CSI', 'HRV_CVI', 'HRV_CSI_Modified', 'HRV_SampEn' ] for elem in np.array(ecg_fast_hrv.columns.values, dtype=str)) # Test frequency domain ecg1 = nk.ecg_simulate(duration=60, sampling_rate=2000, heart_rate=70, random_state=42) hrv1 = nk.ecg_hrv(nk.ecg_process(ecg1, sampling_rate=2000)[0], sampling_rate=2000) ecg2 = nk.signal_resample(ecg1, sampling_rate=2000, desired_sampling_rate=500) hrv2 = nk.ecg_hrv(nk.ecg_process(ecg2, sampling_rate=500)[0], sampling_rate=500) assert np.allclose(np.mean(hrv1[["HRV_HF", "HRV_LF", "HRV_VLF"]].iloc[0] - hrv2[["HRV_HF", "HRV_LF", "HRV_VLF"]].iloc[0]), 0, atol=1)
def test_hrv_frequency(): # Test frequency domain ecg1 = nk.ecg_simulate(duration=60, sampling_rate=2000, heart_rate=70, random_state=42) _, peaks1 = nk.ecg_process(ecg1, sampling_rate=2000) hrv1 = nk.hrv_frequency(peaks1, sampling_rate=2000) ecg2 = nk.signal_resample(ecg1, sampling_rate=2000, desired_sampling_rate=500) _, peaks2 = nk.ecg_process(ecg2, sampling_rate=500) hrv2 = nk.hrv_frequency(peaks2, sampling_rate=500) assert np.allclose(hrv1["HRV_HF"] - hrv2["HRV_HF"], 0, atol=1.5) assert np.isnan(hrv1["HRV_LF"][0]) assert np.isnan(hrv2["HRV_LF"][0]) assert np.isnan(hrv1["HRV_VLF"][0]) assert np.isnan(hrv2["HRV_LF"][0])