Clustering on periodic circular coordinates.
Project description
CircleClust
Clustering on periodic circular coordinates with automatic detection of centroids and boundary handling.
Installation
pip install circleclust
Why CircleClust?
Standard clustering algorithms don't account for periodicity. Values like color hues on a color wheel or bedtimes may wrap around boundaries. CircleClust handles this by correctly detecting clusters that cross periodic boundaries.
Features
- Automatic size parameter detection using train/test RMSD minimization
- Circular boundary handling - clusters crossing 0/period are correctly identified
- Period-aware clustering - works with any period (radians, degrees, hours, minutes)
- Visualization tools - plot centroids means and stds
- Robust peak detection - identifies distribution peaks on periodic axes
Quick Start
import numpy as np
import pandas as pd
from circleclust import CircleClust
url = "https://raw.githubusercontent.com/timpyrkov/circleclust/refs/heads/master/tests/sample.csv"
# Read sample data csv: 500 data points in range [0, 2π)
df = pd.read_csv(url)
data = df['x'].values
# Create and fit model
clust = CircleClust(verbose=True)
clust.fit(data, period=2*np.pi)
# Get detected centroids
clust.get_centroids()
# Output: {'centroid': array([0.46530696, 3.1074802 , 4.34047566]),
# 'std': array([0.66322512, 0.2304881 , 0.29800344])}
# Predict cluster labels
labels = clust.predict(data)
np.unique(labels, return_counts=True)
# Output: (array([-1, 0, 1, 2]), array([179, 150, 81, 90]))
# Visualize results
clust.show_peaks(output="clusters.png")
Examples
Color Hue Clustering
Cluster pixel hues from an image, correctly handling the red color boundary:
Notebook: Color Wheel Example
This example demonstrates how CircleClust correctly identifies red clusters that span the 0°/360° boundary, treating pixels near both boundaries as a single cluster.
Sleep Pattern Analysis
Analyze go-to-sleep and wake-up times near midnight:
Notebook: Sleep-Wake Pattern Example
This example shows how to detect sleep patterns that cross the day boundary at midnight, properly clustering late-night and early-morning bedtimes.
API Reference
CircleClust Class
Main class for circular clustering with automatic peak detection.
Constructor Parameters
data(Iterable[float], optional): Data to fit immediately upon constructionperiod(float, default 2π): Period of input values; data is wrapped into [0, period)window(float, optional): Manual override for smoothing window widthmax_screen_divisor(int, default 32): Maximum divisor k in window screeningmax_screen_iter(int, default 2): Number of screening repetitionstrain_frac(float, default 0.7): Training fraction during screeningrandom_seed(int, default 0): Random seed for reproducibilityverbose(bool, default False): Enable informational prints
Main Methods
fit(data, period=None): Fit the model to data. Important: provide correct period range for data values!predict(data): Predict cluster labels for input data points (returns array of integers, -1 for outliers)get_centroids(): Get detected centroids as a dict with 'centroid' (means) and 'std' (standard deviations) arraysshow_peaks(output=None): Visualize detected peaks on a histogram plotshow_centroids(output=None): Alias for show_peaks()
Usage Tips
-
Provide the correct period: Your data must be within [0, period). Make sure your period matches your data domain (e.g.,
2*np.pifor radians,360for degrees,24*60for minutes in a day) -
Let the algorithm find the optimal window: Unless you have domain knowledge, let CircleClust automatically detect the optimal smoothing window
-
Check for outliers: Points labeled
-1in the predict output are outliers not assigned to any cluster
Documentation
Comprehensive documentation with interactive examples: https://circleclust.readthedocs.io
Project details
Release history Release notifications | RSS feed
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 circleclust-0.0.2.tar.gz.
File metadata
- Download URL: circleclust-0.0.2.tar.gz
- Upload date:
- Size: 327.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5defaa99b1ebaa804dd02bb61a5dce64a4fbee823b07f7eea807fd6b36ac4dc
|
|
| MD5 |
5b97bba9b49319b19e9e1a6acbe965c9
|
|
| BLAKE2b-256 |
ed0418d2baaa1c96b1a350dc0e1c5f421cb77b8ffcc78c67dca62ee5369c6c2d
|
File details
Details for the file circleclust-0.0.2-py3-none-any.whl.
File metadata
- Download URL: circleclust-0.0.2-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca522979752dbdeb4b6e50864611b71f47a4d5f9f75b0ffcc00b74dcb62ddf20
|
|
| MD5 |
40ad20e60466c628a948a8179834f3a6
|
|
| BLAKE2b-256 |
87c4a23c2df66b25c1cbd09153a25c29fd060451a282aa18d951d7a287a026c0
|