Esempio n. 1
0
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
Esempio n. 2
0
def test_rsp_findpeaks():

    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)
    assert signals.shape == (120000, 2)
    assert signals["RSP_Peaks"].sum() == 28
    assert signals["RSP_Troughs"].sum() == 28
    assert info["RSP_Peaks"].shape[0] == 28
    assert info["RSP_Troughs"].shape[0] == 28
    # assert that extrema start with a trough and end with a peak
    assert info["RSP_Peaks"][0] > info["RSP_Troughs"][0]
    assert info["RSP_Peaks"][-1] > info["RSP_Troughs"][-1]
Esempio n. 3
0
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
Esempio n. 4
0
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