Skip to content

False-Positive Event Removal

biosigpy.hrv.removefp.removefp

removefp(tk: ArrayLike) -> np.ndarray

Remove detections preceded by an abnormally short interval.

Parameters:

Name Type Description Default
tk array_like

Non-empty, finite, strictly increasing event timestamps in seconds. The time origin is unrestricted.

required

Returns:

Type Description
ndarray

One-dimensional event timestamps after simultaneous one-pass removal.

Raises:

Type Description
TypeError

If tk is non-numeric or complex.

ValueError

If tk is empty, non-finite, not a vector, or not strictly increasing.

Notes

The adaptive baseline uses :func:biosigpy.tools.medfilt_threshold with the fixed Biosiglib settings. All flags are computed from the original interval series before any event is removed. This function does not sort its input.

Examples:

>>> from biosigpy.hrv import removefp
>>> removefp([0, 1, 2, 2.2, 3, 4, 5])
array([0., 1., 2., 3., 4., 5.])
Source code in src/biosigpy/hrv/removefp.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def removefp(tk: ArrayLike) -> np.ndarray:
    """Remove detections preceded by an abnormally short interval.

    Parameters
    ----------
    tk : array_like
        Non-empty, finite, strictly increasing event timestamps in seconds.
        The time origin is unrestricted.

    Returns
    -------
    numpy.ndarray
        One-dimensional event timestamps after simultaneous one-pass removal.

    Raises
    ------
    TypeError
        If ``tk`` is non-numeric or complex.
    ValueError
        If ``tk`` is empty, non-finite, not a vector, or not strictly
        increasing.

    Notes
    -----
    The adaptive baseline uses :func:`biosigpy.tools.medfilt_threshold` with
    the fixed Biosiglib settings. All flags are computed from the original
    interval series before any event is removed. This function does not sort
    its input.

    Examples
    --------
    >>> from biosigpy.hrv import removefp
    >>> removefp([0, 1, 2, 2.2, 3, 4, 5])
    array([0., 1., 2., 3., 4., 5.])
    """

    events = as_real_vector(tk, name="tk")
    if events.size == 0:
        raise ValueError("tk must not be empty")
    if np.any(~np.isfinite(events)):
        raise ValueError("tk must contain only finite values")
    if np.any(np.diff(events) <= 0):
        raise ValueError("tk must be strictly increasing")
    if events.size < 3:
        return events.copy()

    intervals = np.diff(events)
    baseline = medfilt_threshold(
        intervals, window=30, factor=1.0, max_threshold=1.5
    )
    false_positive_intervals = intervals < 0.7 * baseline
    keep = np.concatenate(([True], ~false_positive_intervals))
    return events[keep]

View executable example