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.rsp_rate(peaks90, desired_length=len(rsp90)) cleaned110 = nk.rsp_clean(rsp110, sampling_rate=1000) _, peaks110 = nk.rsp_peaks(cleaned110) rsp_rate110 = nk.rsp_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.array(rsp90_rrv["RRV_LF"]) < np.array(rsp110_rrv["RRV_LF"])
def test_rsp_rate(): rsp = nk.rsp_simulate(duration=120, sampling_rate=1000, respiratory_rate=15, method="sinusoidal", noise=0) rsp_cleaned = nk.rsp_clean(rsp, sampling_rate=1000) signals, info = nk.rsp_peaks(rsp_cleaned) # Test with dictionary. test_length = 30 rate = nk.rsp_rate(peaks=info, sampling_rate=1000, desired_length=test_length) assert rate.shape == (test_length, ) assert np.abs(rate.mean() - 15) < 0.2 # Test with DataFrame. rate = nk.rsp_rate(signals, sampling_rate=1000) assert rate.shape == (signals.shape[0], ) assert np.abs(rate.mean() - 15) < 0.2
def test_rsp_rate(): rsp = nk.rsp_simulate(duration=120, sampling_rate=1000, respiratory_rate=15) rsp_cleaned = nk.rsp_clean(rsp, sampling_rate=1000) signals, info = nk.rsp_findpeaks(rsp_cleaned, sampling_rate=1000) # vary desired_lenght over tests # test with peaks only test_length = 30 data = nk.rsp_rate(info["RSP_Peaks"], sampling_rate=1000, desired_length=test_length) assert data.shape == (test_length, 2) assert np.abs(data["RSP_Rate"].mean() - 15) < 0.2 assert np.abs(data["RSP_Period"].mean() - 4) < 0.1 # test with peaks and troughs passed in separately test_length = 300 data = nk.rsp_rate(peaks=info["RSP_Peaks"], troughs=info["RSP_Troughs"], sampling_rate=1000, desired_length=test_length) assert data.shape == (test_length, 3) assert np.abs(data["RSP_Rate"].mean() - 15) < 0.2 assert np.abs(data["RSP_Period"].mean() - 4) < 0.1 assert np.abs(data["RSP_Amplitude"].mean() - 2086) < 0.5 # test with DataFrame containing peaks and troughs data = nk.rsp_rate(signals, sampling_rate=1000) assert data.shape == (signals.shape[0], 3) assert np.abs(data["RSP_Rate"].mean() - 15) < 0.2 assert np.abs(data["RSP_Period"].mean() - 4) < 0.1 assert np.abs(data["RSP_Amplitude"].mean() - 2087) < 0.5 # test with dict containing peaks and troughs test_length = 30000 data = nk.rsp_rate(info, sampling_rate=1000, desired_length=test_length) assert data.shape == (test_length, 3) assert np.abs(data["RSP_Rate"].mean() - 15) < 0.2 assert np.abs(data["RSP_Period"].mean() - 4) < 0.1 assert np.abs(data["RSP_Amplitude"].mean() - 2087) < 0.5
def rsp_custom_process(distorted, info, detrend_position="First", detrend_method="polynomial", detrend_order=0, detrend_regularization=500, detrend_alpha=0.75, filter_type="None", filter_order=5, filter_lowcut=None, filter_highcut=None): sampling_rate = info["Sampling_Rate"][0] if detrend_position in ["First", 'Both']: distorted = nk.signal_detrend(distorted, method=detrend_method, order=detrend_order, regularization=detrend_regularization, alpha=detrend_alpha) if filter_type != "None": distorted = nk.signal_filter(signal=distorted, sampling_rate=sampling_rate, lowcut=filter_lowcut, highcut=filter_highcut, method=filter_type, order=filter_order) if detrend_position in ["Second", 'Both']: distorted = nk.signal_detrend(distorted, method=detrend_method, order=int(detrend_order), regularization=detrend_regularization, alpha=detrend_alpha) cleaned = distorted extrema_signal, _ = nk.rsp_findpeaks(distorted, outlier_threshold=0) try: rate = nk.rsp_rate(peaks=extrema_signal, sampling_rate=sampling_rate) except ValueError: rate = np.full(len(distorted), np.nan) info["Detrend_Method"] = [detrend_method] info["Detrend_Order"] = [detrend_order] info["Detrend_Regularization"] = [detrend_regularization] info["Detrend_Alpha"] = [detrend_alpha] info["Detrend_Position"] = [detrend_position] info["Filter_Method"] = [filter_type] if filter_type in ["Butterworth", "Bessel"]: info["Filter_Type"] = [filter_type + "_" + str(filter_order)] else: info["Filter_Type"] = [filter_type] info["Filter_Order"] = [filter_order] info["Filter_Low"] = [filter_lowcut] info["Filter_High"] = [filter_highcut] if filter_lowcut is None and filter_highcut is None: info["Filter_Band"] = "None" else: info["Filter_Band"] = [str(np.round(filter_lowcut, 3)) + ", " + str(np.round(filter_highcut, 3))] return rate, info, cleaned
def rsp_custom_process(distorted, info, detrend_position="First", detrend_order=0, filter_order=5, filter_lowcut=None, filter_highcut=2): sampling_rate = info["Sampling_Rate"][0] if detrend_position == "First": distorted = nk.signal_detrend(distorted, order=detrend_order) if filter_lowcut == 0: actual_filter_lowcut = None else: actual_filter_lowcut = filter_lowcut distorted = nk.signal_filter(signal=distorted, sampling_rate=sampling_rate, lowcut=actual_filter_lowcut, highcut=filter_highcut, method="butterworth", butterworth_order=filter_order) if detrend_position == "Second": distorted = nk.signal_detrend(distorted, order=detrend_order) extrema_signal, _ = nk.rsp_findpeaks(distorted, outlier_threshold=0.3) try: rate = nk.rsp_rate(peaks=extrema_signal, sampling_rate=sampling_rate)["RSP_Rate"] except ValueError: rate = np.full(len(distorted), np.nan) info["Detrend_Order"] = [detrend_order] info["Detrend_Position"] = [detrend_position] info["Filter_Order"] = [filter_order] info["Filter_Low"] = [filter_lowcut] info["Filter_High"] = [filter_highcut] return rate, info