sevensense-analysis 0.1.0

Analysis bounded context for 7sense bioacoustics platform - clustering, motif detection, sequence analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# sevensense-analysis

[![Crate](https://img.shields.io/badge/crates.io-sevensense--analysis-orange.svg)](https://crates.io/crates/sevensense-analysis)
[![Docs](https://img.shields.io/badge/docs-sevensense--analysis-blue.svg)](https://docs.rs/sevensense-analysis)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](../../LICENSE)

> Advanced acoustic analysis algorithms for bioacoustic pattern discovery.

**sevensense-analysis** provides sophisticated analysis tools for understanding bird vocalizations at scale. From clustering calls into groups, detecting recurring motifs, to modeling temporal patterns with Markov chains, it transforms raw embeddings into actionable ecological insights.

## Features

- **HDBSCAN Clustering**: Density-based clustering for call-type discovery
- **Markov Models**: Temporal sequence analysis and prediction
- **Motif Detection**: Find recurring vocal patterns
- **Statistical Analysis**: Entropy, diversity indices, anomaly scores
- **Temporal Patterns**: Diel rhythms, seasonal trends
- **Multi-scale Analysis**: From milliseconds to months

## Use Cases

| Use Case | Description | Key Functions |
|----------|-------------|---------------|
| Call-Type Clustering | Group similar vocalizations | `hdbscan_cluster()` |
| Sequence Analysis | Model call sequences | `MarkovChain::analyze()` |
| Motif Discovery | Find repeated patterns | `detect_motifs()` |
| Diversity Metrics | Shannon/Simpson indices | `diversity_index()` |
| Periodicity | Detect rhythmic patterns | `detect_periodicity()` |
| Anomaly Detection | Find unusual calls | `anomaly_score()` |

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
sevensense-analysis = "0.1"
```

## Quick Start

```rust
use sevensense_analysis::{HdbscanClusterer, HdbscanConfig};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Cluster embeddings by call type
    let config = HdbscanConfig {
        min_cluster_size: 5,
        min_samples: 3,
        ..Default::default()
    };

    let clusterer = HdbscanClusterer::new(config);
    let labels = clusterer.fit(&embeddings)?;

    // Count clusters (excluding noise = -1)
    let n_clusters = labels.iter().filter(|&&l| l >= 0).max().unwrap_or(&-1) + 1;
    println!("Found {} call types", n_clusters);

    Ok(())
}
```

---

<details>
<summary><b>Tutorial: HDBSCAN Clustering</b></summary>

### Basic Clustering

```rust
use sevensense_analysis::{HdbscanClusterer, HdbscanConfig};

let config = HdbscanConfig {
    min_cluster_size: 5,      // Minimum points per cluster
    min_samples: 3,           // Core point threshold
    epsilon: 0.0,             // 0 = automatic selection
    metric: DistanceMetric::Euclidean,
};

let clusterer = HdbscanClusterer::new(config);
let result = clusterer.fit(&embeddings)?;

println!("Labels: {:?}", result.labels);
println!("Probabilities: {:?}", result.probabilities);
println!("Outlier scores: {:?}", result.outlier_scores);
```

### Cluster Analysis

```rust
use sevensense_analysis::{cluster_statistics, ClusterStats};

let stats = cluster_statistics(&embeddings, &labels)?;

for (cluster_id, stat) in stats.iter() {
    println!("Cluster {}:", cluster_id);
    println!("  Size: {}", stat.size);
    println!("  Centroid: {:?}", &stat.centroid[..5]);  // First 5 dims
    println!("  Intra-cluster distance: {:.3}", stat.intra_distance);
    println!("  Silhouette score: {:.3}", stat.silhouette);
}
```

### Cluster Assignment for New Data

```rust
// Assign new embeddings to existing clusters
let new_embeddings = load_new_data()?;
let assignments = clusterer.predict(&new_embeddings)?;

for (embedding, cluster) in new_embeddings.iter().zip(assignments.iter()) {
    if *cluster >= 0 {
        println!("Assigned to cluster {}", cluster);
    } else {
        println!("Classified as noise/outlier");
    }
}
```

</details>

<details>
<summary><b>Tutorial: Markov Chain Analysis</b></summary>

### Building a Markov Model

```rust
use sevensense_analysis::{MarkovChain, MarkovConfig};

// Sequences of cluster labels (call types)
let sequences: Vec<Vec<i32>> = vec![
    vec![0, 1, 2, 0, 1],  // Sequence 1
    vec![0, 1, 0, 2, 1],  // Sequence 2
    vec![1, 2, 0, 1, 2],  // Sequence 3
];

let config = MarkovConfig {
    order: 1,              // First-order Markov chain
    smoothing: 0.01,       // Laplace smoothing
};

let chain = MarkovChain::fit(&sequences, config)?;

// Get transition probabilities
let probs = chain.transition_matrix();
println!("P(1|0) = {:.3}", probs[(0, 1)]);  // Probability of 1 given 0
```

### Sequence Prediction

```rust
// Predict next state
let current_state = 0;
let next_probs = chain.predict_next(current_state)?;

println!("Next state probabilities from state {}:", current_state);
for (state, prob) in next_probs.iter().enumerate() {
    println!("  State {}: {:.3}", state, prob);
}

// Generate synthetic sequence
let generated = chain.generate(10, Some(0))?;  // 10 states, starting from 0
println!("Generated sequence: {:?}", generated);
```

### Sequence Analysis

```rust
use sevensense_analysis::MarkovAnalysis;

let analysis = MarkovAnalysis::new(&chain);

// Stationary distribution
let stationary = analysis.stationary_distribution()?;
println!("Stationary distribution: {:?}", stationary);

// Entropy rate
let entropy = analysis.entropy_rate()?;
println!("Entropy rate: {:.3} bits", entropy);

// Expected hitting times
let hitting_times = analysis.mean_hitting_times()?;
println!("Mean hitting time 0→2: {:.2} steps", hitting_times[(0, 2)]);
```

</details>

<details>
<summary><b>Tutorial: Motif Detection</b></summary>

### Finding Repeated Patterns

```rust
use sevensense_analysis::{MotifDetector, MotifConfig};

let config = MotifConfig {
    min_length: 3,           // Minimum motif length
    max_length: 10,          // Maximum motif length
    similarity_threshold: 0.85,
    min_occurrences: 2,
};

let detector = MotifDetector::new(config);
let motifs = detector.detect(&embeddings)?;

for motif in &motifs {
    println!("Motif found:");
    println!("  Length: {} segments", motif.length);
    println!("  Occurrences: {}", motif.occurrences.len());
    println!("  Positions: {:?}", motif.positions());
    println!("  Average similarity: {:.3}", motif.avg_similarity);
}
```

### Motif Visualization

```rust
use sevensense_analysis::motif_to_sequence;

for motif in motifs.iter().take(5) {
    // Get the representative sequence
    let sequence = motif_to_sequence(&embeddings, motif)?;

    println!("Motif #{} (len={})", motif.id, motif.length);
    println!("  Representative: {:?}", sequence);

    // Show all occurrences
    for (i, occ) in motif.occurrences.iter().enumerate() {
        println!("  Occurrence {}: positions {}-{}",
            i, occ.start, occ.end);
    }
}
```

### Cross-Recording Motifs

```rust
// Find motifs that appear across multiple recordings
let recordings: Vec<(RecordingId, Vec<Embedding>)> = load_recordings()?;

let cross_motifs = detector.detect_cross_recording(&recordings)?;

for motif in cross_motifs {
    println!("Cross-recording motif:");
    println!("  Appears in {} recordings", motif.recording_ids.len());
    println!("  Total occurrences: {}", motif.total_occurrences);
}
```

</details>

<details>
<summary><b>Tutorial: Statistical Analysis</b></summary>

### Diversity Indices

```rust
use sevensense_analysis::{shannon_index, simpson_index, species_richness};

// Count species occurrences
let species_counts = count_species(&labels)?;

let shannon = shannon_index(&species_counts);
let simpson = simpson_index(&species_counts);
let richness = species_richness(&species_counts);

println!("Shannon Index (H'): {:.3}", shannon);
println!("Simpson Index (D): {:.3}", simpson);
println!("Species Richness: {}", richness);
```

### Entropy Analysis

```rust
use sevensense_analysis::{sequence_entropy, normalized_entropy};

// Entropy of call sequences
let sequence: Vec<i32> = vec![0, 1, 2, 0, 1, 0, 2, 1, 0];

let entropy = sequence_entropy(&sequence);
let norm_entropy = normalized_entropy(&sequence);

println!("Sequence entropy: {:.3} bits", entropy);
println!("Normalized entropy: {:.3}", norm_entropy);  // 0-1 scale
```

### Periodicity Detection

```rust
use sevensense_analysis::{detect_periodicity, PeriodicityConfig};

let config = PeriodicityConfig {
    min_period: 2,
    max_period: 100,
    confidence_threshold: 0.7,
};

let timestamps: Vec<f64> = get_call_timestamps()?;
let periods = detect_periodicity(&timestamps, config)?;

for (period, confidence) in periods {
    println!("Period: {:.1}s (confidence: {:.2})", period, confidence);
}
```

</details>

<details>
<summary><b>Tutorial: Temporal Analysis</b></summary>

### Diel Activity Patterns

```rust
use sevensense_analysis::{DielAnalyzer, TimeOfDay};

let analyzer = DielAnalyzer::new();

// Analyze activity by time of day
let pattern = analyzer.analyze(&timestamps)?;

println!("Dawn chorus: {} calls", pattern.count(TimeOfDay::Dawn));
println!("Morning: {} calls", pattern.count(TimeOfDay::Morning));
println!("Midday: {} calls", pattern.count(TimeOfDay::Midday));
println!("Evening: {} calls", pattern.count(TimeOfDay::Evening));
println!("Night: {} calls", pattern.count(TimeOfDay::Night));

// Peak activity time
let peak = pattern.peak_hour();
println!("Peak activity: {:02}:00", peak);
```

### Seasonal Trends

```rust
use sevensense_analysis::{SeasonalAnalyzer, Season};

let analyzer = SeasonalAnalyzer::new();
let trend = analyzer.analyze(&dated_records)?;

println!("Spring activity: {:.1}%", trend.percentage(Season::Spring));
println!("Breeding season peak: {:?}", trend.breeding_peak());
println!("Migration periods: {:?}", trend.migration_windows());
```

### Time Series Analysis

```rust
use sevensense_analysis::{TimeSeries, Aggregation};

let series = TimeSeries::from_events(&events)?;

// Aggregate by hour
let hourly = series.aggregate(Aggregation::Hourly)?;

// Detect anomalies
let anomalies = series.detect_anomalies(3.0)?;  // 3-sigma threshold

for anomaly in anomalies {
    println!("Anomaly at {}: {} calls (expected: {})",
        anomaly.timestamp, anomaly.actual, anomaly.expected);
}
```

</details>

---

## Configuration

### HdbscanConfig Parameters

| Parameter | Default | Description |
|-----------|---------|-------------|
| `min_cluster_size` | 5 | Minimum cluster size |
| `min_samples` | 3 | Core point threshold |
| `epsilon` | 0.0 | Distance threshold (0=auto) |
| `metric` | Euclidean | Distance metric |

### MarkovConfig Parameters

| Parameter | Default | Description |
|-----------|---------|-------------|
| `order` | 1 | Markov chain order |
| `smoothing` | 0.01 | Laplace smoothing factor |

## Algorithms

| Algorithm | Complexity | Use Case |
|-----------|------------|----------|
| HDBSCAN | O(n log n) | Clustering with noise |
| Markov Chain | O(n × s²) | Sequence modeling |
| Motif Discovery | O(n² × m) | Pattern finding |
| FFT Periodicity | O(n log n) | Rhythm detection |

## Links

- **Homepage**: [ruv.io]https://ruv.io
- **Repository**: [github.com/ruvnet/ruvector]https://github.com/ruvnet/ruvector
- **Crates.io**: [crates.io/crates/sevensense-analysis]https://crates.io/crates/sevensense-analysis
- **Documentation**: [docs.rs/sevensense-analysis]https://docs.rs/sevensense-analysis

## License

MIT License - see [LICENSE](../../LICENSE) for details.

---

*Part of the [7sense Bioacoustic Intelligence Platform](https://ruv.io) by rUv*