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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use crate::builder::ConfigValue;
use crate::core::ArrowSpace;
use crate::reduction::ImplicitProjection;
use crate::{
    builder::ArrowSpaceBuilder,
    graph::GraphLaplacian,
    sampling::SamplerType,
    search::taumode::TauMode,
    tests::test_data::{make_gaussian_blob, make_gaussian_hd, make_moons_hd},
};

use log::debug;
use std::collections::HashMap;

/// Helper to compare two GraphLaplacian matrices for equality
fn laplacian_eq(a: &GraphLaplacian, b: &GraphLaplacian, eps: f64) -> bool {
    if a.matrix.shape() != b.matrix.shape() {
        return false;
    }

    let (r, c) = a.matrix.shape();
    for i in 0..r {
        for j in 0..c {
            let ai = *a.matrix.get(i, j).unwrap_or(&0.0);
            let bj = *b.matrix.get(i, j).unwrap_or(&0.0);
            if (ai - bj).abs() > eps {
                return false;
            }
        }
    }
    true
}

/// Helper to collect diagonal of the Laplacian matrix as Vec<f64>
fn diag_vec(gl: &GraphLaplacian) -> Vec<f64> {
    let (n, _) = gl.matrix.shape();
    (0..n).map(|i| *gl.matrix.get(i, i).unwrap()).collect()
}

#[allow(dead_code)]
fn l2_norm(x: &[f64]) -> f64 {
    x.iter().map(|&v| v * v).sum::<f64>().sqrt()
}

#[test]
fn test_builder_direction_vs_magnitude_sensitivity() {
    // Construct vectors where two have the same direction but vastly different magnitudes
    let items = make_gaussian_blob(99, 0.5);

    // Build with normalisation=true (cosine-like, scale-invariant)
    let (aspace_norm, gl_norm) = ArrowSpaceBuilder::default()
        .with_lambda_graph(1.0, 3, 2, 2.0, Some(0.25))
        .with_normalisation(true)
        .with_spectral(true)
        .build(items.clone());

    // Build with normalisation=false (Ï„-mode: magnitude-sensitive)
    let (aspace_tau, gl_tau) = ArrowSpaceBuilder::default()
        .with_lambda_graph(1.0, 3, 2, 2.0, Some(0.25))
        .with_normalisation(false)
        .with_spectral(true)
        .build(items.clone());

    // Ï„-mode should differ from normalised graph because it is magnitude-sensitive
    let matrices_equal = laplacian_eq(&gl_norm, &gl_tau, 1e-12);
    assert!(
        !matrices_equal,
        "Ï„-mode should differ from normalised graph due to magnitude sensitivity"
    );

    // Lambda distributions should also differ
    let lambdas_norm = aspace_norm.lambdas();
    let lambdas_tau = aspace_tau.lambdas();

    debug!(
        "Normalized lambdas (first 3): {:?}",
        &lambdas_norm[..3.min(lambdas_norm.len())]
    );
    debug!(
        "Tau lambdas (first 3): {:?}",
        &lambdas_tau[..3.min(lambdas_tau.len())]
    );
}

#[test]
fn test_builder_normalisation_flag_is_preserved() {
    // Verify that normalisation flag is properly propagated through the builder
    let items = make_moons_hd(99, 0.1, 0.5, 3, 123);

    let (_aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.25, 2, 1, 2.0, None)
        .with_normalisation(false)
        .build(items);

    assert_eq!(
        gl.graph_params.normalise, false,
        "normalise flag must be preserved"
    );
}

#[test]
fn test_builder_clustering_produces_valid_assignments() {
    // Test that the builder produces valid cluster assignments
    let items = make_moons_hd(99, 0.05, 1.5, 3, 456);

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

    debug!("Assignments: {:?}", aspace.cluster_assignments);

    // Verify all items are assigned
    let assigned_count = aspace
        .cluster_assignments
        .iter()
        .filter(|x| x.is_some())
        .count();
    assert!(
        assigned_count > 0,
        "At least some items should be assigned to clusters"
    );
}

#[test]
fn test_builder_spectral_laplacian_computation() {
    // Test that spectral Laplacian is computed when requested
    let items = make_moons_hd(4, 0.12, 0.4, 5, 789);

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

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

    debug!(
        "No spectral - signals shape: {:?}",
        aspace_no_spectral.signals.shape()
    );
    debug!(
        "With spectral - signals shape: {:?}",
        aspace_spectral.signals.shape()
    );

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

    // When spectral is enabled, signals should be populated (FxF matrix)
    assert_ne!(
        aspace_spectral.signals.shape(),
        (0, 0),
        "Signals should be populated when spectral computation is enabled"
    );
}

