swarmsort.embeddings module
GPU-accelerated embedding extractors for SwarmSort standalone package. Provides cupytexture and cupytexture_mega embeddings with automatic CPU fallback.
- class swarmsort.embeddings.CupyShapeEmbedding(use_gpu=True, hog_cells=3, hog_bins=9)[source]
Bases:
EmbeddingExtractorShape-focused embedding optimized for microscopy organism tracking.
Key design principles (from embedding_recommendation.md): - NO RESIZE: Processes original patch resolution to preserve fine details - SHAPE-FOCUSED: Prioritizes contour/boundary features over texture - ROTATION-INVARIANT: Handles organisms at any angle
Feature composition (96 dimensions): - HOG features (36 dims): Histogram of Oriented Gradients for local shape - Fourier descriptors (16 dims): Contour shape in frequency domain - Radial signature (32 dims): Distance from centroid to boundary - Hu moments (7 dims): Classic rotation/scale invariant moments - Shape stats (5 dims): Area, perimeter, circularity, aspect ratio, solidity
This embedding is specifically designed for grayscale microscopy images where organisms have similar internal textures but distinct boundary shapes.
- class swarmsort.embeddings.CupyTextureColorEmbedding(patch_size=32, use_gpu=True)[source]
Bases:
EmbeddingExtractorCuPy-accelerated texture embedding with rich color features.
Combines the robust texture analysis of CupyTextureEmbedding with comprehensive color-based features for improved tracking and re-identification performance.
Feature composition (84 total features): - Grayscale texture features (32) - from base CupyTextureEmbedding - RGB color histograms (24) - 8 bins per channel - HSV color histograms (24) - 8 bins per channel - Color moments (4) - mean and std of RGB channels
This embedding is particularly effective for scenarios where both texture and color information are important for distinguishing between objects.
- class swarmsort.embeddings.CupyTextureEmbedding(patch_size=32, use_gpu=True)[source]
Bases:
EmbeddingExtractorFast GPU-accelerated embedding for microorganism tracking. Features: 36 dimensions - Basic statistics (4): mean, std, min, max - Gradient features (4): gradient magnitudes and variations - Shape features (8): center of mass, moments, eccentricity, orientation - Radial features (8): intensity at different radii from center - Color features (6): HSV statistics - Texture features (6): Local Binary Pattern approximation
- class swarmsort.embeddings.EmbeddingExtractor(use_tensor=False)[source]
Bases:
objectBase class for embedding extractors in standalone package.
- class swarmsort.embeddings.MegaCupyTextureEmbedding(patch_size=32, use_gpu=True)[source]
Bases:
EmbeddingExtractorMega Microbe Embedding: Combines the best of shape, color, and texture analysis. - Dims 0-7: Basic stats and gradients - Dims 8-17: Advanced shape features (moments, eccentricity, Hu) - Dims 18-23: HSV color statistics (including circular stats for hue) - Dims 24-33: Rotation-invariant Local Binary Patterns (LBP) - Dims 34-49: Robust radial & polar features (intensity, variation, DFT) - Dims 50-55: Multi-scale wavelet and entropy texture features - Dims 56-63: Reserved / Other advanced features
- swarmsort.embeddings.compute_embedding_distance(emb1, emb2, metric='cosine')[source]
Compute distance between two embeddings.
- Parameters:
- Return type:
- Returns:
Distance in [0, 1] where 0 = identical, 1 = maximally different
- swarmsort.embeddings.compute_embedding_distances_batch(emb, embs, metric='cosine')[source]
Compute distances from one embedding to multiple embeddings.
- swarmsort.embeddings.correlation_distance_jit(emb1, emb2)[source]
JIT-optimized correlation (Pearson) distance computation.
Correlation distance = (1 - Pearson_correlation) / 2 Returns value in [0, 1] where 0 = identical pattern, 1 = opposite pattern.
More robust to offset differences than cosine, but may discard useful mean information from well-normalized embeddings.
- Return type:
- swarmsort.embeddings.cosine_distance_jit(emb1, emb2)[source]
JIT-optimized cosine distance computation.
Cosine distance = 1 - cosine_similarity Returns value in [0, 1] where 0 = identical, 1 = opposite.
This is the standard distance metric for L2-normalized embeddings.
- Return type:
- swarmsort.embeddings.extract_features_batch_jit(patches, features_out)[source]
JIT-optimized batch feature extraction for microorganism patches. patches: (N, H, W, 3) - batch of BGR patches features_out: (N, feature_dim) - output features
- swarmsort.embeddings.extract_mega_features_batch_jit(patches, features_out)[source]
JIT-optimized batch feature extraction combining shape, color, and texture. - Shape: Moments, eccentricity, orientation - Color: HSV statistics - Texture: Rotation-invariant LBP, multi-scale wavelet, entropy
- swarmsort.embeddings.get_embedding_extractor(name, **kwargs)[source]
Get an embedding extractor by name.
- Return type: