Debian Codegraph @

debian/rust-vendor/statrs/tests/nist_tests.rs

raw
1 //! This test relies on data that is reusable but not distributable by statrs as
2 //! such, the data will need to be downloaded from the relevant NIST StRD dataset
3 //! the parsing for testing assumes data to be of form,
4 //! ```text
5 //! sample mean : <possibly signed float for mean>
6 //! sample std_dev : <possibly signed float for standard deviation>
7 //! sample correlation: <possibly signed float for correlation coefficient>
8 //! [zero or more blank lines]
9 //! data0
10 //! data1
11 //! data2
12 //! ...
13 //! ```
14 //! This test can be run on it's own from the shell from this folder as
15 //! ```sh
16 //! ./gather_nist_data.sh && cargo test -- --ignored nist_
17 //! ```
18 use anyhow::Result;
19 use approx::assert_relative_eq;
20 use statrs::statistics::Statistics;
21
22 use std::io::{BufRead, BufReader};
23 use std::path::PathBuf;
24 use std::{env, fs};
25
26 struct TestCase {
27 certified: CertifiedValues,
28 values: Vec<f64>,
29 }
30
31 impl std::fmt::Debug for TestCase {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 write!(f, "TestCase({:?}, [...]", self.certified)
34 }
35 }
36
37 #[derive(Debug)]
38 struct CertifiedValues {
39 mean: f64,
40 std_dev: f64,
41 corr: f64,
42 }
43
44 impl std::fmt::Display for CertifiedValues {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 write!(
47 f,
48 "μ={:.3e}, σ={:.3e}, r={:.3e}",
49 self.mean, self.std_dev, self.corr
50 )
51 }
52 }
53
54 const NIST_DATA_DIR_ENV: &str = "STATRS_NIST_DATA_DIR";
55 const FILENAMES: [&str; 7] = [
56 "Lottery.dat",
57 "Lew.dat",
58 "Mavro.dat",
59 "Michelso.dat",
60 "NumAcc1.dat",
61 "NumAcc2.dat",
62 "NumAcc3.dat",
63 ];
64
65 fn get_path(fname: &str, prefix: Option<&str>) -> PathBuf {
66 if let Some(prefix) = prefix {
67 [prefix, fname].iter().collect()
68 } else {
69 ["tests", fname].iter().collect()
70 }
71 }
72
73 #[test]
74 #[ignore = "NIST tests should not run from typical `cargo test` calls"]
75 fn nist_strd_univariate_mean() {
76 for fname in FILENAMES {
77 let filepath = get_path(fname, env::var(NIST_DATA_DIR_ENV).ok().as_deref());
78 let case = parse_file(filepath)
79 .unwrap_or_else(|e| panic!("failed parsing file {fname} with `{e:?}`"));
80 assert_relative_eq!(case.values.mean(), case.certified.mean, epsilon = 1e-12);
81 }
82 }
83
84 #[test]
85 #[ignore]
86 fn nist_strd_univariate_std_dev() {
87 for fname in FILENAMES {
88 let filepath = get_path(fname, env::var(NIST_DATA_DIR_ENV).ok().as_deref());
89 let case = parse_file(filepath)
90 .unwrap_or_else(|e| panic!("failed parsing file {fname} with `{e:?}`"));
91 assert_relative_eq!(
92 case.values.std_dev(),
93 case.certified.std_dev,
94 epsilon = 1e-10
95 );
96 }
97 }
98
99 fn parse_certified_value(line: String) -> Result<f64> {
100 line.chars()
101 .skip_while(|&c| c != ':')
102 .skip(1) // skip through ':' delimiter
103 .skip_while(|&c| c.is_whitespace()) // effectively `String` trim
104 .take_while(|&c| matches!(c, '0'..='9' | '-' | '.'))
105 .collect::<String>()
106 .parse::<f64>()
107 .map_err(|e| e.into())
108 }
109
110 fn parse_file(path: impl AsRef<std::path::Path>) -> anyhow::Result<TestCase> {
111 let f = fs::File::open(path)?;
112 let reader = BufReader::new(f);
113 let mut lines = reader.lines();
114
115 let mean = parse_certified_value(lines.next().expect("file should not be exhausted")?)?;
116 let std_dev = parse_certified_value(lines.next().expect("file should not be exhausted")?)?;
117 let corr = parse_certified_value(lines.next().expect("file should not be exhausted")?)?;
118
119 Ok(TestCase {
120 certified: CertifiedValues {
121 mean,
122 std_dev,
123 corr,
124 },
125 values: lines
126 .map_while(|line| line.ok()?.trim().parse().ok())
127 .collect(),
128 })
129 }
130
131 #[test]
132 #[ignore = "NIST tests should not run from typical `cargo test` calls"]
133 fn nist_test_covariance_consistent_with_variance() {}
134
135 #[test]
136 #[ignore = "NIST tests should not run from typical `cargo test` calls"]
137 fn nist_test_covariance_is_symmetric() {}