pyfigtree
A python ctypes wrapper of the figtree library for fast Gaussian summation by V. Morariu et al.
The main function for users is pyfigtree.figtree. It computes the
improved fast Gauss transform
g(y) = \sum_{i=1}^N w_i \exp( -|x_i - y|^2 / h^2)
for N samples {x_i} at the target point y.
Kernel density estimation
For a properly normalized Gaussian kernel density estimation in 1D, the weight is
w_i = 1 / (N \sqrt{\pi h^2}),
where h is the bandwidth. Details about the algorithm and the
parameters are given in the
original paper.
Note that multidimensional input usually has to be transformed to avoid distortions if the variates are of different scales. The fastest strategy is to scale the samples to the unit hypercube. For example in 2D
for i in range(2):
x[:, i] = (x[:, i] - x[:, i].min()) / (x[:, i].max() - x[:, i].min())
If that is not good enough (i.e., scales still too different), transform into almost principal components as suggested in Scott, Sain (1992), section 3.3.
Example
Sample from a unit Gaussian, and do kernel density estimation with figtree. The weights are adjusted such that the density is normalized correctly.
from figtree import figtree
import numpy as np
samples = np.random.normal(size=1000)
bandwidth = 0.5
weights = np.ones(len(samples)) / len(samples) / np.sqrt(np.pi) / bandwidth
target_points = np.linspace(-5, 5, 70)
target_densities = figtree(samples, target_points, weights, bandwidth=bandwidth)
from matplotlib import pyplot as plt
plt.plot(target_points, target_densities)
plt.hist(samples, histtype='stepfilled', normed=True)
plt.show()
Installation with pip
pip install pyfigtree
Installation from source
This wrapper has been developed and tested only on linux. To use it, first install both the figtree and the ANN library following the instructions at https://github.com/vmorariu/figtree and make the libraries available to the loader at runtime. For example,
export FIGTREEDIR=/path/to/figtree
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$FIGTREEDIR/lib
export PYTHONPATH=/path/to/pyfigtree:$PYTHONPATH
Then
- add
pyfigtree.pyto yourPYTHON_PATH; - make sure
numpyis installed; - test the setup by executing
python figtree.pyto run a set of unit tests.
Historical note
I wrote this wrapper around 2011. In 2014, I figured I should clean it up and release it on github because it may be useful to others. When I notified Vlad Morariu, the author of figtree, he told me that he had in fact done the same so now there are two python wrappers.
What's the difference? Vlad uses cython and I use ctypes; the
latter is included in a standard python installation, so I have one
less dependency and I don't need to compile anything, the only requirement
is to be able to load the figtree libraries at runtime.
License
Copyright (c) 2014 Frederik Beaujean Frederik.Beaujean@lmu.de
Pyfigtree is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2, as published by the Free Software Foundation.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Metadata
Release files for pyfigtree 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyfigtree-1.0.1.tar.gz | 85.2 kB | Without provenance | Details for pyfigtree-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfigtree-1.0.1-cp38-cp38-manylinux1_x86_64.whl | CPython 3.8 | CPython 3.8 | Linux glibc 2.5+ x86-64 | Details for pyfigtree-1.0.1-cp38-cp38-manylinux1_x86_64.whl |
Total release size: 170.3 kB
Release files / pyfigtree-1.0.1.tar.gz
| Download URL | pyfigtree-1.0.1.tar.gz |
|---|---|
| Size | 85.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba40341970be9e6107cce80571b7dbeab27afffb98f9b20c78c8724770f2b7ec
|
|
BLAKE2b-256 checksum How to use checksums |
386fbc213a634ef37be517e2b0ee14d930322c242ee87a8c7e7f2a24a92500d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.16
|
Release files / pyfigtree-1.0.1-cp38-cp38-manylinux1_x86_64.whl
| Download URL | pyfigtree-1.0.1-cp38-cp38-manylinux1_x86_64.whl |
|---|---|
| Size | 85.1 kB |
| Tags | CPython 3.8 Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
0e69ba40eda67c040ebcf95cfc946ead1cb41961cd264aef6e4d0ed20f4698c9
|
|
BLAKE2b-256 checksum How to use checksums |
8eb2c4003d801369586c2486933f1dbb8cdda5410c0491a90db5a8f0c12412ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.16
|
Security for pyfigtree 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.