Skip to main content

A collection of cython-optimized search functions for NumPy

Project description

ndfind

pypi python pytest Code style: black License

A collection of three cython-optimized search functions for NumPy. When the required value is found, they return immediately, without scanning the whole array. It can result in 1000x or larger speedups for huge arrays if the value is located close to the the beginning of the array.

Installation:

pip install ndfind

Contents

Basic usage:

  • find(a, v) finds v in a, returns index of the first match or -1 if not found
  • first_above(a, v) finds first element in a that is strictly greater than v, returns its index or -1 if not found
  • first_nonzero(a) finds the first nonzero element in a, returns its index or -1 if not found

Advanced usage:

  • find(a, v, rtol=1e-05, atol=1e-08, sorted=False, missing=-1, raises=False) Returns the index of the first element in a equal to v. If either a or v (or both) is of floating type, the parameters atol (absolute tolerance) and rtol (relative tolerance) are used for comparison (see np.isclose() for details).

    Otherwise, returns the missing value (-1 by default) or raises a ValueError if raises=True.

    For example,

    >>> find([3, 1, 4, 1, 5], 4)
    2
    >>> find([1, 2, 3], 7)
    -1
    >>> find([1.1, 1.2, 1.3], 1.2)
    1
    >>> find(np.arange(0, 1, 0.1), 0.3) 
    3
    >>> find([[3, 8, 4], [5, 2, 7]], 7)
    (1, 2)
    >>> find([[3, 8, 4], [5, 2, 7]], 9)
    -1
    >>> find([999980., 999990., 1e6], 1e6)
    1
    >>> find([999980., 999990., 1e6], 1e6, rtol=1e-9)
    2
  • first_above(a, v, sorted=False, missing=-1, raises=False) Returns the index of the first element in a strictly greater than v. If either a or v (or both) is of floating type, the parameters atol (absolute tolerance) and rtol (relative tolerance) are used for comparison (see np.isclose() for details).

    In 2D and above the the values in a are always tested and returned in row-major, C-style order.

    If there is no value in a greater than v, returns the default value (-1 by default) or raises a ValueError if raises=True.

    sorted, use binary search to speed things up (works only if the array is sorted)

    For example,

    >>> first_above([4, 5, 8, 2, 7], 6)
    2 
    >>> first_above([[4, 5, 8], [2, 7, 3]], 6)
    (0, 2) 
    >>> first_above([5, 6, 7], 9)
    3 
  • first_nonzero(a, missing=-1, raises=False) Returns the index of the first nonzero element in a.

    In 2D and above the the values in a are always tested and returned in row-major, C-style order.

    For example,

>>> first_nonzero([0, 0, 7, 0, 5])
2
>>> first_nonzero([False, True, False, False, True])
1
>>> first_nonzero([[0, 0, 0, 0], [0, 0, 5, 3]])
(1, 2)

Testing

Run pytest in the project root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ndfind-0.4.1.tar.gz (300.8 kB view hashes)

Uploaded source

Built Distributions

ndfind-0.4.1-cp311-cp311-win_amd64.whl (284.0 kB view hashes)

Uploaded cp311

ndfind-0.4.1-cp310-cp310-win_amd64.whl (283.0 kB view hashes)

Uploaded cp310

ndfind-0.4.1-cp39-cp39-win_amd64.whl (290.7 kB view hashes)

Uploaded cp39

ndfind-0.4.1-cp38-cp38-win_amd64.whl (294.6 kB view hashes)

Uploaded cp38

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page