Giter Club home page Giter Club logo

kdepy's People

Contributors

bz2 avatar gunvor avatar lukedyer-peak avatar rdimaio avatar snowdrop4 avatar syrte avatar tommyod avatar whitews avatar yanbomb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kdepy's Issues

Poor performance on 1D KDE

Have you tried to replicate this Simple 1D Kernel Density Estimation?

Here is my code:

import numpy as np

from scipy import stats
from sklearn.neighbors.kde import KernelDensity
from KDEpy import FFTKDE, NaiveKDE, TreeKDE
import statsmodels.api as sm

import plotly.graph_objs as go
N = 1000
xs = np.linspace(-5, 10, 1000)

m = np.concatenate((np.random.normal(0, 1, int(0.3 * N)), np.random.normal(5, 1, int(0.7 * N))))
true_dens = (0.3 * stats.norm(0, 1).pdf(xs) + 0.7 * stats.norm(5, 1).pdf(xs))
fig = go.FigureWidget()
# True density
fig.add_scatter(x=xs, y=true_dens, 
                mode='lines', fill='tozeroy', fillcolor='lightgray',
                line=dict(color='black', width=2), 
                name='Input Distribution')

# Scipy
kernel = stats.gaussian_kde(m, bw_method='silverman')
fig.add_scatter(x=xs, y=kernel(xs), line=dict(width=1.5), name='Scipy')

# Scikit-Learn
kde = KernelDensity(kernel='gaussian', bandwidth=np.std(m)*(4/(3*len(m)))**(1/5),).fit(m[:,np.newaxis])
log_dens = kde.score_samples(xs[:,np.newaxis])
fig.add_scatter(x=xs, y=np.exp(log_dens), line=dict(width=1.5), name='SK-Learn')

# StatsModels
dens = sm.nonparametric.KDEUnivariate(m)
dens.fit()
fig.add_scatter(x=xs, y=dens.evaluate(xs), line=dict(width=1.5), name='StatsModels')

# KDEpy
x, y = FFTKDE(kernel='gaussian', bw='silverman').fit(m, weights=np.arange(len(m)) + 1)(2**15)
fig.add_scatter(x=x, y=y, line=dict(width=1.5), name='FFTKDE')

# Rugplot
fig.add_scatter(x=m, y=np.zeros(len(m))-0.01, 
                mode='markers', showlegend=False,
                marker=dict(symbol='line-ns-open', color='black')) 

fig.layout.update(xaxis=dict(zeroline=False))
fig

image

It looks like the FFT estimation is not that good.
Am I doing something wrong? Or what are the caveats one should consider when dealing with FFTKDE?

Necessity of FFTKDE grid enclosure with reflected boundaries

When evaluating the PDF with FFTKDE, the grid bounds must exceed the extrema of the data points. In #6, you mention this is due to your implementation and isn't strictly necessary. What would be required to remove this restriction?

The reason is that evaluating the PDF in higher dimensions, which is already prohibitive, becomes even more so if one also wishes to impose boundary reflections.

Let's say I have N-dimensional data on which I want to impose boundary conditions in all axes. The reflected data will be 2N+1 times the size of the original. To construct the PDF within the boundaries with FFTKDE, one must reflect the axes in each dimension before forming the Cartesian product. If each axis has n points, the resulting grid will have (3n-2)^N points, and this size becomes a problem both for memory and evaluation speed. The resolution of each axis has to be lowered which leads to less accurate PDF predictions (this isn't too bad, because the shape remains accurate and the height can be renormalized with a numerical integration).

If FFTKDE could be evaluated on a grid inside the data extrema, one could fit to the 2N+1 reflected data points but still evaluate the PDF on the n^N non-reflected grid points.

invalid syntax: msg = f'Kernel must be a string or callable. Options: {akernels}'

Hi!
Have a problem with importing, I've tried
from KDEpy import FFTKDE
but have an error
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/KDEpy/__init__.py", line 5, in <module> from KDEpy.NaiveKDE import NaiveKDE File "/usr/local/lib/python3.5/dist-packages/KDEpy/NaiveKDE.py", line 11, in <module> from KDEpy.BaseKDE import BaseKDE File "/usr/local/lib/python3.5/dist-packages/KDEpy/BaseKDE.py", line 50 msg = f'Kernel must be a string or callable. Options: {akernels}' ^ SyntaxError: invalid syntax

Tried to install KDEpy on my mac but doesn't work. Error code below

(base) usertekiAir:~ user$ pip install KDEpy
Collecting KDEpy
Using cached https://files.pythonhosted.org/packages/c9/ff/0229a6cb3df501d2c1908b6a1c036389e695d4f888c502191471e3f2003e/KDEpy-1.0.2.tar.gz
Requirement already satisfied: numpy>=1.14.2 in /Applications/anaconda3/lib/python3.7/site-packages (from KDEpy) (1.16.2)
Requirement already satisfied: scipy>=1.0.1 in /Applications/anaconda3/lib/python3.7/site-packages (from KDEpy) (1.2.1)
Requirement already satisfied: matplotlib>=2.2.0 in /Applications/anaconda3/lib/python3.7/site-packages (from KDEpy) (3.0.3)
Requirement already satisfied: cycler>=0.10 in /Applications/anaconda3/lib/python3.7/site-packages (from matplotlib>=2.2.0->KDEpy) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Applications/anaconda3/lib/python3.7/site-packages (from matplotlib>=2.2.0->KDEpy) (1.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Applications/anaconda3/lib/python3.7/site-packages (from matplotlib>=2.2.0->KDEpy) (2.3.1)
Requirement already satisfied: python-dateutil>=2.1 in /Applications/anaconda3/lib/python3.7/site-packages (from matplotlib>=2.2.0->KDEpy) (2.8.0)
Requirement already satisfied: six in /Applications/anaconda3/lib/python3.7/site-packages (from cycler>=0.10->matplotlib>=2.2.0->KDEpy) (1.12.0)
Requirement already satisfied: setuptools in /Applications/anaconda3/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib>=2.2.0->KDEpy) (40.8.0)
Building wheels for collected packages: KDEpy
Building wheel for KDEpy (setup.py) ... error
Complete output from command /Applications/anaconda3/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-install-kx7v8okg/KDEpy/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" bdist_wheel -d /private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-wheel-rffy7dys --python-tag cp37:
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.9-x86_64-3.7
creating build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/binning.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/kernel_funcs.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/init.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/FFTKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/TreeKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/NaiveKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/BaseKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/bw_selection.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/utils.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
running build_ext
skipping 'KDEpy/cutils.c' Cython extension (up-to-date)
building 'cutils' extension
creating build/temp.macosx-10.9-x86_64-3.7
creating build/temp.macosx-10.9-x86_64-3.7/KDEpy
x86_64-apple-darwin13.4.0-clang -DNDEBUG -fwrapv -O3 -Wall -Wstrict-prototypes -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I/Applications/anaconda3/lib/python3.7/site-packages/numpy/core/include -I/Applications/anaconda3/include/python3.7m -c KDEpy/cutils.c -o build/temp.macosx-10.9-x86_64-3.7/KDEpy/cutils.o
In file included from KDEpy/cutils.c:4:
/Applications/anaconda3/include/python3.7m/Python.h:25:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
error: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 1


