Detect indices of sequential data identical to a value

Marcos Duarte
Laboratory of Biomechanics and Motor Control](http://demotu.org/
Federal University of ABC, Brazil

The function detect_seq.py from Python module detecta detects initial and final indices of sequential data identical to parameter value in x.
Use parameter min_seq to set the minimum number of sequential values to detect.

The signature of detect_seq.py is:

idx = detect_seq(x, value=np.nan, index=False, min_seq=1, max_alert=0, show=False, ax=None)

Installation

pip install detecta

Or

conda install -c duartexyz detecta
In [1]:
import numpy as np
%matplotlib inline

from detecta import detect_seq
In [2]:
help(detect_seq)
Help on function detect_seq in module detecta.detect_seq:

detect_seq(x, value=nan, index=False, min_seq=1, max_alert=0, show=False, ax=None)
    Detect indices in x of sequential data identical to value.
    
    Parameters
    ----------
    x : 1D numpy array_like
        data
    value : number, optional. Default = np.nan
        Value to be found in data
    index : bool, optional. Default = False
        Set to True to return a 2D array of initial and final indices where
        data is equal to value or set to False to return an 1D array of Boolean
        values with same length as x with True where x is equal to value and 
        False where x is not equal to value.
    min_seq : integer, optional. Default = 1
        Minimum number of sequential values to detect        
    max_alert : number, optional. Default = 0
        Minimal number of sequential data for a message to be printed with
        information about these indices. Set to 0 to not print any message.
    show : bool, optional. Default = False
        Show plot (True) of not (False).
    ax : matplotlib object, optional. Default = None
        Matplotlib axis object where to plot.
        
    Returns
    -------
    idx : 1D or 2D numpy array_like
        2D numpy array [indi, indf] of initial and final indices (if index is
        equal to True) or 1D array of Boolean values with same length as x (if
        index is equal to False).
            
    References
    ----------
    .. [1] https://github.com/demotu/detecta/blob/master/docs/detect_seq.ipynb
    
    Examples
    --------
    >>> x = [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0]
    >>> detect_seq(x, 0)
    
    >>> detect_seq(x, 0, index=True)
    
    >>> detect_seq(x, 0, index=True, min_seq=2)  
    
    >>> detect_seq(x, 10)
    
    >>> detect_seq(x, 10, index=True)
    
    >>> detect_seq(x, 0, index=True, min_seq=2, show=True)
    
    >>> detect_seq(x, 0, index=True, max_alert=2)
    
    Version history
    ---------------
    '1.0.1':
        Part of the detecta module - https://pypi.org/project/detecta/

In [3]:
x = [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0]
In [4]:
detect_seq(x, 0)
Out[4]:
array([False,  True,  True,  True, False, False, False,  True, False,
        True,  True,  True])
In [5]:
detect_seq(x, 0, index=True)
Out[5]:
array([[ 1,  3],
       [ 7,  7],
       [ 9, 11]])
In [6]:
detect_seq(x, 0, index=True, min_seq=2)
Out[6]:
array([[ 1,  3],
       [ 9, 11]])
In [7]:
detect_seq(x, 10)
Out[7]:
array([False, False, False, False, False, False, False, False, False,
       False, False, False])
In [8]:
detect_seq(x, 10, index=True)
Out[8]:
array([], shape=(0, 2), dtype=int64)
In [9]:
detect_seq(x, 1, index=True, min_seq=2, show=True)
Out[9]:
array([[4, 6]])
In [10]:
detect_seq(x, 1, index=False, min_seq=1, show=True)
Out[10]:
array([ True, False, False, False,  True,  True,  True, False,  True,
       False, False, False])
In [11]:
detect_seq(x, 0, index=True, min_seq=2, show=True)
Out[11]:
array([[ 1,  3],
       [ 9, 11]])
In [12]:
detect_seq(x, 0, index=True, max_alert=2)
Sequential data equal or longer than 2: ([2], [1 3])
Sequential data equal or longer than 2: ([2], [ 9 11])
Out[12]:
array([[ 1,  3],
       [ 7,  7],
       [ 9, 11]])
In [13]:
x = [1, 2, np.nan, np.nan, 5, 4, 5, np.nan, 2, 1, 2]
detect_seq(x, np.nan, index=True, max_alert=2, show=True)
Out[13]:
array([[2, 3],
       [7, 7]])
In [ ]: