Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
clap = {version = "4.5.49", features = ["derive"]}
hegeltest = "0.32.4"
rand = "0.9.2"
rayon = "1.11.0"

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ Versions 0.1.* are reserved for bug fixes and performance improvements to existi
- Categorical design dimensions

## AI Policy
This project's AI policy is that no AI-written code is included in the core Rust module or in the python bindings. AI-written code may be present in the `python/` directory but is restricted to analysis. AI code is not used for benchmarking either correctness or speed.
This project's AI policy is that no AI-written code is included in the core Rust module or in the python bindings. AI-written code may be present in the `python/` directory but is restricted to analysis. AI code is not used for benchmarking either correctness or speed. AI may be used to assist writing tests or documentation, but writing and test quality are the responsibility of the author.

Gemini code review is used in development and any code suggestions must be human tested and are not auto-accepted. No AI-generated contributions will be accepted, neither will code contributions where the author cannot explain design choices and tradeoffs.
AI code review is used in development and any code suggestions must be human tested and are not auto-accepted. No AI-generated contributions will be accepted, neither will code contributions where the author cannot explain design choices and tradeoffs.

## Style
Rust formatting by `cargo fmt`. Python formatting by `ruff`. `flake8` used for PEP, line length not enforced in docstrings within reason.
Expand Down
13 changes: 9 additions & 4 deletions src/lhd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(test)]
use hegel::TestCase;
#[cfg(test)]
use hegel::generators as gs;
#[cfg(feature = "debug")]
use plotters::prelude::*;
#[cfg(feature = "pyo3-bindings")]
Expand Down Expand Up @@ -131,12 +135,13 @@ pub fn generate_lhd(n_samples: u64, n_dim: u64, rng: &mut StdRng) -> Vec<Vec<f64
lhd
}

#[test]
#[cfg(test)]
#[hegel::test(test_cases = 1000)]
/// Makes a simple test of a latin hypercube checking that,
/// for any interval in any dimension, there should be only one sample.
fn test_generate_lhd() {
let n_samples: u64 = 100;
let n_dim: u64 = 4;
fn test_generate_lhd(tc: TestCase) {
let n_samples: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(100));
let n_dim: u64 = tc.draw(gs::integers::<u64>().min_value(1).max_value(10));
let seed: u64 = 12345;
let mut rng: StdRng = SeedableRng::seed_from_u64(seed);
let design = generate_lhd(n_samples, n_dim, &mut rng);
Expand Down
72 changes: 61 additions & 11 deletions src/maximin_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[cfg(test)]
use crate::lhd::generate_lhd;
#[cfg(test)]
use hegel::TestCase;
#[cfg(test)]
use hegel::generators as gs;
#[cfg(feature = "pyo3-bindings")]
use pyo3::PyResult;
#[cfg(feature = "pyo3-bindings")]
Expand All @@ -10,6 +14,7 @@ use pyo3::prelude::*;
use rand::SeedableRng;
#[cfg(test)]
use rand::rngs::StdRng;

/// Calculate the L2 distance between two points, also known as the
/// Euclidean distance.
///
Expand Down Expand Up @@ -66,20 +71,65 @@ pub fn maximin_criterion(design: &Vec<Vec<f64>>) -> f64 {
min_distance
}

#[test]
#[cfg(test)]
#[hegel::test(test_cases = 1000)]
/// Test that the maximin criterion obeys simple properties across many iterations
fn test_maximin_criterion() {
let n_iterations: u64 = 1000;
let n_samples: u64 = 100;
let n_dim: u64 = 5;
fn test_maximin_criterion(tc: TestCase) {
let n_samples: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(100));
let n_dim: u64 = tc.draw(gs::integers::<u64>().min_value(1).max_value(10));
let seed: u64 = 12345;

let mut rng: StdRng = SeedableRng::seed_from_u64(seed);
for _i in 0..n_iterations {
let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maximin_metric: f64 = maximin_criterion(&lhd);
assert!(maximin_metric >= 0.0);
assert!(maximin_metric < f64::INFINITY)
}

let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maximin_metric: f64 = maximin_criterion(&lhd);
assert!(maximin_metric >= 0.0);
assert!(maximin_metric < f64::INFINITY)
}

#[cfg(test)]
#[hegel::test(test_cases = 100)]
/// Test that the maximin criterion is well-defined for a generated n×n design
/// whose entries are all bounded in [0, 1]
fn test_maximin_criterion_square_uniform(tc: TestCase) {
let n: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(50));
let n_index: usize = n as usize;

let row = gs::vecs(gs::floats::<f64>().min_value(0.0).max_value(1.0))
.min_size(n_index)
.max_size(n_index);
let design: Vec<Vec<f64>> = tc.draw(gs::vecs(row).min_size(n_index).max_size(n_index));

let maximin_metric: f64 = maximin_criterion(&design);
assert!(maximin_metric >= 0.0);
assert!(maximin_metric < f64::INFINITY);
}

