arrowspace 0.26.2

Graph Wiring for embeddings using physical networks wiring. Provides graph analysis, vector search and a energy-distribution stats.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::{
    builder::ArrowSpaceBuilder,
    tests::test_data::{make_gaussian_blob, make_moons_hd},
};

use log::debug;

#[test]
fn test_builder_basic_clustering_with_synthetic_data() {
    // Test basic clustering functionality with high-dimensional moons data
    let items: Vec<Vec<f64>> = make_moons_hd(
        100,  // Moderate number of samples
        0.15, // Moderate noise
        0.4,  // Good separation
        10,   // 10-dimensional data
        42,   // Reproducible seed
    );

    debug!(
        "Generated {} items with {} features",
        items.len(),
        items[0].len()
    );

    let (_aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 5, 2, 2.0, None)
        .with_normalisation(true)
        .with_spectral(true)
        .build(items.clone());

    // Verify basic properties
    debug!("Graph has {} nodes", gl.nnodes);
}

#[test]
fn test_builder_laplacian_diagonal_properties() {
    // Test that Laplacian diagonal entries are non-negative and finite
    let items: Vec<Vec<f64>> = make_moons_hd(
        80,   // Sufficient samples
        0.12, // Low noise for stable structure
        0.5,  // Large separation
        8,    // 8 dimensions
        123,  // Seed
    );

    let (aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.2, 4, 2, 2.0, None)
        .with_normalisation(true)
        .build(items);

    // Check diagonal properties
    let csr = &gl.matrix;
    assert!(csr.is_csr(), "Expected CSR layout");

    let indptr = csr.indptr();
    let indices = csr.indices();
    let data = csr.data();

    for i in 0..aspace.n_clusters {
        let start = indptr.into_raw_storage()[i];
        let end = indptr.into_raw_storage()[i + 1];
        let mut found = false;
        let mut diag = 0.0_f64;

        for pos in start..end {
            let j = indices[pos];
            if j == i {
                diag = data[pos];
                found = true;
                break;
            }
        }

        assert!(
            found,
            "Diagonal entry at ({},{}) should exist in Laplacian",
            i, i
        );
        assert!(
            diag.is_finite(),
            "Diagonal at ({},{}) must be finite, got {}",
            i,
            i,
            diag
        );
        assert!(
            diag >= 0.0,
            "Diagonal at ({},{}) must be non-negative, got {}",
            i,
            i,
            diag
        );
    }

    debug!(
        "✓ All {} diagonal entries are non-negative and finite",
        aspace.n_clusters
    );
}

#[test]
fn test_builder_minimum_items() {
    // Test minimum viable dataset
    let items: Vec<Vec<f64>> = make_moons_hd(
        20,  // Small dataset
        0.1, // Low noise
        0.6, // High separation
        5,   // Low dimensions
        42,
    );

    let (aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.5, 3, 2, 2.0, None)
        .build(items.clone());

    assert!(
        aspace.n_clusters >= 1,
        "Should produce at least one cluster"
    );
    assert_eq!(gl.nnodes, items.len());

    debug!(
        "Minimum items test: {} clusters from {} items",
        aspace.n_clusters, 20
    );
}

#[test]
fn test_builder_scale_invariance_with_normalization() {
    // Test that normalization makes the graph structure scale-invariant
    let items: Vec<Vec<f64>> = make_moons_hd(60, 0.15, 0.4, 8, 0);

    // Build with original scale
    let (aspace1, gl1) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_normalisation(true) // Normalize for scale invariance
        .build(items.clone());

    // Scale all items by constant factor
    let scale_factor = 5.7;
    let items_scaled: Vec<Vec<f64>> = items
        .iter()
        .map(|item| item.iter().map(|&x| x * scale_factor).collect())
        .collect();

    // Build with scaled data
    let (aspace2, gl2) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_normalisation(true) // Normalize for scale invariance
        .build(items_scaled);

    // With normalization, cluster counts should be similar (allowing minor numerical differences)
    assert!(
        (aspace1.n_clusters as i32 - aspace2.n_clusters as i32).abs() <= 3,
        "Normalized clustering should be scale-invariant: {} vs {}",
        aspace1.n_clusters,
        aspace2.n_clusters
    );

    // Graph sizes should match
    assert_eq!(
        gl1.nnodes, gl2.nnodes,
        "Graph node counts should match under scaling"
    );

    debug!(
        "✓ Scale invariance verified: original={} clusters, scaled={} clusters",
        aspace1.n_clusters, aspace2.n_clusters
    );
}

