use super::*;
#[test]
fn all_alpha_factories_check_the_same_coefficient_range() {
let values = AlphaValues::new("actual", [0.0, 0.5, 128.0 / 255.0, 1.0]).unwrap();
assert_eq!(values.values(), &[0.0, 0.5, 128.0 / 255.0, 1.0]);
assert_ne!(values.get(1), values.get(2));
let source = NumericValues::new("confidence", [0.0, 0.5, 1.0]);
let categories = CategoryValues::new("class", ["A", "B"]).unwrap();
for invalid in [-0.1, 1.01, 195.0, f64::NAN, f64::INFINITY] {
assert!(AlphaValues::new("invalid", [invalid]).is_err());
assert!(AlphaValues::try_from(NumericValues::new("invalid", [invalid])).is_err());
assert!(AlphaValues::linear(source.clone(), (0.0, 1.0), (0.0, invalid)).is_err());
assert!(AlphaValues::continuous(source.clone(), (0.0, 1.0), |_| invalid).is_err());
assert!(AlphaValues::threshold(source.clone(), [], [invalid]).is_err());
assert!(AlphaValues::categorical(categories.clone(), |_| invalid).is_err());
}
assert!(AlphaValues::try_from(NumericValues::nullable("missing", [None])).is_err());
assert!(!AlphaValues::categorical(categories.clone(), |_| 0.5)
.unwrap()
.same_declaration(&AlphaValues::categorical(categories, |_| 128.0 / 255.0).unwrap()));
}
#[test]
fn linear_alpha_preserves_coefficients_without_quantization() {
let source = NumericValues::new("confidence", [-1.0, 0.0, 0.25, 0.5, 0.75, 1.0, 2.0]);
let alpha = AlphaValues::linear(source.clone(), (0.0, 1.0), (0.0, 1.0)).unwrap();
assert_eq!(alpha.data.values, [0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0]);
assert_eq!(alpha.semantics(), AlphaSemantics::Continuous);
assert_eq!(
AlphaValues::linear(source.clone(), (0.0, 1.0), (1.0, 0.0))
.unwrap()
.data
.values,
[1.0, 1.0, 0.75, 0.5, 0.25, 0.0, 0.0],
);
assert_eq!(
AlphaValues::linear(source, (0.0, 1.0), (0.125, 0.125))
.unwrap()
.data
.values,
[0.125; 7],
);
let middle = NumericValues::new("middle", [0.5; 2]);
assert_eq!(
AlphaValues::linear(middle.clone(), (0.0, 1.0), (0.0, 0.5))
.unwrap()
.data
.values,
[0.25, 0.25]
);
assert_eq!(
AlphaValues::linear(middle, (0.0, 1.0), (0.25, 0.75))
.unwrap()
.data
.values,
[0.5, 0.5]
);
let extreme = AlphaValues::linear(
NumericValues::new(
"extreme",
[-f64::MAX, -f64::MAX / 2.0, 0.0, f64::MAX / 2.0, f64::MAX],
),
(-f64::MAX, f64::MAX),
(0.0, 1.0),
)
.unwrap();
assert_eq!(extreme.data.values, [0.0, 0.25, 0.5, 0.75, 1.0]);
let tiny = f64::from_bits(1);
let subnormal = AlphaValues::linear(
NumericValues::new("tiny", [0.0, tiny, f64::from_bits(2)]),
(0.0, f64::from_bits(2)),
(0.0, 1.0),
)
.unwrap();
assert_eq!(subnormal.data.values, [0.0, 0.5, 1.0]);
let adjacent = AlphaValues::linear(
NumericValues::new("adjacent", [1.0, 1.0_f64.next_up()]),
(1.0, 1.0_f64.next_up()),
(0.125, 0.875),
)
.unwrap();
assert_eq!(adjacent.data.values, [0.125, 0.875]);
assert_eq!(
adjacent.examples(),
&[(1.0, 0.125), (1.0_f64.next_up(), 0.875)]
);
assert_ne!(
adjacent.declaration.labels[0],
adjacent.declaration.labels[1]
);
for domain in [
(1.0, 1.0),
(2.0, 1.0),
(f64::NAN, 1.0),
(0.0, f64::INFINITY),
] {
assert!(matches!(
AlphaValues::linear(NumericValues::new("invalid", []), domain, (0.0, 1.0)),
Err(Error::Data(_))
));
}
for bad in [
None,
Some(f64::NAN),
Some(f64::INFINITY),
Some(f64::NEG_INFINITY),
] {
let error = AlphaValues::linear(
NumericValues::nullable("invalid", [Some(0.5), bad]),
(0.0, 1.0),
(0.0, 1.0),
)
.unwrap_err();
assert!(matches!(error, Error::Data(_)));
assert!(error.to_string().contains("index 1"), "{error}");
}
let fraction = crate::numeric::mapping::normalized_fraction;
assert_eq!(
fraction((-0.0, 0.0), 10.0).unwrap().to_bits(),
0.5_f64.to_bits()
);
assert!(fraction((0.0, 1.0), f64::NAN).is_err());
assert!(fraction((1.0, 0.0), 0.5).is_err());
}
#[test]
fn log_alpha_maps_positive_coordinates_without_broadening_or_losing_clipped_members() {
let alpha = AlphaValues::log10(
NumericValues::new(
"quality",
[0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0, 100_000.0],
),
(1.0, 10000.0),
(0.0, 1.0),
)
.unwrap();
assert_eq!(alpha.data.values, [0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0]);
assert_eq!(
alpha
.get_numeric_source()
.unwrap()
.iter()
.collect::<Vec<_>>(),
[0.1_f64, 1.0, 10.0, 100.0, 1000.0, 10000.0, 100_000.0].map(Some)
);
assert_eq!(
alpha.get_mapping(),
&AlphaMapping::Log10 {
domain: [1.0, 10000.0],
range: [0.0, 1.0],
}
);
assert_eq!(
alpha
.examples()
.iter()
.map(|(value, _)| *value)
.collect::<Vec<_>>(),
[1.0, 2500.75, 5000.5, 7500.25, 10000.0]
);
let extreme = AlphaValues::log10(
NumericValues::new("extreme", [f64::from_bits(1), f64::MAX]),
(f64::from_bits(1), f64::MAX),
(1.0, 0.0),
)
.unwrap();
assert_eq!(extreme.data.values, [1.0, 0.0]);
for domain in [(0.0, 1.0), (-1.0, 10.0), (f64::MAX.next_down(), f64::MAX)] {
assert!(matches!(
AlphaValues::log10(NumericValues::new("bad", []), domain, (0.0, 1.0)),
Err(Error::Data(_))
));
}
for bad in [
None,
Some(-1.0),
Some(0.0),
Some(-0.0),
Some(f64::NAN),
Some(f64::INFINITY),
] {
let error = AlphaValues::log10(
NumericValues::nullable("bad", [Some(10.0), bad]),
(1.0, 100.0),
(0.0, 1.0),
)
.unwrap_err();
assert!(matches!(error, Error::Data(_)));
assert!(error.to_string().contains("index 1"), "{error}");
}
}
#[test]
fn custom_continuous_materializes_each_sorted_clamped_coordinate_and_truthful_example_once() {
let mut calls = Vec::new();
let alpha = AlphaValues::continuous(
NumericValues::new("reliability", [2.0, 0.125, -0.0, -1.0, 0.125, 0.5, 0.0]),
(0.0, 1.0),
|value| {
calls.push(value.to_bits());
if value < 0.5 {
0.0
} else {
230.0 / 255.0
}
},
)
.unwrap();
assert_eq!(
calls,
[0.0_f64, 0.125, 0.25, 0.5, 0.75, 1.0].map(f64::to_bits)
);
assert_eq!(
alpha.data.values,
[230.0 / 255.0, 0.0, 0.0, 0.0, 0.0, 230.0 / 255.0, 0.0]
);
let source = alpha.get_numeric_source().unwrap();
assert_eq!(
source
.iter()
.map(|value| value.unwrap().to_bits())
.collect::<Vec<_>>(),
[2.0_f64, 0.125, -0.0, -1.0, 0.125, 0.5, 0.0].map(f64::to_bits)
);
let AlphaMapping::Continuous { domain, evaluated } = alpha.get_mapping() else {
panic!("custom declaration")
};
assert_eq!(
domain.map(f64::to_bits),
[0.0_f64.to_bits(), 1.0_f64.to_bits()]
);
assert_eq!(
evaluated
.iter()
.map(|(key, _)| key.to_bits())
.collect::<Vec<_>>(),
calls
);
assert_eq!(alpha.declaration.labels, ["0", "0.25", "0.5", "0.75", "1"]);
assert_eq!(
alpha.examples(),
&[
(0., 0.),
(0.25, 0.),
(0.5, 230. / 255.),
(0.75, 230. / 255.),
(1., 230. / 255.)
]
);
let selected = alpha.select([2, 0, 2]).unwrap();
assert_eq!(selected.get_mapping(), alpha.get_mapping());
assert_eq!(
selected
.get_numeric_source()
.unwrap()
.iter()
.map(|v| v.unwrap().to_bits())
.collect::<Vec<_>>(),
[-0.0_f64, 2.0, -0.0].map(f64::to_bits),
);
assert_eq!(calls.len(), 6);
for source in [
NumericValues::new("bad", [0.25, f64::NAN]),
NumericValues::nullable("bad", [Some(0.25), None]),
] {
let error = AlphaValues::continuous(source, (0.0, 1.0), |_| {
calls.push(99);
1.0
})
.unwrap_err();
assert!(matches!(error, Error::Data(_)));
assert!(error.to_string().contains("index 1"), "{error}");
}
assert!(
AlphaValues::continuous(NumericValues::new("bad", [0.25]), (1.0, 1.0), |_| {
calls.push(99);
1.0
})
.is_err()
);
assert_eq!(calls.len(), 6);
let mut empty_calls = Vec::new();
let empty = AlphaValues::continuous(NumericValues::new("empty", []), (0.0, 1.0), |x| {
empty_calls.push(x.to_bits());
0.0
})
.unwrap();
assert!(empty.is_empty());
assert_eq!(
empty_calls,
[0.0_f64, 0.25, 0.5, 0.75, 1.0].map(f64::to_bits)
);
assert_eq!(empty.semantics(), AlphaSemantics::Continuous);
}
#[test]
fn categorical_alpha_owns_first_observed_raw_categories_and_rejects_nulls_before_callbacks() {
let mut calls = Vec::new();
let source = CategoryValues::new("method", ["A B", "é", "A B", "e\u{301}", "A B"]).unwrap();
let alpha = AlphaValues::categorical(source, |raw| {
calls.push(raw.to_owned());
match raw {
"A B" => 0.0,
"é" => 170.0 / 255.0,
"A B" => 90.0 / 255.0,
_ => 1.0,
}
})
.unwrap();
assert_eq!(calls, ["A B", "é", "A B", "e\u{301}"]);
assert_eq!(alpha.semantics(), AlphaSemantics::Categorical);
assert_eq!(
alpha.data.values,
[0.0, 170.0 / 255.0, 90.0 / 255.0, 1.0, 0.0]
);
let original = alpha.get_category_source().unwrap().get_strings().unwrap();
assert_eq!(
original.iter().collect::<Vec<_>>(),
["A B", "é", "A B", "e\u{301}", "A B"].map(Some)
);
let AlphaMapping::Categorical(entries) = alpha.get_mapping() else {
panic!("categorical declaration")
};
assert_eq!(
entries
.iter()
.map(|(raw, _)| raw.clone())
.collect::<Vec<_>>(),
calls
.iter()
.map(|key| RawKey::String(key.clone()))
.collect::<Vec<_>>(),
);
let selected = alpha.select([3, 0, 3]).unwrap();
assert_eq!(selected.data.values, [1.0, 0.0, 1.0]);
assert_eq!(
selected
.get_category_source()
.unwrap()
.get_strings()
.unwrap()
.iter()
.collect::<Vec<_>>(),
["e\u{301}", "A B", "e\u{301}"].map(Some)
);
assert!(Arc::ptr_eq(&selected.declaration, &alpha.declaration));
let error = AlphaValues::categorical(
CategoryValues::nullable("bad", [Some("valid"), None]).unwrap(),
|raw| {
calls.push(raw.to_owned());
123.0 / 255.0
},
)
.unwrap_err();
assert!(matches!(error, Error::Data(_)));
assert!(error.to_string().contains("index 1"), "{error}");
assert_eq!(calls.len(), 4);
let empty = AlphaValues::categorical(
CategoryValues::new("empty", std::iter::empty::<&str>()).unwrap(),
|_| {
calls.push("unexpected".into());
0.0
},
)
.unwrap();
assert!(empty.is_empty());
assert_eq!(empty.semantics(), AlphaSemantics::Categorical);
assert_eq!(calls.len(), 4);
}
#[test]
fn thresholds_assign_equality_to_the_right_and_store_distinct_whole_set_boundary_labels() {
let alpha = AlphaValues::threshold(
NumericValues::new("signal", [-1.0, -0.0, 0.0, 0.5, 1.0, 2.0]),
[0.0, 1.0],
[0.0, 90.0 / 255.0, 1.0],
)
.unwrap();
assert_eq!(alpha.semantics(), AlphaSemantics::Threshold);
assert_eq!(
alpha.data.values,
[0.0, 90.0 / 255.0, 90.0 / 255.0, 90.0 / 255.0, 1.0, 1.0]
);
assert_eq!(alpha.get_threshold_indices(), Some(&[0, 1, 1, 1, 2, 2][..]));
assert_eq!(
alpha
.get_numeric_source()
.unwrap()
.get(1)
.flatten()
.unwrap()
.to_bits(),
(-0.0_f64).to_bits()
);
assert_eq!(
alpha.get_mapping(),
&AlphaMapping::Threshold {
boundaries: vec![0., 1.],
alphas: vec![0., 90. / 255., 1.],
}
);
assert_eq!(alpha.declaration.labels, ["< 0", "[0, 1)", ">= 1"]);
let selected = alpha.select([4, 0, 4]).unwrap();
assert_eq!(selected.data.values, [1.0, 0.0, 1.0]);
assert!(Arc::ptr_eq(&selected.declaration, &alpha.declaration));
let singleton = AlphaValues::threshold(
NumericValues::new("signal", [1.0, -1.0]),
[],
[17.0 / 255.0],
)
.unwrap();
assert_eq!(singleton.data.values, [17.0 / 255.0, 17.0 / 255.0]);
assert_eq!(
singleton.get_mapping(),
&AlphaMapping::Threshold {
boundaries: vec![],
alphas: vec![17. / 255.],
}
);
assert_eq!(singleton.declaration.labels, ["all values"]);
let close = AlphaValues::threshold(
NumericValues::new("signal", []),
[1.0, 1.0_f64.next_up()],
[0.0, 127.0 / 255.0, 1.0],
)
.unwrap();
assert_eq!(
close.declaration.labels[1],
"[1.0000000000000000e0, 1.0000000000000002e0)"
);
for boundaries in [
vec![f64::NAN],
vec![f64::INFINITY],
vec![1.0, 0.0],
vec![-0.0, 0.0],
] {
assert!(matches!(
AlphaValues::threshold(NumericValues::new("bad", []), boundaries, [0.0, 0.5, 1.0]),
Err(Error::Data(_))
));
}
assert!(AlphaValues::threshold(NumericValues::new("bad", []), [0.0], [1.0]).is_err());
assert!(AlphaValues::threshold(NumericValues::new("bad", []), [], []).is_err());
let error = AlphaValues::threshold(
NumericValues::nullable("bad", [Some(0.5), None]),
[0.0],
[0.0, 1.0],
)
.unwrap_err();
assert!(matches!(error, Error::Data(_)));
assert!(error.to_string().contains("index 1"), "{error}");
}
#[test]
fn alpha_guide_members_identify_declared_raw_samples_and_intervals() {
let samples = AlphaValues::linear(
NumericValues::new("quality", [0.0, 1.0, 2.0]),
(0.0, 2.0),
(0.5, 0.5),
)
.unwrap();
let examples = samples
.guide_examples([ResolvedAlpha {
index: Some(0),
alpha: 0.5,
}])
.unwrap();
assert_eq!(
examples
.iter()
.map(|example| example.key.clone())
.collect::<Vec<_>>(),
[0., 0.5, 1., 1.5, 2.].map(|raw| AlphaExampleKey::Sample(RawKey::Float(raw)))
);
assert!(examples
.iter()
.all(|example| example.alpha.to_bits() == 0.5_f64.to_bits()));
let source = NumericValues::new("quality", [-1.0, 0.0, 1.0]);
let alpha = AlphaValues::threshold(source.clone(), [-0.0, 1.0], [0.5; 3]).unwrap();
let other = AlphaValues::threshold(source, [0.0, 1.0], [0.5; 3]).unwrap();
assert!(!alpha.same_declaration(&other));
let examples = alpha
.guide_examples((0..3).map(|index| ResolvedAlpha {
index: Some(index),
alpha: 0.5,
}))
.unwrap();
assert_eq!(
examples[0].key,
AlphaExampleKey::Interval {
lower: None,
upper: Some(RawKey::Float(-0.0)),
}
);
assert_eq!(
examples[1].key,
AlphaExampleKey::Interval {
lower: Some(RawKey::Float(-0.0)),
upper: Some(RawKey::Float(1.0)),
}
);
assert_eq!(
examples[2].key,
AlphaExampleKey::Interval {
lower: Some(RawKey::Float(1.0)),
upper: None,
}
);
}