kern-kde 0.2.1a1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wojtasauce
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ recursive-include kern *.h
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: kern_kde
3
+ Version: 0.2.1a1
4
+ Summary: kernel density estimation backed by a C extension
5
+ Home-page: https://github.com/WojtekGrbs/kern
6
+ Author: Wojciech Grabias
7
+ License: MIT
8
+ Project-URL: Documentation, https://wojtekgrbs.github.io/kern/
9
+ Project-URL: Source, https://github.com/WojtekGrbs/kern
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: C
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Topic :: Scientific/Engineering
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy>=1.23
26
+ Provides-Extra: docs
27
+ Requires-Dist: sphinx>=7; extra == "docs"
28
+ Requires-Dist: furo>=2024.1.29; extra == "docs"
29
+ Provides-Extra: test
30
+ Requires-Dist: pytest>=8; extra == "test"
31
+ Dynamic: author
32
+ Dynamic: classifier
33
+ Dynamic: description
34
+ Dynamic: description-content-type
35
+ Dynamic: home-page
36
+ Dynamic: license
37
+ Dynamic: license-file
38
+ Dynamic: project-url
39
+ Dynamic: provides-extra
40
+ Dynamic: requires-dist
41
+ Dynamic: requires-python
42
+ Dynamic: summary
43
+
44
+ # kern
45
+
46
+ `kern` is a minimalistic kernel density estimation package backed by optimized C
47
+ implementations. It provides exact, approximate, bounded, and multivariate KDE
48
+ estimators with a scikit-learn-like API.
49
+
50
+ ## Installation
51
+
52
+ ```console
53
+ python -m pip install kern_kde
54
+ ```
55
+
56
+ ## Example usage
57
+ ```python
58
+ from kern import KernelDensity
59
+
60
+ model = KernelDensity(bandwidth=0.3).fit([0.0, 0.2, 1.0])
61
+ density = model.evaluate([0.1, 0.5])
62
+ ```
63
+
64
+ See the [documentation](https://wojtekgrbs.github.io/kern/) for the user guide,
65
+ API reference, and source-build options.
@@ -0,0 +1,22 @@
1
+ # kern
2
+
3
+ `kern` is a minimalistic kernel density estimation package backed by optimized C
4
+ implementations. It provides exact, approximate, bounded, and multivariate KDE
5
+ estimators with a scikit-learn-like API.
6
+
7
+ ## Installation
8
+
9
+ ```console
10
+ python -m pip install kern_kde
11
+ ```
12
+
13
+ ## Example usage
14
+ ```python
15
+ from kern import KernelDensity
16
+
17
+ model = KernelDensity(bandwidth=0.3).fit([0.0, 0.2, 1.0])
18
+ density = model.evaluate([0.1, 0.5])
19
+ ```
20
+
21
+ See the [documentation](https://wojtekgrbs.github.io/kern/) for the user guide,
22
+ API reference, and source-build options.
@@ -0,0 +1,49 @@
1
+ from .estimators import (
2
+ ApproximateKernelDensity,
3
+ BandwidthSelector,
4
+ BoundedKernelDensity,
5
+ KernelDensity,
6
+ MultivariateKernelDensity,
7
+ default_bandwidth_grid,
8
+ )
9
+ from ._fun import (
10
+ bandwidth_grid,
11
+ bandwidth_kfold,
12
+ bandwidth_loo,
13
+ kde,
14
+ kde_approx,
15
+ kde_approx_self,
16
+ kde_beta_ext,
17
+ kde_beta_self,
18
+ kde_multivariate,
19
+ kde_multivariate_self,
20
+ kde_reflected_ext,
21
+ kde_reflected_self,
22
+ kde_self,
23
+ kernel_default_cutoff,
24
+ kernel_is_symmetric,
25
+ )
26
+
27
+ __all__ = [
28
+ "ApproximateKernelDensity",
29
+ "BandwidthSelector",
30
+ "BoundedKernelDensity",
31
+ "KernelDensity",
32
+ "MultivariateKernelDensity",
33
+ "bandwidth_grid",
34
+ "bandwidth_kfold",
35
+ "bandwidth_loo",
36
+ "default_bandwidth_grid",
37
+ "kde",
38
+ "kde_approx",
39
+ "kde_approx_self",
40
+ "kde_beta_ext",
41
+ "kde_beta_self",
42
+ "kde_multivariate",
43
+ "kde_multivariate_self",
44
+ "kde_reflected_ext",
45
+ "kde_reflected_self",
46
+ "kde_self",
47
+ "kernel_default_cutoff",
48
+ "kernel_is_symmetric",
49
+ ]
@@ -0,0 +1,294 @@
1
+ """Python API for :mod:`kern._core`."""
2
+
3
+ import operator
4
+
5
+ import numpy as np
6
+
7
+ from . import _core
8
+
9
+
10
+ KERNELS = ("gaussian", "epanechnikov", "triangular", "uniform", "cosine")
11
+ PARALLEL_MODES = ("auto", "grid", "evaluation")
12
+ MEMORY_MODES = ("low", "high", "auto")
13
+
14
+ _CORE_PARALLEL_MODES = {
15
+ "auto": "auto",
16
+ "grid": "grid",
17
+ "evaluation": "evaluation",
18
+ "bandwidths": "grid",
19
+ "kernels": "evaluation",
20
+ }
21
+ _MEMORY_MODE_IDS = {"low": 0, "high": 1, "auto": 2}
22
+
23
+
24
+ def _array_1d(values, name, *, nonempty=True):
25
+ try:
26
+ array = np.asarray(values, dtype=np.float64)
27
+ except (TypeError, ValueError) as error:
28
+ raise ValueError(f"{name} must be convertible to float64") from error
29
+ if array.ndim != 1:
30
+ raise ValueError(f"{name} must be one-dimensional")
31
+ if nonempty and array.size == 0:
32
+ raise ValueError(f"{name} must not be empty")
33
+ if not np.all(np.isfinite(array)):
34
+ raise ValueError(f"{name} must contain only finite values")
35
+ return np.ascontiguousarray(array)
36
+
37
+
38
+ def _array_2d(values, name, *, nonempty=True):
39
+ try:
40
+ array = np.asarray(values, dtype=np.float64)
41
+ except (TypeError, ValueError) as error:
42
+ raise ValueError(f"{name} must be convertible to float64") from error
43
+ if array.ndim != 2:
44
+ raise ValueError(f"{name} must be two-dimensional")
45
+ if array.shape[1] == 0 or (nonempty and array.shape[0] == 0):
46
+ raise ValueError(f"{name} must not be empty")
47
+ if not np.all(np.isfinite(array)):
48
+ raise ValueError(f"{name} must contain only finite values")
49
+ return np.ascontiguousarray(array)
50
+
51
+
52
+ def _bandwidth(value):
53
+ try:
54
+ value = float(value)
55
+ except (TypeError, ValueError) as error:
56
+ raise ValueError("bandwidth must be positive") from error
57
+ if not np.isfinite(value) or value <= 0.0:
58
+ raise ValueError("bandwidth must be positive")
59
+ return value
60
+
61
+
62
+ def _bandwidth_grid(values):
63
+ grid = _array_1d(values, "h_grid")
64
+ if np.any(grid <= 0.0):
65
+ raise ValueError("all bandwidths must be positive")
66
+ return grid
67
+
68
+
69
+ def _kernel(value):
70
+ if value not in KERNELS:
71
+ raise ValueError(f"kernel must be one of {KERNELS}")
72
+ return value
73
+
74
+
75
+ def _parallel(value):
76
+ try:
77
+ return _CORE_PARALLEL_MODES[value]
78
+ except (KeyError, TypeError) as error:
79
+ raise ValueError(f"parallel must be one of {PARALLEL_MODES}") from error
80
+
81
+
82
+ def _positive_integer(value, name, *, minimum=1):
83
+ try:
84
+ value = operator.index(value)
85
+ except TypeError as error:
86
+ raise ValueError(f"{name} must be an integer") from error
87
+ if value < minimum:
88
+ raise ValueError(f"{name} must be at least {minimum}")
89
+ return value
90
+
91
+
92
+ def _bounded(array, name):
93
+ if np.any((array < 0.0) | (array > 1.0)):
94
+ raise ValueError(f"{name} must contain values inside [0, 1]")
95
+ return array
96
+
97
+
98
+ def _sorted(array):
99
+ if np.any(array[1:] < array[:-1]):
100
+ raise ValueError("sorted_data must be sorted")
101
+ return array
102
+
103
+
104
+ def _approximation_options(kernel, cutoff, max_neighbors, fast_gaussian,
105
+ memory, *, core_compatible=True):
106
+ if cutoff is None:
107
+ cutoff_value = 0.0
108
+ else:
109
+ try:
110
+ cutoff_value = float(cutoff)
111
+ except (TypeError, ValueError) as error:
112
+ raise ValueError("cutoff must be positive or None") from error
113
+ if (not np.isfinite(cutoff_value) or cutoff_value < 0.0
114
+ or (cutoff_value == 0.0 and not core_compatible)):
115
+ raise ValueError("cutoff must be positive or None")
116
+
117
+ if max_neighbors is None:
118
+ neighbor_count = 0
119
+ else:
120
+ neighbor_count = _positive_integer(
121
+ max_neighbors, "max_neighbors", minimum=0 if core_compatible else 1
122
+ )
123
+
124
+ if not isinstance(fast_gaussian, (bool, np.bool_)):
125
+ raise ValueError("fast_gaussian must be a boolean")
126
+ if fast_gaussian and kernel != "gaussian":
127
+ raise ValueError("fast_gaussian is only available for Gaussian KDE")
128
+
129
+ if core_compatible and not isinstance(memory, str):
130
+ memory_id = _positive_integer(memory, "memory", minimum=0)
131
+ if memory_id > 2:
132
+ raise ValueError("memory must be 0, 1, 2, or a named memory mode")
133
+ else:
134
+ try:
135
+ memory_id = _MEMORY_MODE_IDS[memory]
136
+ except (KeyError, TypeError) as error:
137
+ raise ValueError(f"memory must be one of {MEMORY_MODES}") from error
138
+
139
+ return cutoff_value, neighbor_count, bool(fast_gaussian), memory_id
140
+
141
+
142
+ def kernel_is_symmetric(kernel):
143
+ return bool(_core.kernel_is_symmetric(_kernel(kernel)))
144
+
145
+
146
+ def kernel_default_cutoff(kernel):
147
+ return float(_core.kernel_default_cutoff(_kernel(kernel)))
148
+
149
+
150
+ def kde(data, xs, bandwidth, kernel="gaussian"):
151
+ return _core.kde(
152
+ _array_1d(data, "data"),
153
+ _array_1d(xs, "xs", nonempty=False),
154
+ _bandwidth(bandwidth),
155
+ _kernel(kernel),
156
+ )
157
+
158
+
159
+ def kde_self(data, bandwidth, kernel="gaussian"):
160
+ return _core.kde_self(
161
+ _array_1d(data, "data"), _bandwidth(bandwidth), _kernel(kernel)
162
+ )
163
+
164
+
165
+ def kde_beta_ext(data, xs, bandwidth):
166
+ return _core.kde_beta_ext(
167
+ _bounded(_array_1d(data, "data"), "data"),
168
+ _bounded(_array_1d(xs, "xs", nonempty=False), "xs"),
169
+ _bandwidth(bandwidth),
170
+ )
171
+
172
+
173
+ def kde_beta_self(data, bandwidth):
174
+ return _core.kde_beta_self(
175
+ _bounded(_array_1d(data, "data"), "data"), _bandwidth(bandwidth)
176
+ )
177
+
178
+
179
+ def kde_reflected_ext(data, xs, bandwidth, kernel="gaussian"):
180
+ return _core.kde_reflected_ext(
181
+ _bounded(_array_1d(data, "data"), "data"),
182
+ _bounded(_array_1d(xs, "xs", nonempty=False), "xs"),
183
+ _bandwidth(bandwidth),
184
+ _kernel(kernel),
185
+ )
186
+
187
+
188
+ def kde_reflected_self(data, bandwidth, kernel="gaussian"):
189
+ return _core.kde_reflected_self(
190
+ _bounded(_array_1d(data, "data"), "data"),
191
+ _bandwidth(bandwidth),
192
+ _kernel(kernel),
193
+ )
194
+
195
+
196
+ def bandwidth_loo(data, h_grid, kernel="gaussian", parallel="auto"):
197
+ return _core.bandwidth_loo(
198
+ _array_1d(data, "data"),
199
+ _bandwidth_grid(h_grid),
200
+ _kernel(kernel),
201
+ _parallel(parallel),
202
+ )
203
+
204
+
205
+ def bandwidth_kfold(data, h_grid, k_folds, kernel="gaussian",
206
+ parallel="auto"):
207
+ samples = _array_1d(data, "data")
208
+ folds = _positive_integer(k_folds, "k_folds", minimum=2)
209
+ if folds > samples.size:
210
+ raise ValueError("k_folds must not exceed the number of samples")
211
+ return _core.bandwidth_kfold(
212
+ samples, _bandwidth_grid(h_grid), folds, _kernel(kernel),
213
+ _parallel(parallel)
214
+ )
215
+
216
+
217
+ def bandwidth_grid(data, h_grid, kernel="gaussian", k_folds=1,
218
+ parallel="auto"):
219
+ samples = _array_1d(data, "data")
220
+ folds = _positive_integer(k_folds, "k_folds")
221
+ if folds != 1 and (folds < 2 or folds > samples.size):
222
+ raise ValueError("k_folds must be 1 or between 2 and the sample count")
223
+ return _core.bandwidth_grid(
224
+ samples, _bandwidth_grid(h_grid), _kernel(kernel), folds,
225
+ _parallel(parallel)
226
+ )
227
+
228
+
229
+ def kde_approx(sorted_data, xs, bandwidth, kernel="gaussian", cutoff=None,
230
+ max_neighbors=None, fast_gaussian=False, memory="auto"):
231
+ kernel = _kernel(kernel)
232
+ options = _approximation_options(
233
+ kernel, cutoff, max_neighbors, fast_gaussian, memory
234
+ )
235
+ return _core.kde_approx(
236
+ _sorted(_array_1d(sorted_data, "sorted_data")),
237
+ _array_1d(xs, "xs", nonempty=False),
238
+ _bandwidth(bandwidth),
239
+ kernel,
240
+ *options,
241
+ )
242
+
243
+
244
+ def kde_approx_self(sorted_data, bandwidth, kernel="gaussian", cutoff=None,
245
+ max_neighbors=None, fast_gaussian=False, memory="auto"):
246
+ kernel = _kernel(kernel)
247
+ options = _approximation_options(
248
+ kernel, cutoff, max_neighbors, fast_gaussian, memory
249
+ )
250
+ return _core.kde_approx_self(
251
+ _sorted(_array_1d(sorted_data, "sorted_data")),
252
+ _bandwidth(bandwidth),
253
+ kernel,
254
+ *options,
255
+ )
256
+
257
+
258
+ def kde_multivariate(data, xs, bandwidth, kernel="gaussian", block_size=32):
259
+ samples = _array_2d(data, "data")
260
+ points = _array_2d(xs, "xs", nonempty=False)
261
+ if points.shape[1] != samples.shape[1]:
262
+ raise ValueError("data and xs must have the same number of features")
263
+ return _core.kde_multivariate(
264
+ samples, points, _bandwidth(bandwidth), _kernel(kernel),
265
+ _positive_integer(block_size, "block_size")
266
+ )
267
+
268
+
269
+ def kde_multivariate_self(data, bandwidth, kernel="gaussian", block_size=32):
270
+ return _core.kde_multivariate_self(
271
+ _array_2d(data, "data"),
272
+ _bandwidth(bandwidth),
273
+ _kernel(kernel),
274
+ _positive_integer(block_size, "block_size"),
275
+ )
276
+
277
+
278
+ __all__ = [
279
+ "bandwidth_grid",
280
+ "bandwidth_kfold",
281
+ "bandwidth_loo",
282
+ "kde",
283
+ "kde_approx",
284
+ "kde_approx_self",
285
+ "kde_beta_ext",
286
+ "kde_beta_self",
287
+ "kde_multivariate",
288
+ "kde_multivariate_self",
289
+ "kde_reflected_ext",
290
+ "kde_reflected_self",
291
+ "kde_self",
292
+ "kernel_default_cutoff",
293
+ "kernel_is_symmetric",
294
+ ]