Failed building wheel for KDEpy
Running setup.py clean for KDEpy
Failed to build KDEpy
Installing collected packages: KDEpy
Running setup.py install for KDEpy ... error
Complete output from command /Applications/anaconda3/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-install-kx7v8okg/KDEpy/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-record-hd16kmky/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build/lib.macosx-10.9-x86_64-3.7
creating build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/binning.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/kernel_funcs.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/init.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/FFTKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/TreeKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/NaiveKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/BaseKDE.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/bw_selection.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
copying KDEpy/utils.py -> build/lib.macosx-10.9-x86_64-3.7/KDEpy
running build_ext
skipping 'KDEpy/cutils.c' Cython extension (up-to-date)
building 'cutils' extension
creating build/temp.macosx-10.9-x86_64-3.7
creating build/temp.macosx-10.9-x86_64-3.7/KDEpy
x86_64-apple-darwin13.4.0-clang -DNDEBUG -fwrapv -O3 -Wall -Wstrict-prototypes -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I/Applications/anaconda3/lib/python3.7/site-packages/numpy/core/include -I/Applications/anaconda3/include/python3.7m -c KDEpy/cutils.c -o build/temp.macosx-10.9-x86_64-3.7/KDEpy/cutils.o
In file included from KDEpy/cutils.c:4:
/Applications/anaconda3/include/python3.7m/Python.h:25:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
error: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 1

----------------------------------------

Command "/Applications/anaconda3/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-install-kx7v8okg/KDEpy/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-record-hd16kmky/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/kh/55sgb3353t36wpt0l6_p1y840000gn/T/pip-install-kx7v8okg/KDEpy/

FFTKDE.evaluate on one single point

Hello,

What is the best way of evaluating FFTKDE on a single point? Do I need to create a grid containing it in order to use the evaluate method?

Thanks in advance!

Absolute path on Windows running pip install

I'm getting the following error on my Windows machine trying to install from pip

   raise ValueError("path '%s' cannot be absolute" % pathname)
    ValueError: path '/home/tommy/Desktop/KDEpy/KDEpy/cutils.pyx' cannot be absolute

I believe the lines

here = path.abspath(path.dirname(__file__))
SRC_DIR = path.join('.', "KDEpy")
ext_1 = Extension("cutils",
                  [path.join(SRC_DIR, "cutils.pyx")],
libraries=[])

in setup.py are the culprit.

Issues when installing KDEpy

When running python setup.py or pip install KDEpy (on python 2.7), get the following error
from KDEpy import version

def init(self, kernel: str, bw: float):
^
SyntaxError: invalid syntax

KDE for 2dim irregular data (not grid)

Hi,
Here is reference given for 2d kernel, but only in case of grid data.
https://kdepy.readthedocs.io/en/latest/kernels.html#higher-dimensional-kernels

I found that if there is random 2d data set is given. KDEpy does not seem to find the consistent kde, as I compared with scipy_gaussian_kde. It also does not satisfy the normalisation condition.

Here chi0 chi1 have random value b/w 0 and 1.
kernel = BaseKDE._available_kernels['gaussian']
values = np.transpose(np.vstack([chi0, chi1]))
z = kernel(values, norm=2)
This z is not normalised.

Can I make function from KDEpy just like scipy_gaussian_kde ?
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
here kernel is function.

Thank you.

TreeKDE doesn't implement variable bandwidths

Hi @tommyod! Thanks for the fast, well-documented package! I'd been searching around for a bit trying to find implementations that allowed for weights AND variable bandwidths, so this was very helpful.

However, while apparently TreeKDE is supposed to be able to handle variable bandwidths, for me just uses the maximum of that list as a fixed bandwidth. I spent ~4 hours trying to figure out why my adaptive KDE's (a la Silverman) were looking exactly like over-smoothed fixed KDE's, and then I realized I should try the NaiveKDE class (which I had been ignoring since from the speed description, the TreeKDE was supposed to be superior) and it worked perfectly.

I just installed KDEpy via pip two days ago, so I don't think there's a version issue, and from looking through past issues I don't think has come up before. I tried deciphering the implementation itself, and what tipped me off to what was happening was that maximal_bw = np.max(self.bw) was being used. Here's some code where I input a list of bandwidths to both NaiveKDE and TreeKDE, where the list is constant except for one, large bandwidth:

import numpy as np
from KDEpy import NaiveKDE, TreeKDE
import matplotlib.pyplot as plt
from scipy.stats import lognorm

fig, axs = plt.subplots(2, 1, figsize=(9, 12))

# get a list of 1000 random variables from the scipy lognormal
# distribution with shape parameter 0.6
rvs = np.sort(lognorm.rvs(0.6, size=1000))
axs[0].plot(rvs, np.repeat(-0.01,len(rvs)), '|r', markersize=5, label='data')
axs[1].plot(rvs, np.repeat(-0.01,len(rvs)), '|r', markersize=5, label='data')

# get pdf of lognormal distribution
x = np.linspace(0, 7, 200)
pdf = lognorm.pdf(0.6, x)
axs[0].plot(x, pdf, 'r', alpha=0.5, label='Actual PDF')
axs[1].plot(x, pdf, 'r', alpha=0.5, label='Actual PDF')

# use a Silverman's long-tailed bandwidth as global_bw
IQR = np.percentile(rvs, q=75) - np.percentile(rvs, q=25)
global_bw = 0.79*IQR*len(rvs)**(-0.2)

# do fixed bandwidth kde's
fixed_naive_kde = NaiveKDE(kernel='gaussian', bw=global_bw).fit(rvs).evaluate(x)
fixed_tree_kde = TreeKDE(kernel='gaussian', bw=global_bw).fit(rvs).evaluate(x)
axs[0].plot(x, fixed_naive_kde, 'g', label='Fixed bandwidth NaiveKDE')
axs[0].plot(x, fixed_tree_kde, ':b', lw=3, label='Fixed bandwidth TreeKDE')

# input an array of bandwidths with only one NOT equal to global_bw
bws = np.repeat(global_bw, len(rvs))
bws[-10] = 10*bws[-10]

var_naive_kde = NaiveKDE(kernel='gaussian', bw=bws).fit(rvs).evaluate(x)
var_tree_kde = TreeKDE(kernel='gaussian', bw=bws).fit(rvs).evaluate(x)
axs[1].plot(x, var_naive_kde, 'g', label='Variable bandwidth NaiveKDE')
axs[1].plot(x, var_tree_kde, 'b', lw=3, label='Variable bandwidth TreeKDE')

# compare to using the max of the variable bandwidths as the fixed bandwidth
max_tree_kde = TreeKDE(kernel='gaussian', bw=np.max(bws)).fit(rvs).evaluate(x)
axs[1].plot(x, max_tree_kde, ':m', lw=3, label='KDE using max')

axs[0].legend()
axs[1].legend()
plt.show()

TreeKDE variable bandwidths

Variable bandwidth for 3 dimensional data.

Hello,

First of all, thanks for the great package.
I'm trying to compute density maps of a 3 dimensional points distribution. I understood from the documentation that a variable bandwith method was available but I couldn't figure out how to set up this option.
Additionnaly, in the case of a fixed bandwidth KDE for multidimensional data, I would have expected as in the stats_models_multivariateKDE implementation to be able to use a bandwidth per dimension but it seems that we can either use a single value of the bandwidth or to use one bandwidth per data point. Is it in order to take into account the weight of each data point that you implemented it this way ?

Thanks in advance.

Cheers
Yoann

Scaling issue with ISJ and custom grid

Hi!

Thanks again for a great library! We've run into an issue where the estimated PDFs are scaled incorrectly for the ISJ method under the following conditions:

  • The data have a narrow standard deviation
  • We provide a wide grid spacing relative to the observed data

This error does not occur with Silverman's bandwidth calculation.

