Skip to content

Jax core threshold

threshold(time_array, data_array, starts, ends, thr, method)

Threshold function for pynajax

Parameters:

Name Type Description Default
time_array ArrayLike
required
data_array ArrayLike
required
starts ArrayLike
required
ends ArrayLike
required
thr Number
required
method string
required

Returns:

Type Description
tuple of ArrayLike

Description

Source code in src/pynajax/jax_core_threshold.py
 6
 7
 8
 9
10
11
12
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
def threshold(time_array, data_array, starts, ends, thr, method):
    """Threshold function for pynajax

    Parameters
    ----------
    time_array : ArrayLike

    data_array : ArrayLike

    starts : ArrayLike

    ends : ArrayLike

    thr : Number

    method : string


    Returns
    -------
    tuple of ArrayLike
        Description
    """
    if not isinstance(data_array, jnp.ndarray):
        data_array = jnp.asarray(data_array)

    idx_start, idx_end = _get_idxs(time_array, starts, ends)
    idx_slicing = _get_slicing(idx_start, idx_end)

    data_array = data_array[idx_slicing]
    time_array = time_array[idx_slicing]

    if method == "above":
        ix = data_array > thr
    elif method == "below":
        ix = data_array < thr
    elif method == "aboveequal":
        ix = data_array >= thr
    elif method == "belowequal":
        ix = data_array <= thr

    ix2 = jnp.diff(ix * 1)

    new_starts = (
        time_array[1:][ix2 == 1]
        - (time_array[1:][ix2 == 1] - time_array[0:-1][ix2 == 1]) / 2
    )
    new_ends = (
        time_array[0:-1][ix2 == -1]
        + (time_array[1:][ix2 == -1] - time_array[0:-1][ix2 == -1]) / 2
    )

    if ix[0]:  # First element to keep as start
        new_starts = jnp.hstack((jnp.array([time_array[0]]), new_starts))
    if ix[-1]:  # last element to keep as end
        new_ends = jnp.hstack((new_ends, jnp.array([time_array[-1]])))

    return time_array[ix], data_array[ix], new_starts, new_ends