#[test]
fn test_builder_lambda_computation_with_different_tau_modes() {
    let items = make_moons_hd(3, 0.15, 0.35, 4, 321);

    // Build with Median tau mode
    let (aspace_median, _) = ArrowSpaceBuilder::default()
        .with_synthesis(TauMode::Median)
        .with_lambda_graph(0.2, 2, 1, 2.0, None)
        .with_inline_sampling(None)
        .build(items.clone());

    // Build with Max tau mode
    let (aspace_fixed, _) = ArrowSpaceBuilder::default()
        .with_synthesis(TauMode::Fixed(0.5))
        .with_lambda_graph(0.2, 2, 1, 2.0, None)
        .with_inline_sampling(None)
        .build(items.clone());

    let lambdas_median = aspace_median.lambdas();
    let lambdas_fixed = aspace_fixed.lambdas();

    debug!("Median tau lambdas: {:?}", lambdas_median);
    debug!("Max tau lambdas: {:?}", lambdas_fixed);

    // Lambdas should differ between tau modes
    let mut differences = 0;
    for (m, mx) in lambdas_median.iter().zip(lambdas_fixed.iter()) {
        if (m - mx).abs() > 1e-10 {
            differences += 1;
        }
    }

    assert!(
        differences > 0,
        "Different tau modes should produce different lambda values"
    );
}

#[test]
fn test_builder_with_normalized_vs_unnormalized_items() {
    // Test how normalization affects clustering and spectral properties
    let items = make_moons_hd(4, 0.18, 0.4, 6, 654);

    // Create unnormalized items with different scales
    let scales = vec![1.0, 3.0, 0.5, 2.5];
    let items_unnormalized: Vec<Vec<f64>> = items
        .iter()
        .zip(scales.iter())
        .map(|(item, &scale)| item.iter().map(|x| x * scale).collect())
        .collect();

    // Build with normalized data
    let (aspace_norm, gl_norm) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.2, 2, 1, 2.0, None)
        .with_normalisation(true)
        .with_spectral(true)
        .with_inline_sampling(None)
        .build(items.clone());

    // Build with unnormalized data (no normalization flag)
    let (aspace_unnorm, gl_unnorm) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.2, 2, 1, 2.0, None)
        .with_normalisation(false)
        .with_spectral(true)
        .with_inline_sampling(None)
        .build(items_unnormalized);

    debug!("=== SPECTRAL ANALYSIS ===");
    let lambdas_norm = aspace_norm.lambdas();
    let lambdas_unnorm = aspace_unnorm.lambdas();

    debug!(
        "Normalized lambdas: {:?}",
        &lambdas_norm[..3.min(lambdas_norm.len())]
    );
    debug!(
        "Unnormalized lambdas: {:?}",
        &lambdas_unnorm[..3.min(lambdas_unnorm.len())]
    );

    // Diagonal elements should differ due to different graph structure
    let d_norm = diag_vec(&gl_norm);
    let d_unnorm = diag_vec(&gl_unnorm);

    debug!("Normalized diagonals: {:?}", &d_norm[..3.min(d_norm.len())]);
    debug!(
        "Unnormalized diagonals: {:?}",
        &d_unnorm[..3.min(d_unnorm.len())]
    );

    // The graphs should be different due to magnitude sensitivity
    assert!(
        !laplacian_eq(&gl_norm, &gl_unnorm, 1e-10),
        "Normalized and unnormalized builds should produce different graphs"
    );
}

