def test_rsp_rrv(): rsp90 = nk.rsp_simulate(duration=60, sampling_rate=1000, respiratory_rate=90, random_state=42) rsp110 = nk.rsp_simulate(duration=60, sampling_rate=1000, respiratory_rate=110, random_state=42) cleaned90 = nk.rsp_clean(rsp90, sampling_rate=1000) _, peaks90 = nk.rsp_peaks(cleaned90) rsp_rate90 = nk.signal_rate(peaks90, desired_length=len(rsp90)) cleaned110 = nk.rsp_clean(rsp110, sampling_rate=1000) _, peaks110 = nk.rsp_peaks(cleaned110) rsp_rate110 = nk.signal_rate(peaks110, desired_length=len(rsp110)) rsp90_rrv = nk.rsp_rrv(rsp_rate90, peaks90) rsp110_rrv = nk.rsp_rrv(rsp_rate110, peaks110) assert np.array(rsp90_rrv["RRV_SDBB"]) < np.array(rsp110_rrv["RRV_SDBB"]) assert np.array(rsp90_rrv["RRV_RMSSD"]) < np.array(rsp110_rrv["RRV_RMSSD"]) assert np.array(rsp90_rrv["RRV_SDSD"]) < np.array(rsp110_rrv["RRV_SDSD"]) # assert np.array(rsp90_rrv["RRV_pNN50"]) == np.array(rsp110_rrv["RRV_pNN50"]) == np.array(rsp110_rrv["RRV_pNN20"]) == np.array(rsp90_rrv["RRV_pNN20"]) == 0 # assert np.array(rsp90_rrv["RRV_TINN"]) < np.array(rsp110_rrv["RRV_TINN"]) # assert np.array(rsp90_rrv["RRV_HTI"]) > np.array(rsp110_rrv["RRV_HTI"]) assert np.array(rsp90_rrv["RRV_HF"]) < np.array(rsp110_rrv["RRV_HF"]) assert np.isnan(rsp90_rrv["RRV_LF"][0]) assert np.isnan(rsp110_rrv["RRV_LF"][0])
def test_signal_rate(): # Test with array. signal = nk.signal_simulate(duration=10, sampling_rate=1000, frequency=1) info = nk.signal_findpeaks(signal) rate = nk.signal_rate(peaks=info["Peaks"], sampling_rate=1000, desired_length=None) assert rate.shape[0] == len(info["Peaks"]) # Test with dictionary.produced from signal_findpeaks. assert info[list(info.keys())[0]].shape == (info["Peaks"].shape[0], ) # Test with DataFrame. rsp = nk.rsp_simulate(duration=120, sampling_rate=1000, respiratory_rate=15, method="sinuosoidal", noise=0) rsp_cleaned = nk.rsp_clean(rsp, sampling_rate=1000) signals, info = nk.rsp_peaks(rsp_cleaned) rate = nk.signal_rate(signals, sampling_rate=1000) assert rate.shape == (signals.shape[0], ) # Test with dictionary.produced from rsp_findpeaks. test_length = 30 rate = nk.signal_rate(info, sampling_rate=1000, desired_length=test_length) assert rate.shape == (test_length, )
def test_signal_rate(): # since singal_rate wraps signal_period, the latter is tested as well # Test with array. duration = 10 sampling_rate = 1000 signal = nk.signal_simulate(duration=duration, sampling_rate=sampling_rate, frequency=1) info = nk.signal_findpeaks(signal) rate = nk.signal_rate(peaks=info["Peaks"], sampling_rate=1000, desired_length=len(signal)) assert rate.shape[0] == duration * sampling_rate # Test with dictionary.produced from signal_findpeaks. assert info[list(info.keys())[0]].shape == (info["Peaks"].shape[0],) # Test with DataFrame. duration = 120 sampling_rate = 1000 rsp = nk.rsp_simulate( duration=duration, sampling_rate=sampling_rate, respiratory_rate=15, method="sinuosoidal", noise=0 ) rsp_cleaned = nk.rsp_clean(rsp, sampling_rate=sampling_rate) signals, info = nk.rsp_peaks(rsp_cleaned) rate = nk.signal_rate(signals, sampling_rate=sampling_rate, desired_length=duration * sampling_rate) assert rate.shape == (signals.shape[0],) # Test with dictionary.produced from rsp_findpeaks. rate = nk.signal_rate(info, sampling_rate=sampling_rate, desired_length=duration * sampling_rate) assert rate.shape == (duration * sampling_rate,)
def signal_process(pulses, sampling_rate): """ 数据的初步处理分析 寻找峰值 初步时域分析 :param pulses: 标准化的数据 :param sampling_rate: 采样率 :return: """ # 数据检查 是否是1维格式 pulses = nk.as_vector(pulses) # 数据去噪 低通滤波去噪 bessel cleaned_pulses = nk.signal_filter(pulses, sampling_rate=sampling_rate, lowcut=0.5, highcut=8, order=3, method="bessel") # common_plot(None, cleaned_pulses, title="Cleand Data", sampling_rate=sampling_rate) feature_points = find_feature_points(cleaned_pulses, sampling_rate) # 寻找其他特征点 peaks = feature_points["Peaks"] # 计算心率 rates = nk.signal_rate(peaks, sampling_rate=sampling_rate, desired_length=None) feature_points["Heart Rates"] = rates feature_points["HR_Time"] = np.mean(rates) print(np.round(np.mean(rates), 2)) return feature_points pass
def test_rsp_rrv(): rsp90 = nk.rsp_simulate(duration=60, sampling_rate=1000, respiratory_rate=90, random_state=42) rsp110 = nk.rsp_simulate(duration=60, sampling_rate=1000, respiratory_rate=110, random_state=42) cleaned90 = nk.rsp_clean(rsp90, sampling_rate=1000) _, peaks90 = nk.rsp_peaks(cleaned90) rsp_rate90 = nk.signal_rate(peaks90, desired_length=len(rsp90)) cleaned110 = nk.rsp_clean(rsp110, sampling_rate=1000) _, peaks110 = nk.rsp_peaks(cleaned110) rsp_rate110 = nk.signal_rate(peaks110, desired_length=len(rsp110)) rsp90_rrv = nk.rsp_rrv(rsp_rate90, peaks90) rsp110_rrv = nk.rsp_rrv(rsp_rate110, peaks110) assert np.array(rsp90_rrv["RRV_SDBB"]) < np.array(rsp110_rrv["RRV_SDBB"]) assert np.array(rsp90_rrv["RRV_RMSSD"]) < np.array(rsp110_rrv["RRV_RMSSD"]) assert np.array(rsp90_rrv["RRV_SDSD"]) < np.array(rsp110_rrv["RRV_SDSD"]) # assert np.array(rsp90_rrv["RRV_pNN50"]) == np.array(rsp110_rrv["RRV_pNN50"]) == np.array(rsp110_rrv["RRV_pNN20"]) == np.array(rsp90_rrv["RRV_pNN20"]) == 0 # assert np.array(rsp90_rrv["RRV_TINN"]) < np.array(rsp110_rrv["RRV_TINN"]) # assert np.array(rsp90_rrv["RRV_HTI"]) > np.array(rsp110_rrv["RRV_HTI"]) assert np.array(rsp90_rrv["RRV_HF"]) < np.array(rsp110_rrv["RRV_HF"]) assert np.isnan(rsp90_rrv["RRV_LF"][0]) assert np.isnan(rsp110_rrv["RRV_LF"][0]) # Test warning on too short duration with pytest.warns(nk.misc.NeuroKitWarning, match=r"The duration of recording is too short.*"): short_rsp90 = nk.rsp_simulate(duration=10, sampling_rate=1000, respiratory_rate=90, random_state=42) short_cleaned90 = nk.rsp_clean(short_rsp90, sampling_rate=1000) _, short_peaks90 = nk.rsp_peaks(short_cleaned90) short_rsp_rate90 = nk.signal_rate(short_peaks90, desired_length=len(short_rsp90)) nk.rsp_rrv(short_rsp_rate90, short_peaks90)