Below I've pasted code for replicating the issue, along with the output. Note how the ISJ approach greatly overestimates the PDF when providing our own evenly-spaced grid points. This does not occur if we narrow the range of the grid points or expand the standard deviation of the random data.

I tried to figure out what might be happening in the source, but I was unable to track down the issue, but it might have something to do with the real_bw calculation giving rise to the L factor. Thus, I'm unable to provide a PR, just an example for replicating the error, which will hopefully be useful in figuring out what's happening.

Thanks!

import numpy as np
from KDEpy import FFTKDE
import matplotlib.pyplot as plt

# set up some params for the example
rmin = -2
rmax = 2
xvals = np.linspace(rmin, rmax, 512)
dat = np.random.normal(loc=0, scale=.05, size=1500)

# calculate with ISJ
isj = FFTKDE(kernel='epa', bw="ISJ").fit(np.array(dat))

# eval with xvals
yval_pdf_ISJ=isj.evaluate(xvals)

# eval with default values
gps, yval_pdf_ISJ_e  = isj.evaluate()

# calc with silverman
silver = FFTKDE(kernel='epa', bw="silverman").fit(np.array(dat))
yval_pdf_silverman = silver.evaluate(xvals)

# plot the data
plt.hist(dat, density=True, bins='auto', alpha=0.5)

# plot the fits
plt.plot(xvals, yval_pdf_ISJ, alpha=0.8, label='ISJ')
plt.plot(gps, yval_pdf_ISJ_e, alpha=0.8, label='ISJe')
plt.plot(xvals, yval_pdf_silverman, alpha=0.8, label='silverman')

# set the range
plt.xlim(-0.5, .5)

# add legend
plt.legend()

image

Defining the limits of the grid

Hi,

I am trying to perform several 2D estimations in the same grid (different groups in the same XY space). In order to compute the KDE I am passing the data for each group. However, given that the limits are not necessarily the same, I can't use the resulting KDE (I want to operate on the KDEs, but the grids are not the same...). Is there a way to fix the limits of the x and y scale? I saw that you can do it for 1D like np.linspace(-5, 5, 128), but when I try to do something similar in 2D it fails:

#Setting for x
grid, points = kde.fit(data).evaluate(np.linspace(-0.5, 20, 128))

#Gives
ValueError: Shape of data and grid points must be the same.

#Trying to set x and y
grid, points = kde.fit(data).evaluate((np.linspace(-0.5, 20, 128),np.linspace(-0.5, 20, 128)))

#Gives
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Code to replicate comparison graphs

Is the code to replicate the graphs on the comparison page available? I wasn't able to find it in the repo. I tried replicating a couple of the graphs on that page, however, I am getting different results. I'm not sure where the discrepancy lies. I have a jupyter notebook comparing kdepy, statsmodels, and sklearn. Perhaps you might see why the comparison graphs differ?

Discrepancy in automated bandwidth selection?

Hello,

I have been working on porting Botev et al. (2010) algorithm for KDE in 1D from MATLAB to Python (original code from the author's page here) since I was very unhappy with the scipy KDE. To double-check, I also looked online for Python implementations of Botev et al., so I found your repo among others.

I just wanted to give you a heads-up about a potential discrepancy in your implementation. In bw_selection.py -> _fixed_point, lines 63 and 79, you have f = (0.5) * np.pi .... However, both in the MATLAB original and in the implementation by Daniel B. Smith, that part is f = (2.0) * np.pi .... Since I see that you commented your code fairly well but did not add a note here to explain the reason for the difference, I just wanted to point it out. As far as I can tell, you don't seem to have changed the multiplicative constant elsewhere to adjust for this.

When I run that snippet, your code seems to work differently from Botev et al. in that the bandwidth you return is substantially larger (by a factor of ~2) than the one returned by Botev's MATLAB implementation (on exactly the same data). However, I might be missing other changes you made in the code. Still, might be worth double-checking.

New machine, now cannot replicate KDEpy KDE's in scientific paper in review.

KDEpy is cited in the paper, but I am extremely alarmed that the identical data and code no longer generate the same plots (originally made in 2019), which is critical for a core interpretation of the paper. Here is an example. It appears to have become over-smoothed/weights are no longer working as they were.

This code generated the first plot on a 2019 environment (and in the paper), now it generates the second more smoothed version. Please help.

data_all = np.array(df['value'])
weights_all = np.array(df['wgt'])
x, y = FFTKDE(kernel='gaussian', bw='ISJ').fit(data_all, weights=weights_all).evaluate()
xb, yb = FFTKDE(kernel='box', bw='ISJ').fit(data_all, weights=weights_all).evaluate()
plt.scatter(data_all, np.zeros_like(data_all), marker='|', color=c_kde)
plt.plot(xb, yb, color=c_kde); plt.tight_layout()
plt.plot(x, y, color=c_kde); plt.tight_layout()
Testold
testnew

Cannot pip install KDEpy on Ubuntu 16.04.1

First things first, thank you for this amazing piece of work :)

As per title, I failed at pip installing KDEpy using pip on Ubuntu 16.04.1. Minimum system info follows.

$ uname -a 
Linux #hostname# 4.15.0-43-generic #46~16.04.1-Ubuntu SMP Fri Dec 7 13:31:08 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ python3 --version
Python 3.5.2

To be sure it isn't something with my system configuration I did the following.

  1. I created a venv
$ mkdir /tmp/throwaway
$ python3 -m venv /tmp/throwaway
$ source /tmp/throwaway/bin/activate
  1. I updated pip and installed wheel
(throwaway) $ pip install --upgrade pip
(throwaway) $ pip install wheel
  1. I tried to pip install KDEpy
(throwaway) $ pip install kdepy

I put what I believe to be the relevant output, please ask for more info and I'd be happy to help!

  creating build/temp.linux-x86_64-3.5/KDEpy
  x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/tmp/throwaway/include -I/usr/include/python3.5m -c KDEpy/cutils.c -o build/temp.linux-x86_64-3.5/KDEpy/cutils.o
  KDEpy/cutils.c:4:20: fatal error: Python.h: No such file or directory
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
  
  ----------------------------------------
  Failed building wheel for kdepy
  Running setup.py clean for kdepy
Failed to build kdepy
Installing collected packages: numpy, scipy, six, cycler, kiwisolver, pyparsing, python-dateutil, matplotlib, kdepy
  Running setup.py install for kdepy ... error
    Complete output from command /tmp/throwaway/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-zk12zrwc/kdepy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-mfnxe_0g/install-record.txt --single-version-externally-managed --compile --install-headers /tmp/throwaway/include/site/python3.5/kdepy:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.5
    creating build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/BaseKDE.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/FFTKDE.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/bw_selection.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/kernel_funcs.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/binning.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/__init__.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/utils.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/TreeKDE.py -> build/lib.linux-x86_64-3.5/KDEpy
    copying KDEpy/NaiveKDE.py -> build/lib.linux-x86_64-3.5/KDEpy
    running build_ext
    building 'cutils' extension
    creating build/temp.linux-x86_64-3.5
    creating build/temp.linux-x86_64-3.5/KDEpy
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/tmp/throwaway/include -I/usr/include/python3.5m -c KDEpy/cutils.c -o build/temp.linux-x86_64-3.5/KDEpy/cutils.o
    KDEpy/cutils.c:4:20: fatal error: Python.h: No such file or directory
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    
    ----------------------------------------
Command "/tmp/throwaway/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-zk12zrwc/kdepy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-mfnxe_0g/install-record.txt --single-version-externally-managed --compile --install-headers /tmp/throwaway/include/site/python3.5/kdepy" failed with error code 1 in /tmp/pip-install-zk12zrwc/kdepy/

