📦 synker
synker is a Python package for generating synthetic datasets based on real data using Kernel Density Estimation (KDE) methods.
It supports:
- Bandwidth selection (Scott's and Silverman's rules)
- 2D KDE
- Synthetic data generation
- Kullback–Leibler (KL) divergence evaluation
- Arbitrary probability interval selection for data filtering and analysis
🚀 Features
- Bandwidth Estimation using Scott's and Silverman's rules
- 2D Kernel Density Estimation
- Synthetic Data Generation based on KDE
- KL Divergence Calculation to compare real and synthetic data
- Probability Interval Filtering (
pinkde): Identify data points within specified probability ranges - Modular and extensible design for ease of integration
🧠 Installation
Clone the repository and install the package locally:
git clone https://github.com/dhaselib/synker
cd synker
pip install .
🗂 Example Usage
import numpy as np
import matplotlib.pyplot as plt
from synker import Scott
from synker import Silverman
from synker import KL_div
from synker import Synthetic
from synker import kde, Pinkde
import pandas as pd
# Generate sample data
np.random.seed(42)
data = np.random.weibull(a=10, size=(1000, 2))
X = np.random.weibull(a=5, size=1000)
Y = np.random.weibull(a=20, size=1000)
# Bandwidth estimation
hx = Scott(X)
hy = Scott(Y)
# Or using Silverman's rule
# hx = Silverman(X)
# hy = Silverman(Y)
# KDE
syn_X = np.linspace(min(X), max(X), 100)
syn_Y = np.linspace(min(Y), max(Y), 100)
pkde = kde(X, Y, syn_X, syn_Y, hx, hy)
# Alternatively:
pkde = kde(X, Y, hx=hx, hy=hy, res=100)
# Generate synthetic data
synth_data = Synthetic(X=X, Y=Y, hx=hx, hy=hy, res=100)
# Or use automatic bandwidth selection
synth_data = Synthetic(X, Y, bandwidth_method="Scott")
Synth_X = synth_data[:, 0]
Synth_Y = synth_data[:, 1]
# Calculate KL divergence
KL_divergence = KL_div(real_data=data, synthetic_data=synth_data, hx=hx, hy=hy)
# Use pinkde to filter based on probability interval
grid_x = np.linspace(min(X), max(X), 100)
grid_y = np.linspace(min(Y), max(Y), 100)
min_val, max_val = 0.2, 0.5
pinkde_result = Pinkde(X, Y, hx, hy, "Scott", grid_x, grid_y, 100, min_val, max_val)
# Plotting
plt.figure(figsize=(8, 8))
plt.scatter(X, Y, label="Original Data")
plt.scatter(Synth_X, Synth_Y, label="Synthetic Data")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("Original vs. Synthetic Data")
plt.figure(figsize=(8, 8))
plt.scatter(X, Y, label="Original Data", alpha=0.3)
plt.scatter(
pinkde_result["X"],
pinkde_result["Y"],
label=f"P {min_val * 100:.0f}–{max_val * 100:.0f}%",
color='red',
alpha=0.3
)
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("Pinkde Result")
plt.show()
✅ Testing
A test file test_synker.py is included to validate core functionalities like bandwidth estimation, KDE, synthetic generation, KL divergence, and the pinkde module.
Run tests:
python -m unittest test_synker.py
Tests cover:
- Scott and Silverman bandwidth estimation
- 2D KDE result structure
- Synthetic data shape and boundary validation
- Non-negative KL divergence check
- Probability interval selection with
pinkde
📁 Project Structure
synker/
├── __init__.py
├── scott.py
├── silverman.py
├── kde.py
├── sampling.py
├── kl_divergence.py
├── pinkde.py
├── tests/
│ └── test_synker.py
└── README.md
📜 License
This project is licensed under the MIT License:
MIT License
Copyright (c) 2025 Danial Haselibozchaloee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00#\x00 \x001\x00
\x00
\x00
Release files for synker 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| synker-0.0.5.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| synker-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / synker-0.0.5.tar.gz
| Download URL | synker-0.0.5.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7751a4a9e47e8a02029fe73e2e9dd57ffa2bfdeede258426a04eff806d6bd938
|
|
BLAKE2b-256 checksum How to use checksums |
6098a78bbf218b8dadb8e99ae70107a23468a857a48b877efb369875fd0547d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|
Release files / synker-0.0.5-py3-none-any.whl
| Download URL | synker-0.0.5-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b401ace5256e6e09dc192f6f1d39e3ef3946b443867271cc4c09d6c592ff877
|
|
BLAKE2b-256 checksum How to use checksums |
4e90f0109d27695e92cd9747c412e4f1ba10e9f59e3c8f5fecda0faeb38957a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|