#[test]
fn test_builder_with_inline_sampling() {
    // Test builder with inline sampling enabled
    let items = make_gaussian_blob(100, 0.5);

    let (_aspace_sampling, _gl_sampling) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_inline_sampling(Some(SamplerType::DensityAdaptive(0.5)))
        .build(items.clone());

    let (_aspace_no_sampling, _gl_no_sampl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_inline_sampling(Some(SamplerType::DensityAdaptive(0.5)))
        .build(items);
}

#[test]
fn test_builder_dimensionality_reduction() {
    // Test builder with dimensionality reduction enabled
    let items = make_moons_hd(50, 0.15, 0.35, 128, 111);

    let (aspace_reduced, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_dims_reduction(true, Some(0.3))
        .with_sparsity_check(false)
        .build(items.clone());

    let (aspace_full, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 4, 2, 2.0, None)
        .with_dims_reduction(false, None)
        .with_sparsity_check(false)
        .build(items);

    debug!("Original dimension: {}", aspace_full.nfeatures);
    debug!("Reduced dimension: {:?}", aspace_reduced.reduced_dim);

    if let Some(reduced_dim) = aspace_reduced.reduced_dim {
        assert!(
            reduced_dim < aspace_full.nfeatures,
            "Reduced dimension should be less than original"
        );
        assert!(
            aspace_reduced.projection_matrix.is_some(),
            "Projection matrix should be present when reduction is enabled"
        );
    }
}

#[test]
fn test_builder_lambda_statistics() {
    // Test that lambda statistics show reasonable variance using high-dimensional moons data
    // Use make_moons_hd with high noise to create natural clusters with distinct spectral properties

    let items: Vec<Vec<f64>> = make_moons_hd(
        200, // Sufficient samples for meaningful statistics
        0.3, // High noise for variance - standard deviation of Gaussian noise
        0.5, // Moderate separation between moons
        40,  // High dimensionality to spread variance
        768, // Fixed seed for reproducibility
    );

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

    // Build ArrowSpace with spectral computation enabled
    // Use parameters that create a well-connected graph for meaningful eigenvalues
    let (aspace, gl) = ArrowSpaceBuilder::default()
        .with_lambda_graph(
            0.5,  // Larger eps for connectivity across noise
            6,    // More neighbors to capture local structure
            3,    // Keep top-3 neighbors
            2.0,  // Quadratic kernel
            None, // Auto-compute sigma
        )
        .with_sparsity_check(false)
        .build(items);

    debug!("Graph has {} nodes", gl.nnodes);

    // Extract lambda statistics
    let lambdas = aspace.lambdas();

    let min = lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
    let max = lambdas.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
    let mean = lambdas.iter().sum::<f64>() / lambdas.len() as f64;

    // Compute standard deviation for variance measure
    let variance = lambdas.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / lambdas.len() as f64;
    let std_dev = variance.sqrt();

    debug!("=== LAMBDA DISTRIBUTION ===");
    debug!("Min:     {:.6}", min);
    debug!("Max:     {:.6}", max);
    debug!("Mean:    {:.6}", mean);
    debug!("Std Dev: {:.6}", std_dev);
    debug!("Range:   {:.6}", max - min);

    // Show first few lambdas for inspection
    debug!("First 5 lambdas: {:?}", &lambdas[..5.min(lambdas.len())]);

    // All lambdas should be non-negative (spectral property)
    assert!(
        min >= 0.0,
        "All lambdas should be non-negative, got min={}",
        min
    );

    // Should have meaningful variance - the noise in make_moons_hd ensures this
    // With high noise (0.3), points within each moon have varying distances to centroids,
    // creating different local graph structures and thus different lambda values
    assert!(
        max > min,
        "Lambdas should have some variance: max={:.6}, min={:.6}",
        max,
        min
    );

    // Stronger variance test: range should be significant relative to mean
    let relative_range = (max - min) / mean.max(1e-10);
    assert!(
        relative_range > 0.1,
        "Lambda range should be at least 10% of mean for high-variance data: \
         range={:.6}, mean={:.6}, relative={:.6}",
        max - min,
        mean,
        relative_range
    );

    // Standard deviation should indicate spread
    assert!(
        std_dev > 1e-6,
        "Lambda standard deviation should indicate spread: std_dev={:.6}",
        std_dev
    );

    debug!("✓ Lambda statistics show expected variance from noisy moon dataset");
}

#[test]
fn test_builder_cluster_radius_impact() {
    // Test how cluster radius affects clustering
    let items = make_gaussian_blob(99, 0.5);

    // This test verifies that the auto-computed cluster parameters
    // produce reasonable clustering behavior
    let (aspace, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 3, 2, 2.0, None)
        .with_seed(42)
        .build(items);

    // Radius should be positive
    assert!(
        aspace.cluster_radius > 0.0,
        "Cluster radius should be positive"
    );
}

#[test]
fn test_empty_with_projection_path() {
    crate::tests::init();
    let mut proj_data = HashMap::new();
    proj_data.insert(
        "pj_mtx_original_dim".to_string(),
        ConfigValue::OptionUsize(Some(384)),
    );
    proj_data.insert(
        "pj_mtx_reduced_dim".to_string(),
        ConfigValue::OptionUsize(Some(91)),
    );
    proj_data.insert(
        "pj_mtx_seed".to_string(),
        ConfigValue::OptionU64(Some(123456789)),
    );
    proj_data.insert("extra_reduced_dim".to_string(), ConfigValue::Bool(false));

    let nrows = 10_000;
    let ncols = 384;

    let aspace = ArrowSpace::empty_with_projection(proj_data, nrows, ncols);

    // Basic shape
    assert_eq!(aspace.nitems, nrows);
    assert_eq!(aspace.nfeatures, ncols);

    // Projection is set correctly
    let proj = aspace
        .projection_matrix
        .as_ref()
        .expect("projection_matrix must be Some");
    assert_eq!(
        *proj,
        ImplicitProjection {
            original_dim: 384,
            reduced_dim: 91,
            seed: 123456789,
        }
    );

    // reduced_dim and extra_reduced_dim are consistent with proj_data
    assert_eq!(aspace.reduced_dim, Some(91));
    assert_eq!(aspace.extra_reduced_dim, false);
}

#[test]
#[should_panic(expected = "Reconstructing with extra dim reduction is not implemented yet")]
fn test_empty_with_projection_panics_on_extra_reduced_dim_true() {
    let mut proj_data = HashMap::new();
    proj_data.insert("pj_mtx_original_dim".to_string(), ConfigValue::Usize(384));
    proj_data.insert("pj_mtx_reduced_dim".to_string(), ConfigValue::Usize(91));
    proj_data.insert("pj_mtx_seed".to_string(), ConfigValue::U64(123456789));
    // This should trigger the assertion inside `empty_with_projection`.
    proj_data.insert("extra_reduced_dim".to_string(), ConfigValue::Bool(true));

    let nrows = 10_000;
    let ncols = 384;

    let _ = ArrowSpace::empty_with_projection(proj_data, nrows, ncols);
}

#[test]
fn test_empty_with_projection_none_path() {
    let mut proj_data = HashMap::new();
    proj_data.insert(
        "pj_mtx_original_dim".to_string(),
        ConfigValue::OptionUsize(None),
    );
    proj_data.insert(
        "pj_mtx_reduced_dim".to_string(),
        ConfigValue::OptionUsize(None),
    );
    proj_data.insert("pj_mtx_seed".to_string(), ConfigValue::OptionUsize(None));
    proj_data.insert("extra_reduced_dim".to_string(), ConfigValue::Bool(false));

    let nrows = 10_000;
    let ncols = 384;

    let aspace = ArrowSpace::empty_with_projection(proj_data, nrows, ncols);

    assert_eq!(aspace.projection_matrix, None);
    assert_eq!(aspace.reduced_dim, None);
    assert_eq!(aspace.extra_reduced_dim, false);
}

#[test]
fn test_arrowspace_config_typed_without_projection() {
    let items = make_gaussian_blob(99, 0.5);
    let (aspace, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 3, 2, 2.0, None)
        .with_seed(42)
        .build(items);

    // Extract config
    let config = aspace.arrowspace_config_typed();

    // Verify basic dimensions
    assert_eq!(config.get("nitems").unwrap().as_usize().unwrap(), 99);
    assert_eq!(config.get("nfeatures").unwrap().as_usize().unwrap(), 10);

    // Verify no projection
    assert_eq!(config.get("pj_mtx_original_dim").unwrap().as_usize(), None);
    assert_eq!(config.get("pj_mtx_reduced_dim").unwrap().as_usize(), None);
    assert_eq!(config.get("pj_mtx_seed").unwrap().as_u64(), None);
    assert_eq!(
        config.get("extra_reduced_dim").unwrap().as_bool().unwrap(),
        false
    );

    // Verify tau mode is present
    assert!(config.contains_key("taumode"));

    // Verify clustering params
    assert!(config.contains_key("n_clusters"));
    assert!(config.contains_key("cluster_radius"));
}

#[test]
fn test_arrowspace_config_typed_with_projection() {
    let items = make_gaussian_hd(99, 0.5);
    let (aspace, _) = ArrowSpaceBuilder::default()
        .with_lambda_graph(0.3, 3, 2, 2.0, None)
        .with_seed(42)
        .with_dims_reduction(true, Some(0.25))
        .build(items);

    // Extract config
    let config = aspace.arrowspace_config_typed();

    // Verify basic dimensions
    assert_eq!(config.get("nitems").unwrap().as_usize().unwrap(), 99);
    assert_eq!(config.get("nfeatures").unwrap().as_usize().unwrap(), 100);

    // Verify projection parameters are present and correct
    assert_eq!(
        config
            .get("pj_mtx_original_dim")
            .unwrap()
            .as_usize()
            .unwrap(),
        100
    );
    assert_eq!(
        config
            .get("pj_mtx_reduced_dim")
            .unwrap()
            .as_usize()
            .unwrap(),
        50
    );
    assert_eq!(config.get("pj_mtx_seed").unwrap().as_u64().unwrap(), 42);

    // extra_reduced_dim should be false for Eigen mode
    assert_eq!(
        config.get("extra_reduced_dim").unwrap().as_bool().unwrap(),
        false
    );

    // Verify tau mode
    let taumode = config.get("taumode").unwrap();
    match taumode {
        ConfigValue::TauMode(_) => {} // Pass if it's a TauMode variant
        _ => panic!("Expected TauMode variant"),
    }

    // Verify clustering params exist
    assert!(config.get("n_clusters").is_some());
    assert!(config.get("cluster_radius").is_some());
}