Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving NaiveKDE-objects with pickle #113

Open
sbieringer opened this issue Sep 14, 2022 · 1 comment
Open

Saving NaiveKDE-objects with pickle #113

sbieringer opened this issue Sep 14, 2022 · 1 comment

Comments

@sbieringer
Copy link

Hi all,

Thanks for the package, it has been a great help! I am using version 1.1.0.
Whenever I try to save a NaiveKDE object with pickle, e.g.

import KDEpy
import pickle
import numpy as np

x = np.random.randn(1000)
kde = KDEpy.NaiveKDE('gaussian')
kde.fit(x)

with open('./kde.pt', 'wb')as f:
    pickle.dump(kde, f)

I get an error of the sort

      7 kde.fit(x)
      9 with open('./kde.pt', 'wb')as f:
---> 10     pickle.dump(kde, f)

PicklingError: Can't pickle <function gaussian at 0x7f517bca1430>: it's not the same object as KDEpy.kernel_funcs.gaussian

This could be easily resolved by changing lines 328-338 to names not coinciding with the function names, e.g.

gaussian_k = Kernel(gaussian, var=1, support=np.inf)
exp_k = Kernel(exponential, var=2, support=np.inf)
box_k = Kernel(box, var=1 / 3, support=1)
tri_k = Kernel(tri, var=1 / 6, support=1)
epa_k = Kernel(epanechnikov, var=1 / 5, support=1)
biweight_k = Kernel(biweight, var=1 / 7, support=1)
triweight_k = Kernel(triweight, var=1 / 9, support=1)
tricube_k = Kernel(tricube, var=35 / 243, support=1)
cosine_k = Kernel(cosine, var=(1 - (8 / np.pi**2)), support=1)
logistic_k = Kernel(logistic, var=(np.pi**2 / 3), support=np.inf)
sigmoid_k = Kernel(sigmoid, var=(np.pi**2 / 4), support=np.inf)

_kernel_functions = {
    "gaussian": gaussian_k,
    "exponential": exp_k,
    "box": box_k,
    "tri": tri_k,
    "epa": epa_k,
    "biweight": biweight_k,
    "triweight": triweight_k,
    "tricube": tricube_k,
    "cosine": cosine_k,
    # 'logistic': logistic_k,
    # 'sigmoid': sigmoid_k
}
@tommyod
Copy link
Owner

tommyod commented Sep 14, 2022

Feel free to add a test and open a pull request on this :) I would merge it if it's as easy as you say!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants