A Python package for generating synthetic data.
Project description
📦 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file synker-0.0.5.tar.gz.
File metadata
- Download URL: synker-0.0.5.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7751a4a9e47e8a02029fe73e2e9dd57ffa2bfdeede258426a04eff806d6bd938
|
|
| MD5 |
6d8a790ff3919a997c0a770fe06d679a
|
|
| BLAKE2b-256 |
6098a78bbf218b8dadb8e99ae70107a23468a857a48b877efb369875fd0547d8
|
File details
Details for the file synker-0.0.5-py3-none-any.whl.
File metadata
- Download URL: synker-0.0.5-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b401ace5256e6e09dc192f6f1d39e3ef3946b443867271cc4c09d6c592ff877
|
|
| MD5 |
546dd41043ca9e2b4d6d2faceff95f64
|
|
| BLAKE2b-256 |
4e90f0109d27695e92cd9747c412e4f1ba10e9f59e3c8f5fecda0faeb38957a9
|