#[test]
fn test_builder_laplacian_symmetry() {
    // Test that the Laplacian is symmetric (undirected graph)
    let items: Vec<Vec<f64>> = make_moons_hd(70, 0.18, 0.35, 9, 456);

    let (aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.25, 5, 2, 2.0, None)
        .with_normalisation(true)
        .build(items);

    let csr = &gl.matrix;
    assert!(csr.is_csr(), "Expected CSR layout");

    let n = aspace.n_clusters;
    let indptr = csr.indptr();
    let indices = csr.indices();
    let data = csr.data();
    let eps = 1e-10;

    let mut symmetric_pairs = 0;
    let mut total_edges = 0;

    for i in 0..n {
        let start = indptr.into_raw_storage()[i];
        let end = indptr.into_raw_storage()[i + 1];

        for p in start..end {
            let j = indices[p];
            if i == j {
                continue; // Skip diagonal
            }

            total_edges += 1;
            let vij = data[p];

            // Find symmetric entry (j, i)
            let js = indptr.into_raw_storage()[j];
            let je = indptr.into_raw_storage()[j + 1];
            let mut vji_opt: Option<f64> = None;

            for q in js..je {
                if indices[q] == i {
                    vji_opt = Some(data[q]);
                    break;
                }
            }

            if let Some(vji) = vji_opt {
                assert!(
                    (vij - vji).abs() <= eps * (1.0 + vij.abs().max(vji.abs())),
                    "Symmetric entries must match: L[{},{}]={:.6} vs L[{},{}]={:.6}",
                    i,
                    j,
                    vij,
                    j,
                    i,
                    vji
                );
                symmetric_pairs += 1;
            } else {
                panic!(
                    "Graph should be symmetric: found edge ({},{}) = {:.6} but missing ({},{})",
                    i, j, vij, j, i
                );
            }
        }
    }

    debug!(
        "✓ Verified symmetry for {} edge pairs (total {} edges)",
        symmetric_pairs, total_edges
    );
}

