Skip to main content

A fast nnls solver for python implemented in C++ using Eigen

Project description

fnnlsEigen

pypi

fnnlsEigen implements the fast non-negativity-constrained least squares algorithm (fnnls, Link to article). The algorithm is implemented in C++ using Eigen (Link) and is callable from Python using a Cython interface.

The nnls and the fnnls algorithms

The non-negative least squares algorithm solves the following problem for a given matrix Z and vector x:

where d is the solution vector.

The fnnls algorithm in this repository is optimised for intermediary sparsed matrices and is considerably faster than e.g. SciPy's nnls algorithm for large matrices.

Installing

$ python3 -m pip install fnnlsEigen

API

Direct usage

The fnnls solver accepts both np.float32 and np.float64 precision; dtype via fnnls and fnnlsf respectively.

>>> import numpy as np
>>> import fnnlsEigen as fe

>>> Z = np.array([[1.0, 0.0], [0.0, 1.0], [0.5, 0.5]], dtype=np.float64)
>>> x = np.array([1.0, 1.0, 0.0], dtype=np.float64)
>>> d = fe.fnnls(Z, x)
>>> d
array([0.66666667, 0.66666667])

Optionally, the breaking tolerance (tolerance) and maximum number of iterations (max_iterations) can be adjusted.

    max_iterations : int
        Defaults to 3 * array_Z.shape[1].
    tolerance : float32 / float64
        Defaults to machine epsilon of float32 / float64 times #columns in the given matrix.

Batched usage

Should the provided matrix Z change very little or not at all between calls to the solver; Performance can be enhanced by caching the Z.transpose() * Z product since this product is expensive. Two classes (float32 / float64) are provided as to enable memory mapping for the Z.transpose() * Z product:

CachePreComputeNNLS
>>> import numpy as np
>>> import fnnlsEigen as fe

>>> pc = fe.CachePreComputeNNLS()

>>> Z = np.abs(np.random.rand(500, 1000))

>>> for _ in range(0, 100):
>>>     x = np.abs(np.random.rand(500))
>>>     d = pc.fnnls(Z, x)

Contributing

Clone the project from github. Create a topic branch and issue a pull request for said branch. Wait for review.

Setup - Linux

On Linux platforms the environment can be easily set up as follows (after entering the cloned directory):

$ ./install_setup.sh

This creates a virtual environment and installs the python dependencies.

Enable the environment:

$ source env.sh

Compile the project:

$ make build

Tests and style checks can be accessed via:

$ make test

and

$ make check

Setup - Windows

Windows shell scripts and setup environments for Windows is a welcomed contribution.

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

fnnlsEigen-1.0.1.tar.gz (1.0 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fnnlsEigen-1.0.1-cp38-cp38-manylinux_2_24_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ x86-64

fnnlsEigen-1.0.1-cp37-cp37m-win_amd64.whl (81.1 kB view details)

Uploaded CPython 3.7mWindows x86-64

File details

Details for the file fnnlsEigen-1.0.1.tar.gz.

File metadata

  • Download URL: fnnlsEigen-1.0.1.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for fnnlsEigen-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8bb22fce10c2e4ba29fee07e0c2cabc405fe1e92744e69d49cf787527ed39345
MD5 2a2c56f57714061b543291de73b43800
BLAKE2b-256 c099e5198b4b9546beca06d4d65e739a20e16171120bd6ceede57cffb0a4f042

See more details on using hashes here.

File details

Details for the file fnnlsEigen-1.0.1-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

  • Download URL: fnnlsEigen-1.0.1-cp38-cp38-manylinux_2_24_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.8, manylinux: glibc 2.24+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for fnnlsEigen-1.0.1-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 104526d8a94b582b7fab29f96037fc473e95aad474d5a1782991ee8994474e8a
MD5 4b91e7b6a3a0e81cabd2abe3ac12440e
BLAKE2b-256 4bb50ea41aba33790544cd38c1eda61fbba0d0fbeb001d8ad7c9166d2c717ea3

See more details on using hashes here.

File details

Details for the file fnnlsEigen-1.0.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: fnnlsEigen-1.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 81.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for fnnlsEigen-1.0.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ed6aa383be7e5d318dad443f6ecdc5f1ee6a94e9057dd39c1c522a72c2a779db
MD5 d0e0bac0dd173a696039a462a4213f12
BLAKE2b-256 532d8548150ad63f6ae307a55a201e52314641f3db124b77c132b1e29328beb8

See more details on using hashes here.

Supported by

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