pub mod bootstrap_test;
pub mod randomized_tukey_hsd_test;
pub mod student_t_test;
pub mod tukey_hsd_test;
pub mod two_way_anova_without_replication;
pub use bootstrap_test::BootstrapTest;
pub use randomized_tukey_hsd_test::RandomizedTukeyHsdTest;
pub use student_t_test::StudentTTest;
pub use tukey_hsd_test::TukeyHsdTest;
pub use two_way_anova_without_replication::TwoWayAnovaWithoutReplication;
use std::collections::BTreeMap;
use crate::errors::ElinorError;
use crate::errors::Result;
pub fn pairs_from_maps<K>(
map_a: &BTreeMap<K, f64>,
map_b: &BTreeMap<K, f64>,
) -> Result<Vec<(f64, f64)>>
where
K: Clone + Eq + Ord + std::fmt::Display,
{
tuples_from_maps([map_a, map_b]).map(|tuples| {
tuples
.into_iter()
.map(|tuple| (tuple[0], tuple[1]))
.collect()
})
}
pub fn tuples_from_maps<'a, I, K>(maps: I) -> Result<Vec<Vec<f64>>>
where
I: IntoIterator<Item = &'a BTreeMap<K, f64>>,
K: Clone + Eq + Ord + std::fmt::Display + 'a,
{
let maps = maps.into_iter().collect::<Vec<_>>();
for i in 1..maps.len() {
if maps[0].len() != maps[i].len() {
return Err(ElinorError::InvalidArgument(format!(
"The number of keys in maps must be the same, but got maps[0].len()={} and maps[{}].len()={}.",
maps[0].len(),
i,
maps[i].len()
)));
}
if maps[0].keys().ne(maps[i].keys()) {
return Err(ElinorError::InvalidArgument(
"The keys in the maps must be the same.".to_string(),
));
}
}
let mut tuples = vec![];
for query_id in maps[0].keys() {
let mut tuple = vec![];
for &map in &maps {
tuple.push(*map.get(query_id).unwrap());
}
tuples.push(tuple);
}
Ok(tuples)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pairs_from_maps_different_keys() {
let map_a = [("a", 0.70), ("b", 0.30), ("c", 0.20)].into();
let map_b = [("a", 0.50), ("b", 0.10), ("d", 0.00)].into();
assert_eq!(
pairs_from_maps(&map_a, &map_b),
Err(ElinorError::InvalidArgument(
"The keys in the maps must be the same.".to_string()
))
);
}
#[test]
fn test_tuples_from_maps_different_keys() {
let map_a = [("a", 0.70), ("b", 0.30), ("c", 0.20)].into();
let map_b = [("a", 0.50), ("b", 0.10), ("d", 0.00)].into();
let map_c = [("a", 0.60), ("b", 0.20), ("c", 0.10)].into();
assert_eq!(
tuples_from_maps([&map_a, &map_b, &map_c]),
Err(ElinorError::InvalidArgument(
"The keys in the maps must be the same.".to_string()
))
);
}
#[test]
fn test_tuples_from_maps_single_map() {
let map_a = [("a", 0.70), ("b", 0.30), ("c", 0.20)].into();
assert_eq!(
tuples_from_maps([&map_a]),
Ok(vec![vec![0.70], vec![0.30], vec![0.20]])
);
}
}