NBSlim
Rust implementation of SIA, SIATEC, COSIATEC, and RecurSIA – algorithms for compressing 2D point sets by discovering translational equivalence classes (TECs). Designed for compressing Note Block Studio (.nbs) music files, but works on any set of points in the plane.
This crate is the core algorithm implementation extracted from the NBSlim Python package, released independently for Rust projects.
Algorithms
- SIA – finds all maximal translatable patterns from a point set (
O(n²)). - SIATEC – builds translational equivalence classes from SIA results (
O(m·n), worst-caseO(n³)). - COSIATEC – greedy lossless compression: repeatedly extract the best TEC (highest compression ratio) and remove its covered points.
- RecurSIA – recursive version of COSIATEC that compresses patterns of each TEC, capturing nested repetitions.
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
Usage
Basic compression with COSIATEC
use cosiatec_compress;
let points = vec!;
// restrict_dpitch_zero: only horizontal translations (Δtick, 0)
// sweepline_optimization: use faster sweepline exact matching (default true)
let tecs = cosiatec_compress;
for tec in &tecs
Recursive compression (RecurSIA)
use recursive_cosiatec_compress;
let tecs = recursive_cosiatec_compress;
// Patterns smaller than 2 points are not recursed.
Working with TECs
use HashSet;
use TranslationalEquivalence;
// Coverage set (all points that belong to any occurrence of this TEC)
let coverage: = tec.coverage;
// Compression ratio (coverage size / encoding units)
let ratio = tec.compression_ratio;
// Compactness relative to the full dataset
let dataset_points: = points.iter.copied.collect;
let compactness = tec.compactness;
Reconstructing original points from TECs
use HashSet;
let mut all_points = new;
for tec in &tecs
let mut reconstructed: = all_points.into_iter.collect;
reconstructed.sort;
assert_eq!;
Merge small TECs (using a static predicate)
use merge_tecs;
let merged = merge_tecs;
Compression statistics
use compression_stats;
let = compression_stats;
println!;
Building TECs directly from SIA patterns
use ;
let mtps = find_mtps;
let tecs = build_tecs_from_mtps;
NBS file integration (optional)
The crate includes utils::notes_to_points and utils::points_to_notes to convert between NBS note events and the 2D point representation used by the algorithms, packing instrument and pitch into a single coordinate. See the documentation for details.
References
- Meredith, D., Lemström, K., & Wiggins, G. A. (2002). Algorithms for discovering repeated patterns in multidimensional representations of polyphonic music.
- Meredith, D. (2013). COSIATEC and SIATECCompress: Pattern discovery by geometric compression.
- Meredith, D. (2019). RecurSIA-RRT: Recursive translatable point-set pattern discovery with removal of redundant translators. arXiv:1906.12286.