#[cfg(test)]
#[hegel::test(test_cases = 100)]
/// Test that the L2 distance is symmetric and zero for identical points
fn test_calculate_l2_distance_symmetry(tc: TestCase) {
let n_dim: u64 = tc.draw(gs::integers::<u64>().min_value(1).max_value(10));
let n_index: usize = n_dim as usize;
let tolerance: f64 = 10.0_f64.powf(-9.0);

let point: Vec<f64> = tc.draw(
gs::vecs(gs::floats::<f64>().min_value(0.0).max_value(1.0))
.min_size(n_index)
.max_size(n_index),
);
let other: Vec<f64> = tc.draw(
gs::vecs(gs::floats::<f64>().min_value(0.0).max_value(1.0))
.min_size(n_index)
.max_size(n_index),
);

let distance_ab: f64 = calculate_l2_distance(&point, &other);
let distance_ba: f64 = calculate_l2_distance(&other, &point);
assert!((distance_ab - distance_ba).abs() < tolerance);

let distance_aa: f64 = calculate_l2_distance(&point, &point);
assert_eq!(distance_aa, 0.0);
}

#[test]
Expand Down
43 changes: 32 additions & 11 deletions src/maxpro_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[cfg(test)]
use crate::lhd::generate_lhd;
#[cfg(test)]
use hegel::TestCase;
#[cfg(test)]
use hegel::generators as gs;
#[cfg(feature = "pyo3-bindings")]
use pyo3::PyResult;
#[cfg(feature = "pyo3-bindings")]
Expand Down Expand Up @@ -98,20 +102,37 @@ pub fn py_maxpro_criterion(design: Vec<Vec<f64>>) -> PyResult<f64> {
Ok(maxpro_criterion(&design))
}

#[test]
#[cfg(test)]
#[hegel::test(test_cases = 1000)]
/// Ensures maxpro criterion is well-conditioned over randomized inputs
fn test_maxpro_criterion() {
let n_iterations: u64 = 1000;
let n_samples: u64 = 100;
let n_dim: u64 = 5;
fn test_maxpro_criterion(tc: TestCase) {
let n_samples: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(100));
let n_dim: u64 = tc.draw(gs::integers::<u64>().min_value(1).max_value(10));
let seed: u64 = 12345;
let mut rng: StdRng = SeedableRng::seed_from_u64(seed);
for _i in 0..n_iterations {
let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maxpro_metric: f64 = maxpro_criterion(&lhd);
assert!(maxpro_metric >= 0.0);
assert!(maxpro_metric < f64::INFINITY)
}

let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maxpro_metric: f64 = maxpro_criterion(&lhd);
assert!(maxpro_metric >= 0.0);
assert!(maxpro_metric < f64::INFINITY)
}