Using KDEpy in Global Mapper Pro v23

Hello Tommy,
Global Mapper Pro v23 (currently in beta) will support running Python scripts through its own script editor and I wondered what you thought about possibly using KDEpy with point features loaded into a Global Mapper workspace? Ingestion of point features into KDEpy is one hurdle. The second hurdle would be the vector output from KDEpy; e.g., line and area features.

I realize this may seem like an oddball question from out in left field, but while researching point density visualizations, the illustration of KDEpy's Fast 2D computations using binning and FFT looked exactly what I've been trying to accomplish graphically from raster heat maps.

Ideation
https://drive.google.com/file/d/11m_l8BE6IqbaV8EFCnL_JfLDKWzg06nX/view?usp=sharing

Implement icdf method

Statsmodel's KDEUnivariate has the method to evaluate inverse cumulative distribution function, with the signature icdf(q) where q is the quantile.

It'd be nice to have this in this great library. I'm assuming it should be easy to implement imitating the statsmodel implementation which is here.

Thank you for your consideration!

Create Conda package for conda-forge

Hi @tommyod. First, thanks very much for KDEpy!

I use KDEpy as part of my smoothed particle hydrodynamics analysis and visualisation software Plonk (https://github.com/dmentipl/plonk). I use it for interpolation of particle quantities to a pixel grid, like in SPLASH (http://users.monash.edu.au/~dprice/splash/).

I have a Conda (https://docs.conda.io/en/latest/) package for Plonk on my channel on Anaconda Cloud (https://anaconda.org/dmentipl). I've also created a Conda package for KDEpy on my channel. However, I would like to put Plonk on conda-forge (https://conda-forge.org/). Typically, for a conda-forge package all dependencies are on conda-forge. Since I depend on KDEpy, I was wondering if you could put KDEpy on conda-forge. I think you would have to do this as you are the author and maintainer of KDEpy.

I have written a meta.yaml file for KDEpy; see below. This file is the file conda-build uses to build KDEpy into a Conda package. You can use this to build the Conda package with conda-build, and upload it to Anaconda Cloud. However, for conda-forge, the procedure seems to be:

  1. Fork https://github.com/conda-forge/staged-recipes.
  2. Add your "recipe".
  3. Issue pull request.

I've tested the build following instructions here (https://conda-forge.org/docs/maintainer/adding_pkgs.html#staging-test-locally), and it seems to work.

Let me know what you think and if you have any questions.

Regards,
Daniel

meta.yaml

{% set name = "KDEpy" %}
{% set version = "1.0.2" %}

package:
  name: "{{ name|lower }}"
  version: "{{ version }}"

source:
  url: "https://pypi.io/packages/source/{{ name[0] }}/{{ name }}/{{ name }}-{{ version }}.tar.gz"
  sha256: "6ddeae3f5e20d195b4c60f3416dd8fec08838e16a6f3c8dc857a1fc28f0e58a9"

build:
  number: 0
  skip: True  # [py<35]
  script: "{{ PYTHON }} setup.py install --single-version-externally-managed --record=record.txt"

requirements:
  build:
    - {{ compiler('c') }}
  host:
    - cython
    - matplotlib >=2.2.0
    - numpy >=1.14.2
    - python
    - scipy >=1.0.1
  run:
    - matplotlib >=2.2.0
    - numpy >=1.14.2
    - python
    - scipy >=1.0.1

test:
  imports:
    - KDEpy

about:
  home: "https://github.com/tommyod/KDEpy"
  license: "GNU General Public v3 (GPLv3)"
  license_family: "GPL3"
  summary: "Kernel Density Estimation in Python."

extra:
  recipe-maintainers:
    - tommyod

Installation fails in python 3.9

Here is the output from pip install KDEpy

Collecting KDEpy
Using cached KDEpy-1.0.2.tar.gz (136 kB)
Requirement already satisfied: numpy>=1.14.2 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from KDEpy) (1.19.4)
Requirement already satisfied: scipy>=1.0.1 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from KDEpy) (1.5.4)
Requirement already satisfied: matplotlib>=2.2.0 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from KDEpy) (3.3.3)
Requirement already satisfied: cycler>=0.10 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy) (0.10.0)
Requirement already satisfied: python-dateutil>=2.1 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy) (2.8.1)
Requirement already satisfied: numpy>=1.14.2 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from KDEpy) (1.19.4)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy) (1.3.1)
Requirement already satisfied: pillow>=6.2.0 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy) (8.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy) (2.4.7)
Requirement already satisfied: six in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from cycler>=0.10->matplotlib>=2.2.0->KDEpy) (1.15.0)
Requirement already satisfied: six in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from cycler>=0.10->matplotlib>=2.2.0->KDEpy) (1.15.0)
Requirement already satisfied: numpy>=1.14.2 in /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages (from KDEpy) (1.19.4)
Building wheels for collected packages: KDEpy
Building wheel for KDEpy (setup.py): started
Building wheel for KDEpy (setup.py): finished with status 'error'
ERROR: Command errored out with exit status 1:
command: /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"'; file='"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-_feitbkb
cwd: /tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/
Complete output (156 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.9
creating build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/binning.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/bw_selection.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/kernel_funcs.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/utils.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/BaseKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/FFTKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/NaiveKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/init.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/TreeKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
warning: build_py: byte-compiling is disabled, skipping.

running build_ext
skipping 'KDEpy/cutils.c' Cython extension (up-to-date)
building 'cutils' extension
creating build/temp.linux-x86_64-3.9
creating build/temp.linux-x86_64-3.9/KDEpy
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fPIC -I/home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages/numpy/core/include -I/home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/include -I/usr/include/python3.9 -c KDEpy/cutils.c -o build/temp.linux-x86_64-3.9/KDEpy/cutils.o
KDEpy/cutils.c: In function โ€˜__Pyx_modinit_type_init_codeโ€™:
KDEpy/cutils.c:18644:25: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18644 | __pyx_type___pyx_array.tp_print = 0;
| ^
KDEpy/cutils.c:18649:31: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18649 | __pyx_type___pyx_MemviewEnum.tp_print = 0;
| ^
KDEpy/cutils.c:18664:30: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18664 | __pyx_type___pyx_memoryview.tp_print = 0;
| ^
KDEpy/cutils.c:18677:35: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18677 | __pyx_type___pyx_memoryviewslice.tp_print = 0;
| ^
KDEpy/cutils.c: In function โ€˜__Pyx_ParseOptionalKeywordsโ€™:
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
| ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
| ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
| ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
| ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
| ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject op) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c: In function โ€˜__Pyx_decode_c_stringโ€™:
KDEpy/cutils.c:20396:9: warning: โ€˜PyUnicode_FromUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
20396 | return PyUnicode_FromUnicode(NULL, 0);
| ^~~~~~
In file included from /usr/include/python3.9/unicodeobject.h:1026,
from /usr/include/python3.9/Python.h:97,
from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:551:42: note: declared here
551 | Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject
) PyUnicode_FromUnicode(
| ^~~~~~~~~~~~~~~~~~~~~
error: command '/usr/bin/gcc' failed with exit code 1

ERROR: Failed building wheel for KDEpy
Running setup.py clean for KDEpy
Failed to build KDEpy
Installing collected packages: KDEpy
Running setup.py install for KDEpy: started
Running setup.py install for KDEpy: finished with status 'error'
ERROR: Command errored out with exit status 1:
command: /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"'; file='"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-rx_8z9nx/install-record.txt --single-version-externally-managed --compile --install-headers /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/include/site/python3.9/KDEpy
cwd: /tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/
Complete output (156 lines):
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.9
creating build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/binning.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/bw_selection.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/kernel_funcs.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/utils.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/BaseKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/FFTKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/NaiveKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/init.py -> build/lib.linux-x86_64-3.9/KDEpy
copying KDEpy/TreeKDE.py -> build/lib.linux-x86_64-3.9/KDEpy
warning: build_py: byte-compiling is disabled, skipping.

running build_ext
skipping 'KDEpy/cutils.c' Cython extension (up-to-date)
building 'cutils' extension
creating build/temp.linux-x86_64-3.9
creating build/temp.linux-x86_64-3.9/KDEpy
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fPIC -I/home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/lib/python3.9/site-packages/numpy/core/include -I/home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/include -I/usr/include/python3.9 -c KDEpy/cutils.c -o build/temp.linux-x86_64-3.9/KDEpy/cutils.o
KDEpy/cutils.c: In function โ€˜__Pyx_modinit_type_init_codeโ€™:
KDEpy/cutils.c:18644:25: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18644 |   __pyx_type___pyx_array.tp_print = 0;
      |                         ^
KDEpy/cutils.c:18649:31: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18649 |   __pyx_type___pyx_MemviewEnum.tp_print = 0;
      |                               ^
KDEpy/cutils.c:18664:30: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18664 |   __pyx_type___pyx_memoryview.tp_print = 0;
      |                              ^
KDEpy/cutils.c:18677:35: error: โ€˜PyTypeObjectโ€™ {aka โ€˜struct _typeobjectโ€™} has no member named โ€˜tp_printโ€™
18677 |   __pyx_type___pyx_memoryviewslice.tp_print = 0;
      |                                   ^
KDEpy/cutils.c: In function โ€˜__Pyx_ParseOptionalKeywordsโ€™:
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
  580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
      |                                             ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
  580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
      |                                             ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19320:21: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19320 |                     (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                     ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
  580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
      |                                             ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜PyUnicode_AsUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:580:45: note: declared here
  580 | Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
      |                                             ^~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c:19336:25: warning: โ€˜_PyUnicode_get_wstr_lengthโ€™ is deprecated [-Wdeprecated-declarations]
19336 |                         (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
      |                         ^
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:446:26: note: declared here
  446 | static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
KDEpy/cutils.c: In function โ€˜__Pyx_decode_c_stringโ€™:
KDEpy/cutils.c:20396:9: warning: โ€˜PyUnicode_FromUnicodeโ€™ is deprecated [-Wdeprecated-declarations]
20396 |         return PyUnicode_FromUnicode(NULL, 0);
      |         ^~~~~~
In file included from /usr/include/python3.9/unicodeobject.h:1026,
                 from /usr/include/python3.9/Python.h:97,
                 from KDEpy/cutils.c:4:
/usr/include/python3.9/cpython/unicodeobject.h:551:42: note: declared here
  551 | Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
      |                                          ^~~~~~~~~~~~~~~~~~~~~
error: command '/usr/bin/gcc' failed with exit code 1
----------------------------------------

ERROR: Command errored out with exit status 1: /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"'; file='"'"'/tmp/pip-install-w9uncvgw/kdepy_5ea3cd831da8443687772eb1fb15396c/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-rx_8z9nx/install-record.txt --single-version-externally-managed --compile --install-headers /home/mbr085/.virtualenvs/mergeforest-mvpxmIAj/include/site/python3.9/KDEpy Check the logs for full command output.

How are the grid limits estimated?

I'm trying the following code:

import numpy as np
from KDEpy import FFTKDE

N = 100
dims = 2

xy = np.random.uniform(0., 1., (N, dims))

# Scotts rule
bw = N**(-1. / (dims + 4))

grid = FFTKDE(bw=bw, kernel='gaussian').fit(xy).evaluate(50)

print(grid[0].shape, grid[0].min(), grid[0].max())
print(grid[1].shape, grid[1].min(), grid[1].max())

As I understand it, grid[0] returns the grid, and grid[1] the evaluated grid. But in the example above the limits of grid[0] is way beyond the limits of the data.

Am I understanding something wrong here?

Bandwidth selection for 2d data

Hi,

How can I apply automatic bandwidth selection for 2d data? I tried 'silverman', 'scott', 'ISJ' and they all say "rule is only available for 1D data".

I also think about cross-validation but I don't know what and how to compute some error function for grid search. Any suggestion?

Best,
Jeff

Wrapper over plt.plot for quick plots

This is a suggestion only and might already be implemented or not wanted but here goes.

To create a kdeplot using seaborn I just need the following:

import seaborn as sns
sns.kdeplot(data)

To do the same in KDEpy I first need to instantiate an estimator, fit some data and then plot.

Would be great to be able to do

import KDEpy
KDEpy.plot(data, estimator=FFTKDE)

What do you think?

Can't install KDEpy 1.0.5 on Windows Anaconda environment

I have the latest anaconda environment on my Windows machine.

 conda list anaconda
# packages in environment at C:\ProgramData\Anaconda3\envs\testkisin:
#
# Name                    Version                   Build  Channel
anaconda                  2020.07                  py38_0
anaconda-client           1.7.2                    py38_0
anaconda-navigator        1.9.12                   py38_0
anaconda-project          0.8.4                      py_0

Python is 3.8.3
Conda version 4.8.5
Pip version 20.2.3
Trying to install the latest KDEpy 1.0.5 and got the error :

pip install KDEpy==1.0.5
ERROR: Could not find a version that satisfies the requirement KDEpy==1.0.5 (from versions: 0.1, 0.2, 0.3, 0.4, 0.5, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.5.6, 0.6, 0.6.9, 0.6.10, 0.6.11, 1.0.2, 1.0.3)
ERROR: No matching distribution found for KDEpy==1.0.5

Doing the same in Linux exactly identical environment without any problems.
What can be the problem ? The latest KDEpy is not available for Windows ?

Thank you.

inconsistent behavior when using custom grid

FFTKDE seems to change the grid internally when providing a custom grid in 2d. Results do not seem to match with the results from NaiveKDE or TreeKDE. See below for an example

# imports
import numpy as np
import matplotlib.pyplot as plt
import KDEpy
%matplotlib inline

# Create bimodal 2D data
data = np.vstack((np.random.randn(2**8, 2), 
                  np.random.randn(2**8, 2) + (0, 5)))

# Create 2D grid
grid_size = 20
min_X = np.min(data, axis=0)
max_X = np.max(data, axis=0)
grid_margins = tuple(np.linspace(mn, mx, grid_size)
                     for mn, mx in zip(min_X, max_X))
grid = np.stack(np.meshgrid(*grid_margins), -1).reshape(-1, len(grid_margins))

# density estimates
y1 = KDEpy.FFTKDE(bw=0.2).fit(data).evaluate(grid)
y2 = KDEpy.TreeKDE(bw=0.2).fit(data).evaluate(grid)
y3 = KDEpy.NaiveKDE(bw=0.2).fit(data).evaluate(grid)

# plots
fig, axes = plt.subplots(2, 2,  figsize = (10, 10))
axes[0, 0].set_title('Data')
axes[0, 0].scatter(data[:, 0], data[:, 1])
axes[0, 0].set_title('FFTKDE')
axes[0, 1].scatter(grid[:, 0], grid[:, 1], c=y1)
axes[0, 0].set_title('TreeKDE')
axes[1, 0].scatter(grid[:, 0], grid[:, 1], c=y2)
axes[0, 0].set_title('Naive')
axes[1, 1].scatter(grid[:, 0], grid[:, 1], c=y3)

name 'norm' is not defined

Hi, when I ran the example code 'from KDEpy import FFTKDE
data = norm(loc=0, scale=1).rvs(2**3)
estimator = FFTKDE(kernel='gaussian', bw='silverman')
x, y = estimator.fit(data, weights=None).evaluate()
plt.plot(x, y, label='KDE estimate')' after installation, it says 'NameError: name 'norm' is not defined'. How should I deal with that? Thank you!

Constant values

What happens if data = [1, 1, 1, 1, 1, ...]?

Perhaps set true_bw = max(true_bw, epsilon).

Anisotropic KDEs via PCA

Currently, the bandwidth in d dimensions is bw * np.eye(d)---the covariance matrix is a multiple of the identity. As a result, the KDE works best if anisotropic data is shifted, rotatated and scaled before sent into the algorithm.

Having a routine for this built into the library would be nice.

@blasern suggested implementing anisotropic KDE. We agree that the following might work:

  • Shift data to have mean zero. Call this transformation f.
  • Apply the whitening transformation using the PCA / SVD. Call this g.
  • Fit a KDE.
  • Transform back using f^{-1}(g^{-1}(D)), make sure to scale the volume to preserve an integral of unity (determinant of the transformation).
                KDE                       
     Data  -------------> KDE estimate    
                                          
        |                  |              
  g o f |                  |   f^-1 o g^-1
        |                  |              
        v                  v              
           ------------->                 
 Transformed    KDE      KDE estimate     
    Data                                  

The above would implement very general KDEs in arbitrary dimensions.

Where can I find an example for n- dimensional kernel regression using FFTKDE??I tried the below 3-d multiple regression but having difficulty interpreting the output.

#The data to be analysed is of shape 5063 dataframe
from KDEpy import FFTKDE
crim=np.array(crim)
rm=np.array(rm)
medv=np.array(medv)
#The data to be analysed is of shape 506
3 dataframe
weights=np.vstack([crim,rm,medv])
print(weights.shape)

grid1,points1 = FFTKDE(bw=0.5).fit(weights.T).evaluate()
x1, y1 = np.unique(grid1[:, 0]), np.unique(grid1[:, 1])
print(len(x1))
z = points1.reshape(100, 10)
y_pred = np.sum((z.T / np.sum(z, axis=1)).T * y1 , axis=1)
print(y_pred)

Numpy 1.20 DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`.

Hello,

Using numpy 1.20 seeing a DeprecationWarning traced to https://github.com/tommyod/KDEpy/blob/master/KDEpy/binning.py#L79

Probably a simple change needed to change np.float to np.float64 or float, as noted in the warning.

Thanks for this package.

Stacktrace:

/Users/delaurentis/miniconda3/envs/dra/lib/python3.7/site-packages/KDEpy/binning.py:79: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  data = np.asarray_chkfinite(data, dtype=np.float)

Possible typo in docs regarding complexity of FFTKDE

Hi,

In FFTKDE's docs (https://kdepy.readthedocs.io/en/latest/API.html#KDEpy.FFTKDE.FFTKDE), the time complexity for the first phase is given as O(N*(2^d)) and for the second phase as (Onlogn); the total running time is then given as O(N(d^2)+nlogn). I am not sure if the N(d^2) in the total running time is a typo (d^2 instead of 2^d) or if I am just misunderstanding something. Thank you again for implementing this, by the way! It's extremely useful ๐Ÿ˜„

Problems with fitting a KDE to a sine curve

Hello, as a small test to visualize the important of bandwidth in a KDE I plotted a sine curve until 2*pi and create 500 data points around the sine curve with some noise. However, when I try to do the KDE it gives me a very strange result which does not resemble a sine curve at all. Below is all the code the first part generates the sine graph with a scatter plot. I used the example 'Boundary correction using mirroring' without it it still gives a very weird result. The end result is the graph in the last picture which in theory should closely resemble a sine curve.

I am clearly doing something wrong here but I cannot figure out what perhaps I am entirely misunderstanding what the FFTKDE function does and not using it for its intended purpose.
image
image
image

Integration with sklearn

Hi

Do you plan to add support for integration with sklearn, to use e.g. cross-validation for bandwidth selection? Would be great to do something like this with KDEpy.

This would need the estimator.get_params(), etc. functions required from sklearn.

Ideally there would also be a estimator.score() function.

Problem in understanding

I'm trying to find the kernel density of 2D Data points but when I'm trying to sum up all the points probability it's not getting sum up to 1 or even close. Sometimes it is going above 1 too. Can you please explain this?

Why I get so big difference?

import numpy
from scipy import stats
import matplotlib.pyplot as plt
import KDEpy

a = numpy.sort([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.7, 1])
x, y = KDEpy.FFTKDE().fit(a).evaluate(len(a))
plt.plot(x, y)

image

plt.plot(a, stats.gaussian_kde(a).evaluate(a))

image

Evaluation of |f^l|^2

Hello,

As a part of a simulation study I want to perform to compare KDE estimators I am also implementing the Improved Sheather-Jones method. I found your package and it is useful to compare what I did to what is actually implemented.

If we see the formula presented by Botev et. al for the computation of |f^l|^2, the constant multiplying \pi^{2j} is 0.5 and not 2.

In his code, he also uses 2, instead of 0.5, but I think this may be a mistake.

Have you noted this issue?

`
# Fast evaluation of |f^l|^2 using the DCT, see Plancherel theorem

f = (
    2
    * np.pi ** (2 * ell)
    * np.sum(np.power(I_sq, ell) * a2 * np.exp(-I_sq * np.pi ** 2 * t))
)

`

macOS brew python3.9 installation error

Command

python3 -m pip install kdepy

Error

Collecting kdepy
  Using cached KDEpy-1.0.2.tar.gz (136 kB)
Requirement already satisfied: numpy>=1.14.2 in /usr/local/lib/python3.9/site-packages (from kdepy) (1.20.2)
Requirement already satisfied: scipy>=1.0.1 in /usr/local/lib/python3.9/site-packages (from kdepy) (1.6.2)
Requirement already satisfied: matplotlib>=2.2.0 in /usr/local/lib/python3.9/site-packages (from kdepy) (3.4.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.9/site-packages (from matplotlib>=2.2.0->kdepy) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.9/site-packages (from matplotlib>=2.2.0->kdepy) (1.3.1)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.9/site-packages (from matplotlib>=2.2.0->kdepy) (8.2.0)
Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.9/site-packages (from matplotlib>=2.2.0->kdepy) (2.4.7)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.9/site-packages (from matplotlib>=2.2.0->kdepy) (2.8.1)
Requirement already satisfied: six in /usr/local/lib/python3.9/site-packages (from cycler>=0.10->matplotlib>=2.2.0->kdepy) (1.15.0)
Building wheels for collected packages: kdepy
  Building wheel for kdepy (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"'; __file__='"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-wheel-bh3ixylx
       cwd: /private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/
  Complete output (211 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-11-x86_64-3.9
  creating build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/binning.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/kernel_funcs.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/__init__.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/FFTKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/TreeKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/NaiveKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/BaseKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/bw_selection.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  copying KDEpy/utils.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
  running build_ext
  building 'cutils' extension
  creating build/temp.macosx-11-x86_64-3.9
  creating build/temp.macosx-11-x86_64-3.9/KDEpy
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -I/usr/local/opt/[email protected]/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c KDEpy/cutils.c -o build/temp.macosx-11-x86_64-3.9/KDEpy/cutils.o
  KDEpy/cutils.c:18644:26: error: no member named 'tp_print' in 'struct _typeobject'
    __pyx_type___pyx_array.tp_print = 0;
    ~~~~~~~~~~~~~~~~~~~~~~ ^
  KDEpy/cutils.c:18649:32: error: no member named 'tp_print' in 'struct _typeobject'
    __pyx_type___pyx_MemviewEnum.tp_print = 0;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
  KDEpy/cutils.c:18664:31: error: no member named 'tp_print' in 'struct _typeobject'
    __pyx_type___pyx_memoryview.tp_print = 0;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
  KDEpy/cutils.c:18677:36: error: no member named 'tp_print' in 'struct _typeobject'
    __pyx_type___pyx_memoryviewslice.tp_print = 0;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
  KDEpy/cutils.c:19320:22: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                       ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
        PyUnicode_WSTR_LENGTH(op) :                    \
        ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19320:22: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                       ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
        ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
               ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19320:22: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                       ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
         PyUnicode_WSTR_LENGTH(op)))
         ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19320:52: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                     ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
        PyUnicode_WSTR_LENGTH(op) :                    \
        ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19320:52: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                     ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
        ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
               ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19320:52: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                      (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                     ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
         PyUnicode_WSTR_LENGTH(op)))
         ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:26: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                           ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
        PyUnicode_WSTR_LENGTH(op) :                    \
        ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:26: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                           ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
        ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
               ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:26: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                           ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
         PyUnicode_WSTR_LENGTH(op)))
         ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:59: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                            ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
        PyUnicode_WSTR_LENGTH(op) :                    \
        ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:59: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                            ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
        ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
               ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:19336:59: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                          (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                            ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
         PyUnicode_WSTR_LENGTH(op)))
         ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
  #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                    ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3)
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  KDEpy/cutils.c:20396:16: warning: 'PyUnicode_FromUnicode' is deprecated [-Wdeprecated-declarations]
          return PyUnicode_FromUnicode(NULL, 0);
                 ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:551:1: note: 'PyUnicode_FromUnicode' has been explicitly marked deprecated here
  Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
  ^
  /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  13 warnings and 4 errors generated.
  error: command '/usr/bin/clang' failed with exit code 1
  ----------------------------------------
  ERROR: Failed building wheel for kdepy
  Running setup.py clean for kdepy
Failed to build kdepy
Installing collected packages: kdepy
    Running setup.py install for kdepy ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"'; __file__='"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-record-oe8ofcx_/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/kdepy
         cwd: /private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/
    Complete output (211 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-11-x86_64-3.9
    creating build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/binning.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/kernel_funcs.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/__init__.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/FFTKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/TreeKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/NaiveKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/BaseKDE.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/bw_selection.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    copying KDEpy/utils.py -> build/lib.macosx-11-x86_64-3.9/KDEpy
    running build_ext
    building 'cutils' extension
    creating build/temp.macosx-11-x86_64-3.9
    creating build/temp.macosx-11-x86_64-3.9/KDEpy
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -I/usr/local/opt/[email protected]/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c KDEpy/cutils.c -o build/temp.macosx-11-x86_64-3.9/KDEpy/cutils.o
    KDEpy/cutils.c:18644:26: error: no member named 'tp_print' in 'struct _typeobject'
      __pyx_type___pyx_array.tp_print = 0;
      ~~~~~~~~~~~~~~~~~~~~~~ ^
    KDEpy/cutils.c:18649:32: error: no member named 'tp_print' in 'struct _typeobject'
      __pyx_type___pyx_MemviewEnum.tp_print = 0;
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    KDEpy/cutils.c:18664:31: error: no member named 'tp_print' in 'struct _typeobject'
      __pyx_type___pyx_memoryview.tp_print = 0;
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    KDEpy/cutils.c:18677:36: error: no member named 'tp_print' in 'struct _typeobject'
      __pyx_type___pyx_memoryviewslice.tp_print = 0;
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    KDEpy/cutils.c:19320:22: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                         ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
          PyUnicode_WSTR_LENGTH(op) :                    \
          ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19320:22: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                         ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
          ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
                 ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19320:22: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                         ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
           PyUnicode_WSTR_LENGTH(op)))
           ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19320:52: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                       ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
          PyUnicode_WSTR_LENGTH(op) :                    \
          ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19320:52: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                       ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
          ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
                 ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19320:52: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                        (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                       ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
           PyUnicode_WSTR_LENGTH(op)))
           ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:26: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                             ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
          PyUnicode_WSTR_LENGTH(op) :                    \
          ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:26: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                             ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
          ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
                 ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:26: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                             ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
           PyUnicode_WSTR_LENGTH(op)))
           ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:59: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                              ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:261:7: note: expanded from macro 'PyUnicode_GET_SIZE'
          PyUnicode_WSTR_LENGTH(op) :                    \
          ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:59: warning: 'PyUnicode_AsUnicode' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                              ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:262:14: note: expanded from macro 'PyUnicode_GET_SIZE'
          ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
                 ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:580:1: note: 'PyUnicode_AsUnicode' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:19336:59: warning: '_PyUnicode_get_wstr_length' is deprecated [-Wdeprecated-declarations]
                            (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                                                              ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:264:8: note: expanded from macro 'PyUnicode_GET_SIZE'
           PyUnicode_WSTR_LENGTH(op)))
           ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:451:35: note: expanded from macro 'PyUnicode_WSTR_LENGTH'
    #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
                                      ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:445:1: note: '_PyUnicode_get_wstr_length' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3)
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    KDEpy/cutils.c:20396:16: warning: 'PyUnicode_FromUnicode' is deprecated [-Wdeprecated-declarations]
            return PyUnicode_FromUnicode(NULL, 0);
                   ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/cpython/unicodeobject.h:551:1: note: 'PyUnicode_FromUnicode' has been explicitly marked deprecated here
    Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
    ^
    /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyport.h:508:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    13 warnings and 4 errors generated.
    error: command '/usr/bin/clang' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"'; __file__='"'"'/private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-install-h14gosat/kdepy_b8bb737771484eb787a46da0748c78fa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/8m/l8lb7bqd1bd0c6njv30t2zdc0000gn/T/pip-record-oe8ofcx_/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/kdepy Check the logs for full command output.

GPU acceleration with CuPy

Just leaving a note since its nice to share interest. My use-case is performing KDE many times on large (10 million) sets of data points.

I thought I would have a play and see if it would be low-hanging fruit to add support for CuPy to KDEpy. CuPy is a GPU library with NumPy-like syntax. One of the really nice features is that (when supported), NumPy functions will automatically use the CuPy equivalent when applied to a CuPy array. GPUs are ridiculously fast at calculating FFTs compared to NumPy, so I thought it might be nice to take the speedup provided by KDEpy even further. From what I can tell from the docstring of FFTKDE, the FFT (and not the linear binning) is the bottleneck.

After playing around a bit, I realised that the cutils code is a hard dependency, but also that you've written a (slower) numpy function. I'm a bit surprised that there don't exist faster numpy (and hence CuPy) binning implementations - perhaps this would be an idea to look out for? Do I understand correctly that the binning algorithm you're using is bilinear binning?

(btw, I kept getting gcc: error: KDEpy/cutils.c: No such file or directory (full error here) when trying to developer install it with pip on Ubuntu)

Just thought I'd bring the topic up, as I thought your library was cool! :)

Negative weights and normalization

Dear all,

I need to compute a kernel moving average smoothing of some data. That is, given a list of N data d_n measured at the locations x_n, I want to evaluate

           \sum_{n=1}^N K(x - x_n, h) d_n
    f(x) = ------------------------------ ,
             \sum_{n=1}^N K(x - x_n, h)

in a set of grid-points x. To this purpose, I thought I might use KDEpy twice, once for the numerator and once for the denominator, using for the former the d_n as weights. I have however two main issues:

  • For the numerator I need to be able to use negative weights, because in principle the data d_n might be negative. I can easily fix this by setting w_n = d_n + min(d_n). However, looking at the code, this does not seem to be really necessary. Can you please confirm?
  • The code forces the weights to be normalized to unity. I understand this is a necessary step to ensure the final result of KDE is really a PDF. In my case, though, this produces the unwanted effect to make the results wrong by a scaling factor. Again, I can easily fix this by multiplying back the result by sum(w_n), but perhaps you might consider adding a keyword to prevent the forced normalization for uses similar to mine.

Best regards,
Marco

Memory corruption when using custom grid

Hi!

An first of all, thank you for this great package, both the performance and ease-of-use are amazing.

However, I'm currently encountering an issue that leaves me scratching my head:
Sometimes, using a custom grid results in memory corruption, crashing the interpreter (or the IPython kernel in my case).

As far as I can tell, the the issues lies in the Cython code, as I don't think other parts of the code can cause memory corruption.

Unfortunately, I don't have much experience with Cython to do the debugging myself, and furthermore I could not identify a pattern when the error occurs.

Nevertheless, I have a concrete snippet that crashes every time with the message double free or corruption (out):

import json
import numpy as np
from KDEpy import FFTKDE

with open('data.json') as f:
    data = json.load(f)

kde = FFTKDE().fit(data)

# Evaluating on automatic grid works
kde.evaluate()

# Evaluating on custom grid causes memory corruption
grid = np.linspace(0, 677, 678)
density = kde.evaluate(grid)

And here's the data file: data.zip

(GitHub would not let me upload it without zipping it first)

If you need any more information, please let me know.

Thank you very much in advance!

The output has different dimensiones to the input data

I would like to determine the KDE for each point of my data. However, the output produced by FFTKDE has always 1024 rows, in contrast to other implementations (Eg. the gaussian_kde from scipy). Is there a way to produce similar results as the gaussian_kde implementation?

An example code:

from KDEpy import FFTKDE
from scipy.stats import gaussian_kde

# Data
customer_ages = [40, 56, 20, 35, 27, 24, 29, 37, 39, 46]
customer_income = [152, 64, 24, 140, 88, 64, 103, 148, 150, 132]
x = customer_ages
y = customer_income

# Scipy gaussian_kde
xy = np.vstack([customer_ages,customer_income])
out = gaussian_kde(xy)(xy)
print('Scipy: gaussian_kde\n-------------------')
print('1 output, with shape ' + str(out.shape))
print(out)

# KDEpy FFTKDE
customer_2d = np.array([customer_ages, customer_income]).transpose()
out = FFTKDE(kernel="gaussian").fit(customer_2d).evaluate()
print('\nKDEpy: FFTKDE\n-------------')
print('2 output, x' + str(out[0].shape) + ' and y' + str(out[1].shape))
print(out[0])
print(out[1])

And the output:

Scipy: gaussian_kde
-------------------
1 output, with shape (10,)
[3.35135483e-04 7.71774462e-05 1.21084195e-04 3.46854720e-04
 2.49810866e-04 2.06022906e-04 2.74664480e-04 3.47653843e-04
 3.45484771e-04 1.81037600e-04]

KDEpy: FFTKDE
-------------
2 output, x(1024, 2) and y(1024,)
[[ 15.92684001  17.6       ]
 [ 15.92684001  22.14193548]
 [ 15.92684001  26.68387097]
 ...
 [ 60.07315999 149.31612903]
 [ 60.07315999 153.85806452]
 [ 60.07315999 158.4       ]]
[2.22044605e-16 2.27673700e-05 1.57620254e-05 ... 2.22044605e-16
 2.22044605e-16 2.22044605e-16]

FFTKDE on a fix (equidistant) grid

I was wondering if there was a way to evaluate FFTKDE on a fix grid. For example, to evaluate the kernel on a grid of 256 points between -2 and 2. And the same thing for more thant 1 dimension, ex. 256 points in x0 between -2 and 2 and 256 between -1 and 1 in x1.

Bandwidth not recalculated with multiple calls to fit

We recently ran into an issue when setting up an FFTKDE object once and calling fit more than once on the same object with different data. It seems as though the bw is not recalculated after the first call to fit because it is set to the self.bw, so that it isn't overwritten. This can give rise to some serious errors in the density estimates.

Here is some code that illustrates the issue:

from KDEpy import FFTKDE
import matplotlib.pyplot as plt
import numpy as np

# set up plotting range and generate some data
xrng = np.linspace(-100, 100, 2048)
x = np.random.randn(1000)
xs = x*25

# set up the KDE
kernel = 'epa'
bw = 'ISJ'
kde = FFTKDE(kernel=kernel, bw=bw)

# do an initial fit on non-scaled data
p = kde.fit(x).evaluate(xrng)

# do another with a different scale and plot the results
p = kde.fit(xs).evaluate(xrng)
plt.plot(xrng, p)

# plot what the correct answer should be
p2 = FFTKDE(kernel=kernel, bw=bw).fit(xs).evaluate(xrng)
plt.plot(xrng, p2)

image

It seems to me that a fix in BaseKDE would correct this if self.bw wasn't overwritten with the value the first call to fit.

I hope this example helps track down the issue. I think that the behavior I demonstrate above is not what you'd want to do (I've worked around it my code by recreating the FFTKDE object each time), but please let me know if I'm incorrect.

Thanks for a great library!
Per

Memory error for large arrays

Hi,

First: thanks you for the great package, it works really well! However I experience something that I don't quite understand. I'm fitting a pretty large 4D KDE and it all works fine if a evaluate on a grid of the size

nbins_x, nbins_y, nbins_z, nbins_z2 = 200, 30, 80, 500

but once I increase the resolution a bit to let's say

nbins_x, nbins_y, nbins_z, nbins_z2 = 200, 40, 80, 500

I get a memory error. I have also profiled the memory consumption in the first and second case

1.)

5565.734 MiB 3628.438 MiB   bins1 = np.array(list(itertools.product(bins_Et, bins_sind, bins_Er, bins_dang)))
14721.020 MiB 9155.285 MiB    y = KDE3d.evaluate(bins1)

2.)

6776.355 MiB 4849.047 MiB   bins1 = np.array(list(itertools.product(bins_Et, bins_sind, bins_Er, bins_dang))) 
53828.105 MiB 47051.750 MiB       y = KDE3d.evaluate(bins1)

As you see the memory consumption of the evaluate function suddenly jumps up from 9GB to 47GB without any obvious reason. I unfortunately currently don't have time to investigate this in more detail. But you might have an idea?! If it helps: I experience the same behavior on different computers.

Here the traceback:

  File "KDEs_new.py", line 197, in main
    y = KDE3d.evaluate(bins1)
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/KDEpy/FFTKDE.py", line 200, in evaluate
    ans = convolve(data, kernel_weights, mode='same').reshape(-1, 1)
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/scipy/signal/signaltools.py", line 802, in convolve
    out = fftconvolve(volume, kernel, mode=mode)
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/scipy/signal/signaltools.py", line 415, in fftconvolve
    ret = np.fft.irfftn(sp1 * sp2, fshape, axes=axes)[fslice].copy()
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/numpy/fft/fftpack.py", line 1232, in irfftn
    a = irfft(a, s[-1], axes[-1], norm)
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/numpy/fft/fftpack.py", line 466, in irfft
    _real_fft_cache)
  File "/home/ga53lag/Software/pyv3/lib/python3.7/site-packages/numpy/fft/fftpack.py", line 83, in _raw_fft
    r = work_function(a, wsave)
MemoryError

Thanks a lot,
Theo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.