fl-data-generator
A lightweight, framework-agnostic Python library for IID and Non-IID data partitioning in Federated Learning (FL).
Overview
In Federated Learning research and benchmarks, simulating realistic data heterogeneity across distributed clients is essential. fl-data-generator simplifies partitioning arbitrary datasets into realistic client distributions without tying you to any specific deep learning framework.
Whether you are using PyTorch, TensorFlow, JAX, or federated frameworks like Flower, FedML, or PySyft, fl-data-generator provides clean sample indices and dataset wrappers for your clients.
Features
- IID Partitioning: Uniform random sample allocation across all clients.
- Dirichlet Partitioning: Continuous label distribution skew ($\text{Dir}(\alpha)$) over class proportions, with client minimum size constraints (
min_size). - Pathological Non-IID Partitioning: Allocation of a fixed number of classes ($k$ classes per client, following the NIID-Bench benchmark scheme).
- Quantity Skew Partitioning: Volume heterogeneity where client dataset sizes vary according to a Dirichlet distribution ($\text{Dir}(\beta)$).
- Hybrid Partitioning (Experimental): Sequential joint label skew and quantity skew allocation combining Dirichlet $\alpha$ and $\beta$.
- FederatedDataset Wrapper: High-level wrapper supporting built-in computer vision datasets (
mnist,cifar10,cifar100,fashion_mnist) as well as custom(X, y)tuples and PyTorchDatasetinstances.
Installation
From PyPI (Recommended)
pip install fl-data-generator
With Optional Extras
# PyTorch and torchvision integration
pip install "fl-data-generator[torch]"
# All optional dependencies (torch + visualization)
pip install "fl-data-generator[all]"
# Development dependencies (pytest, hypothesis, ruff, etc.)
pip install "fl-data-generator[dev]"
# Visualization support (matplotlib)
pip install "fl-data-generator[viz]"
From Source
git clone https://github.com/khastkhodaei/fl-data-generator.git
cd fl-data-generator
pip install -e .
Runnable Examples
1. IID Partitioning
Uniformly partitions sample indices across clients.
import numpy as np
from fl_data_generator import iid_partition
labels = np.array([0, 1] * 50) # 100 samples
client_dict = iid_partition(labels, num_clients=4, seed=42)
print("Client sample counts:", [len(indices) for indices in client_dict.values()])
# Real Output: Client sample counts: [25, 25, 25, 25]
2. Dirichlet Label Skew Partitioning
Simulates non-IID label skew parameterized by concentration parameter alpha. Smaller $\alpha$ generates severe distribution skew, while larger $\alpha$ approaches uniform class distributions.
import numpy as np
from fl_data_generator import dirichlet_partition
labels = np.repeat(np.arange(4), 50) # 200 samples across 4 classes
client_dict = dirichlet_partition(labels, num_clients=3, alpha=0.5, seed=42)
print("Client sample counts:", [len(indices) for indices in client_dict.values()])
# Real Output: Client sample counts: [114, 42, 44]
3. Pathological Non-IID Partitioning ($k$ Classes per Client)
Partitions data such that each client receives samples from exactly $k$ distinct classes (NIID-Bench #C=k scheme).
import numpy as np
from fl_data_generator import pathological_partition
labels = np.repeat(np.arange(4), 50) # 200 samples across 4 classes
client_dict = pathological_partition(labels, num_clients=4, classes_per_client=2, seed=42)
client_0_labels = labels[client_dict[0]]
print("Client 0 distinct classes:", np.unique(client_0_labels).tolist())
# Real Output: Client 0 distinct classes: [1, 2]
4. Quantity Skew Partitioning (Volume Heterogeneity)
Allocates varying numbers of samples to clients according to a Dirichlet distribution parameterized by concentration parameter beta.
import numpy as np
from fl_data_generator import quantity_skew_partition
labels = np.arange(200) # 200 samples
client_dict = quantity_skew_partition(labels, num_clients=4, beta=0.5, seed=42)
print("Client sample counts:", [len(indices) for indices in client_dict.values()])
# Real Output: Client sample counts: [52, 72, 16, 60]
5. Hybrid Partitioning (Experimental)
Combines quantity skew ($\beta$) and label skew ($\alpha$) via sequential allocation.
import numpy as np
from fl_data_generator import hybrid_partition
labels = np.repeat(np.arange(4), 50) # 200 samples across 4 classes
client_dict = hybrid_partition(labels, num_clients=4, alpha=0.5, beta=0.5, seed=42)
print("Client sample counts:", [len(indices) for indices in client_dict.values()])
# Real Output: Client sample counts: [56, 89, 2, 53]
6. FederatedDataset Interface
High-level interface for creating client-specific datasets and PyTorch DataLoaders.
import numpy as np
from fl_data_generator import FederatedDataset
X = np.random.default_rng(42).standard_normal((100, 5))
y = np.array([0, 1] * 50)
fed_data = FederatedDataset((X, y), num_clients=4, partition="iid", seed=42)
client_0_ds = fed_data.get_client_dataset(0)
print(f"Client 0 sample count: {len(client_0_ds)}")
features, label = client_0_ds[0]
print(f"Feature shape: {features.shape}, Label: {label}")
# Real Output:
# Client 0 sample count: 25
# Feature shape: (5,), Label: 1
Roadmap
The following experimental features and partition strategies are planned for future releases:
- Feature Skew (Covariate Shift): Partitioning based on continuous feature space clusters or synthetic domain perturbations.
- Power-Law Quantity Skew: Heavy-tailed sample volume distribution across clients.
- Asymmetric Class Topologies: Custom directed class sharing graphs.
Project Structure
fl-data-generator/
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI matrix workflow
├── .gitattributes # Line ending normalization
├── src/
│ └── fl_data_generator/ # Core library package
│ ├── __init__.py # Public API exports
│ ├── _validation.py # Input validation utilities
│ ├── dataset.py # FederatedDataset interface & torchvision loaders
│ ├── dirichlet.py # Dirichlet label skew partitioner
│ ├── hybrid.py # Hybrid label & quantity skew partitioner
│ ├── iid.py # Uniform IID partitioner
│ ├── pathological.py # Pathological k-class partitioner
│ └── quantity.py # Quantity skew partitioner
├── tests/ # Unit, integration, and property test suite
│ ├── test_dataset.py
│ ├── test_dirichlet.py
│ ├── test_hybrid.py
│ ├── test_iid.py
│ ├── test_pathological.py
│ ├── test_properties.py
│ ├── test_quantity.py
│ ├── test_readme.py
│ └── test_validation.py
├── CHANGELOG.md # Release changelog (Keep a Changelog)
├── CITATION.cff # Citation metadata
├── LICENSE # MIT License
├── README.md # Project documentation
└── pyproject.toml # Package build configuration & Ruff settings
Citation
If you use fl-data-generator in your research or benchmarks, please cite it using:
@software{khastkhodaei2026fldatagenerator,
author = {Khastkhodaei, Mostafa},
title = {fl-data-generator: A Lightweight Federated Learning Data Partitioning Library},
url = {https://github.com/khastkhodaei/fl-data-generator},
version = {0.1.2},
year = {2026}
}
You can also use the citation metadata provided in CITATION.cff.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for fl-data-generator 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fl_data_generator-0.1.2.tar.gz | 26.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fl_data_generator-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.5 kB
Release files / fl_data_generator-0.1.2.tar.gz
| Download URL | fl_data_generator-0.1.2.tar.gz |
|---|---|
| Size | 26.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a062f03d2f535ca8f4b53cfab5a5d4c516238f2106e5b76cdbe7e7365fc431c4
|
|
BLAKE2b-256 checksum How to use checksums |
9fc35d6adf8c5fc4e0987b40a1ceb3ab542eab1c4b805aac495098dd0d9acd3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / fl_data_generator-0.1.2-py3-none-any.whl
| Download URL | fl_data_generator-0.1.2-py3-none-any.whl |
|---|---|
| Size | 19.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e556c39acd721ce5efd76eb87a66d48803bb0627d8f551ef5edb4d8484150bd8
|
|
BLAKE2b-256 checksum How to use checksums |
b5dfe31d997fbbfabc033e58eb6d429018b30a48f3b73b795260ad90a881d783
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Security for fl-data-generator 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.