#[test]
fn test_builder_parameter_preservation() {
    // Test that graph parameters are correctly preserved through the builder
    let items: Vec<Vec<f64>> = make_moons_hd(50, 0.2, 0.4, 7, 321);

    let (_, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(
            0.123,       // eps
            7,           // k
            3,           // topk
            3.5,         // p
            Some(0.456), // sigma
        )
        .with_normalisation(false)
        .build(items);

    // Verify all parameters are preserved
    assert_eq!(gl.graph_params.eps, 0.123, "eps must match");
    assert_eq!(gl.graph_params.k, 7, "k must match");
    assert_eq!(gl.graph_params.topk, 3 + 1, "topk must match");
    assert_eq!(gl.graph_params.p, 3.5, "p must match");
    assert_eq!(gl.graph_params.sigma, Some(0.456), "sigma must match");
    assert_eq!(
        gl.graph_params.normalise, false,
        "normalise flag must match"
    );

    debug!("✓ All graph parameters correctly preserved");
}

#[test]
fn test_builder_with_different_dimensions() {
    // Test builder works across different dimensionalities
    let test_cases = vec![
        (50, 3, "low-dimensional"),
        (60, 10, "medium-dimensional"),
        (70, 25, "high-dimensional"),
    ];

    for (n_samples, dims, desc) in test_cases {
        let items: Vec<Vec<f64>> = make_moons_hd(
            n_samples,
            0.15,
            0.4,
            dims,
            42 + dims as u64, // Vary seed by dimension
        );

        let (aspace, gl) = ArrowSpaceBuilder::default()
            .with_lambda_graph(0.3, 5, 2, 2.0, None)
            .with_normalisation(true)
            .with_spectral(true)
            .with_sparsity_check(false)
            .build(items);

        assert!(aspace.n_clusters > 0, "{}: Should produce clusters", desc);
        assert!(
            aspace.nfeatures == dims,
            "{}: Features should be {}",
            desc,
            dims
        );

        debug!(
            "{}: {} clusters, {} features, {} nodes",
            desc, aspace.n_clusters, aspace.nfeatures, gl.nnodes
        );
    }
}

#[test]
fn test_builder_spectral_laplacian_shape() {
    // Test that spectral Laplacian has correct shape (FxF)
    let items: Vec<Vec<f64>> = make_moons_hd(90, 0.16, 0.38, 12, 555);

    // Build WITHOUT spectral
    let (aspace_no_spectral, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.25, 4, 2, 2.0, None)
        .with_spectral(false)
        .build(items.clone());

    // Build WITH spectral
    let (aspace_spectral, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.25, 4, 2, 2.0, None)
        .with_spectral(true)
        .build(items.clone());

    // Without spectral, signals should be empty
    assert_eq!(
        aspace_no_spectral.signals.shape(),
        (0, 0),
        "Signals should be empty when spectral is disabled"
    );

    // With spectral, signals should be FxF where F is number of features
    let expected_dim = aspace_spectral.nfeatures;
    assert_eq!(
        aspace_spectral.signals.shape(),
        (expected_dim, expected_dim),
        "Signals should be {}x{} (feature-by-feature Laplacian)",
        expected_dim,
        expected_dim
    );

    debug!(
        "✓ Spectral Laplacian shape: {:?}",
        aspace_spectral.signals.shape()
    );
}

#[test]
fn test_builder_lambda_values_are_nonnegative() {
    // Test that all lambda values (spectral scores) are non-negative
    let items: Vec<Vec<f64>> = make_moons_hd(100, 0.2, 0.35, 11, 999);

    let (aspace, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 5, 2, 2.0, None)
        .with_normalisation(true)
        .with_spectral(true)
        .build(items);

    let lambdas = aspace.lambdas();

    for (i, &lam) in lambdas.iter().enumerate() {
        assert!(
            lam >= 0.0,
            "Lambda at index {} should be non-negative, got {:.6}",
            i,
            lam
        );
    }

    let min_lambda = lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
    let max_lambda = lambdas.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

    debug!(
        "✓ All {} lambdas are non-negative: min={:.6}, max={:.6}",
        lambdas.len(),
        min_lambda,
        max_lambda
    );
}

#[test]
fn test_builder_with_high_noise() {
    // Generate 3 Gaussian blobs with noise=0.9 (moderate overlap)
    let items = make_gaussian_blob(300, 0.9);

    let (aspace, _gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.4, 6, 3, 2.0, None)
        .with_normalisation(true)
        .build(items);

    // Note: With noise=0.9, the optimal K heuristic may conservatively
    // choose K=2 instead of K=3 due to cluster overlap. This is correct
    // behavior - the algorithm prefers under-clustering to over-clustering.
    assert!(
        aspace.n_clusters >= 2,
        "Should produce valid clusters even with high noise, got {}",
        aspace.n_clusters
    );

    debug!(
        "✓ Found {} clusters (conservative estimate for noisy data)",
        aspace.n_clusters
    );
}

#[test]
fn test_builder_normalization_effects() {
    // Compare normalized vs unnormalized builds
    let items: Vec<Vec<f64>> = make_moons_hd(75, 0.14, 0.45, 8, 654);

    // Build with normalization
    let (aspace_norm, gl_norm) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 5, 2, 2.0, None)
        .with_normalisation(true)
        .build(items.clone());

    // Build without normalization
    let (aspace_raw, gl_raw) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 5, 2, 2.0, None)
        .with_normalisation(false)
        .build(items);

    debug!("Normalized: {} clusters", aspace_norm.n_clusters);
    debug!("Raw (τ-mode): {} clusters", aspace_raw.n_clusters);

    // Parameters should be correctly set
    assert_eq!(gl_norm.graph_params.normalise, true);
    assert_eq!(gl_raw.graph_params.normalise, false);

    // Both should produce valid results
    assert!(aspace_norm.n_clusters > 0);
    assert!(aspace_raw.n_clusters > 0);

    debug!("✓ Both normalization modes produce valid clusterings");
}