Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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])
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])
def test_ecg_intervalrelated():
data = nk.data("bio_resting_5min_100hz")
df, info = nk.ecg_process(data["ECG"], sampling_rate=100)
columns = ['ECG_Rate_Mean', 'HRV_RMSSD', 'HRV_MeanNN', 'HRV_SDNN', 'HRV_SDSD',
'HRV_CVNN', 'HRV_CVSD', 'HRV_MedianNN', 'HRV_MadNN', 'HRV_MCVNN',
'HRV_IQRNN', '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_SD1SD2', 'HRV_S', 'HRV_CSI', 'HRV_CVI', 'HRV_CSI_Modified',
'HRV_PIP', 'HRV_IALS', 'HRV_PSS', 'HRV_PAS', 'HRV_ApEn',
'HRV_SampEn', 'HRV_GI', 'HRV_SI', 'HRV_AI', 'HRV_PI',
'HRV_C1d', 'HRV_C1a', 'HRV_SD1d',
'HRV_SD1a', 'HRV_C2d',
'HRV_C2a', 'HRV_SD2d', 'HRV_SD2a',
'HRV_Cd', 'HRV_Ca', 'HRV_SDNNd',
'HRV_SDNNa']
# Test with signal dataframe
def test_ecg_eventrelated():
ecg, info = nk.ecg_process(nk.ecg_simulate(duration=20))
epochs = nk.epochs_create(ecg, events=[5000, 10000, 15000], epochs_start=-0.1, epochs_end=1.9)
ecg_eventrelated = nk.ecg_eventrelated(epochs)
# Test rate features
assert np.alltrue(np.array(ecg_eventrelated["ECG_Rate_Min"]) < np.array(ecg_eventrelated["ECG_Rate_Mean"]))
assert np.alltrue(np.array(ecg_eventrelated["ECG_Rate_Mean"]) < np.array(ecg_eventrelated["ECG_Rate_Max"]))
assert len(ecg_eventrelated["Label"]) == 3
def test_ecg_process():
sampling_rate = 1000
noise = 0.05
ecg = nk.ecg_simulate(sampling_rate=sampling_rate, noise=noise)
signals, info = nk.ecg_process(ecg, sampling_rate=sampling_rate, method="neurokit")
def test_hrv_time():
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)
_, peaks_slow = nk.ecg_process(ecg_slow, sampling_rate=1000)
_, peaks_fast = nk.ecg_process(ecg_fast, sampling_rate=1000)
hrv_slow = nk.hrv_time(peaks_slow, sampling_rate=1000)
hrv_fast = nk.hrv_time(peaks_fast, sampling_rate=1000)
assert np.all(hrv_fast["HRV_RMSSD"] < hrv_slow["HRV_RMSSD"])
assert np.all(hrv_fast["HRV_MeanNN"] < hrv_slow["HRV_MeanNN"])
assert np.all(hrv_fast["HRV_SDNN"] < hrv_slow["HRV_SDNN"])
assert np.all(hrv_fast["HRV_CVNN"] < hrv_slow["HRV_CVNN"])
assert np.all(hrv_fast["HRV_CVSD"] < hrv_slow["HRV_CVSD"])
assert np.all(hrv_fast["HRV_MedianNN"] < hrv_slow["HRV_MedianNN"])
assert np.all(hrv_fast["HRV_MadNN"] < hrv_slow["HRV_MadNN"])
assert np.all(hrv_fast["HRV_MCVNN"] < hrv_slow["HRV_MCVNN"])
assert np.all(hrv_fast["HRV_pNN50"] == hrv_slow["HRV_pNN50"])
assert np.all(hrv_fast["HRV_pNN20"] < hrv_slow["HRV_pNN20"])
assert np.all(hrv_fast["HRV_TINN"] < hrv_slow["HRV_TINN"])
nk.eda_plot(signals, sampling_rate=None)
# Save it
plot = nk.eda_plot(signals, sampling_rate=None)
plot.set_size_inches(10, 6, forward=True)
plot.savefig("README_eda.png", dpi=300, h_pad=3)
# =============================================================================
# Cardiac activity (ECG) processing
# =============================================================================
# Generate 15 seconds of ECG signal (recorded at 250 samples / second)
ecg = nk.ecg_simulate(duration=15, sampling_rate=250, heart_rate=70, random_state=333)
# Process it
signals, info = nk.ecg_process(ecg, sampling_rate=250)
# Visualise the processing
nk.ecg_plot(signals, sampling_rate=250)
# Save it
plot = nk.ecg_plot(signals, sampling_rate=250)
plot.set_size_inches(10, 6, forward=True)
plot.savefig("README_ecg.png", dpi=300, h_pad=3)
# =============================================================================
# Respiration (RSP) processing
# =============================================================================
# Generate one minute of RSP signal (recorded at 250 samples / second)
rsp = nk.rsp_simulate(duration=60, sampling_rate=250, respiratory_rate=15)
# Load the NeuroKit package
import neurokit2 as nk
# Simulate 30 seconds of ECG Signal (recorded at 250 samples / second)
ecg_signal = nk.ecg_simulate(duration=30, sampling_rate=250)
# Automatically process the (raw) ECG signal
signals, info = nk.ecg_process(ecg_signal, sampling_rate=250)
# Extract clean ECG and R-peasks location
rpeaks = info["ECG_R_Peaks"]
cleaned_ecg = signals["ECG_Clean"]
# Visualize R-peaks in ECG signal
nk.events_plot(rpeaks, cleaned_ecg)
# Segment the signal around the R-peaks
epochs = nk.epochs_create(cleaned_ecg, events=rpeaks, sampling_rate=250, epochs_start=-0.4, epochs_duration=1)
# Plotting all the heart beats
nk.epochs_plot(epochs)
nk.ecg_plot(signals)