#[cfg(test)]
#[hegel::test(test_cases = 100)]
/// Test that the maxpro criterion is well-defined for a generated n×n design
/// whose entries are all bounded in [0, 1]
fn test_maxpro_criterion_square_uniform(tc: TestCase) {
let n: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(50));
let n_index: usize = n as usize;

let row = gs::vecs(gs::floats::<f64>().min_value(0.0).max_value(1.0))
.min_size(n_index)
.max_size(n_index);
let design: Vec<Vec<f64>> = tc.draw(gs::vecs(row).min_size(n_index).max_size(n_index));

let maxpro_metric: f64 = maxpro_criterion(&design);
assert!(maxpro_metric >= 0.0);
assert!(maxpro_metric < f64::INFINITY);
}

#[test]
Expand Down
47 changes: 24 additions & 23 deletions src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use crate::maximin_utils::maximin_criterion;
#[cfg(any(test, feature = "pyo3-bindings"))]
use crate::maxpro_utils::maxpro_criterion;
use core::f64;
#[cfg(test)]
use hegel::TestCase;
#[cfg(test)]
use hegel::generators as gs;
#[cfg(feature = "pyo3-bindings")]
use pyo3::PyResult;
#[cfg(feature = "pyo3-bindings")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "pyo3-bindings")]
use pyo3::prelude::*;

#[cfg(test)]
use rand::SeedableRng;
#[cfg(test)]
Expand Down Expand Up @@ -104,37 +107,35 @@ where
ordered_design
}

#[test]
#[cfg(test)]
#[hegel::test(test_cases = 1000)]
/// Test that the LHD has the same metric values before and after ordering
fn test_ordered_criteria_parity() {
let n_iterations: u64 = 1000;
let n_samples: u64 = 50;
let n_dim: u64 = 5;
fn test_ordered_criteria_parity(tc: TestCase) {
let n_samples: u64 = tc.draw(gs::integers::<u64>().min_value(2).max_value(100));
let n_dim: u64 = tc.draw(gs::integers::<u64>().min_value(1).max_value(10));
let seed: u64 = 12345;
let atol: f64 = 1e-10;

let mut rng: StdRng = SeedableRng::seed_from_u64(seed);
for _i in 0..n_iterations {
// Generate LHD
let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maxpro_metric_before: f64 = maxpro_criterion(&lhd);
let maximin_metric_before: f64 = maximin_criterion(&lhd);
// Generate LHD
let lhd = generate_lhd(n_samples, n_dim, &mut rng);
let maxpro_metric_before: f64 = maxpro_criterion(&lhd);
let maximin_metric_before: f64 = maximin_criterion(&lhd);

// Order Design
let ordered_design = order_design(lhd, maximin_criterion, false);
// Order Design
let ordered_design = order_design(lhd, maximin_criterion, false);

// Ensure parity
let maxpro_metric_after: f64 = maxpro_criterion(&ordered_design);
let maximin_metric_after: f64 = maximin_criterion(&ordered_design);
// Ensure parity
let maxpro_metric_after: f64 = maxpro_criterion(&ordered_design);
let maximin_metric_after: f64 = maximin_criterion(&ordered_design);

assert!(maxpro_metric_after >= 0.0);
assert!(maxpro_metric_after < f64::INFINITY);
assert!(maximin_metric_after >= 0.0);
assert!(maximin_metric_after < f64::INFINITY);
assert!(maxpro_metric_after >= 0.0);
assert!(maxpro_metric_after < f64::INFINITY);
assert!(maximin_metric_after >= 0.0);
assert!(maximin_metric_after < f64::INFINITY);

assert!((maximin_metric_before - maximin_metric_after).abs() < atol);
assert!((maxpro_metric_before - maxpro_metric_after).abs() < atol);
}
assert!((maximin_metric_before - maximin_metric_after).abs() < atol);
assert!((maxpro_metric_before - maxpro_metric_after).abs() < atol);
}

#[test]
Expand Down
Loading