comnum-spec 0.3.0

Declarative Monte Carlo campaigns of comnum (YAML, JSON, TOML), validation against the literature, recordings I/O (SigMF, raw IQ, NPY) and the comnum command
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
//! Search of the operating point: the value of a variable at which a metric reaches a
//! target, with its confidence interval.
//!
//! A sweep answers "what is the BER at 12 dB?"; a link budget asks the converse, "which
//! SNR gives a BER of $10^{-3}$?". The `search` section of a specification answers it
//! directly:
//!
//! ```yaml
//! search:
//!   along: ebn0_db          # the variable searched (a grid axis or not)
//!   range: [4, 16]          # the bracket of the search
//!   metric: ber
//!   target: 1.0e-3
//!   tolerance: 0.02         # stop when the bracket is narrower
//!   log: true               # interpolate log10 of the metric (default)
//! ```
//!
//! For every combination of the other grid axes, the metric is evaluated at both ends of
//! the range (the target must lie between the two values), then the bracket is halved
//! until it is narrower than `tolerance` (at most `max_evaluations` evaluations). Every
//! evaluation is a run of one condition with the stopping rule of the specification;
//! all of them use the same random streams (common random numbers), so the comparison
//! of two values of the variable is not blurred by the draws, and the estimate is
//! monotone wherever the link is.
//!
//! The crossing is interpolated between the two ends of the final bracket (linearly in
//! `log10` of the metric, or in the metric). Its confidence interval comes from the
//! confidence intervals of these two estimates: the crossings of the lower and of the
//! upper bounds of the metric, interpolated the same way (and extrapolated when a bound
//! does not cross the target inside the bracket).
//!
//! # Examples
//! ```
//! use comnum_spec::{search::search, RunOptions, Spec};
//! let spec = Spec::from_yaml(r#"
//! name: qpsk_required
//! link:
//!   - {type: source, constellation: QPSK, n_symbols: 4000}
//!   - {type: awgn_ebn0, ebn0_db: $ebn0_db}
//! metrics: {report: [ber], scale: none}
//! stop: {min_errors: {ber: 400}, max_trials: 400}
//! search: {along: ebn0_db, range: [2, 10], metric: ber, target: 1.0e-3, tolerance: 0.05}
//! "#).unwrap();
//! let r = search(&spec, &RunOptions::default()).unwrap();
//! // Q(√(2 Eb/N0)) = 1e-3 at 6.79 dB
//! assert!((r[0].value - 6.79).abs() < 0.15, "{}", r[0].value);
//! assert!(r[0].lo <= r[0].value && r[0].value <= r[0].hi);
//! ```

use crate::params::parse;
use crate::{RunOptions, Spec, SpecScenario};
use comnum_core::{Error, Result, ensure};
use comnum_sim::grid::{Condition, Grid};
use comnum_sim::json;
use comnum_sim::loggers::{create_parent_dir, csv_field, run_header, value_json, write_json as write_json_file};
use comnum_sim::runner::RunInfo;
use serde::Deserialize;
use serde_json::Value as Json;
use std::path::Path;

/// The `search` section of a specification.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SearchSpec {
    /// The variable searched.
    pub along: String,
    /// The bracket `[lo, hi]` of the search.
    pub range: [f64; 2],
    /// The metric.
    pub metric: String,
    /// The value of the metric to reach.
    pub target: f64,
    /// Stop when the bracket is narrower (in the unit of the variable).
    #[serde(default = "d_tolerance")]
    pub tolerance: f64,
    /// Interpolate `log10` of the metric (rates spanning decades).
    #[serde(default = "d_true")]
    pub log: bool,
    /// Largest number of evaluations per combination of the other axes.
    #[serde(default = "d_max_eval")]
    pub max_evaluations: usize,
}

fn d_tolerance() -> f64 {
    0.05
}
fn d_true() -> bool {
    true
}
fn d_max_eval() -> usize {
    30
}

impl SearchSpec {
    /// Parse and validate the `search` mapping.
    ///
    /// # Errors
    /// [`Error::Config`] for a missing field, an empty range or a non-positive tolerance.
    pub fn from_json(v: &Json) -> Result<SearchSpec> {
        let s: SearchSpec = parse("search", v.clone())?;
        ensure(s.range[0] < s.range[1], || format!("search: the range {:?} must increase", s.range))?;
        ensure(s.tolerance > 0.0, || "search: the tolerance must be positive".into())?;
        ensure(s.max_evaluations >= 3, || "search: at least 3 evaluations".into())?;
        ensure(!s.log || s.target > 0.0, || "search: a log search needs a positive target".into())?;
        Ok(s)
    }
}

/// One evaluation of the metric during a search.
#[derive(Clone, Debug, PartialEq)]
pub struct Evaluation {
    /// Value of the variable.
    pub x: f64,
    /// Estimate of the metric and its confidence interval.
    pub value: f64,
    /// Lower bound of the interval.
    pub lo: f64,
    /// Upper bound of the interval.
    pub hi: f64,
    /// Trials run.
    pub trials: u64,
}

/// The operating point of one combination of the other axes.
#[derive(Clone, Debug, PartialEq)]
pub struct SearchResult {
    /// The values of the other axes.
    pub condition: Condition,
    /// The variable searched.
    pub along: String,
    /// The metric.
    pub metric: String,
    /// Its target.
    pub target: f64,
    /// The value of the variable at which the metric reaches the target.
    pub value: f64,
    /// Lower bound of its confidence interval.
    pub lo: f64,
    /// Upper bound of its confidence interval.
    pub hi: f64,
    /// Whether the final bracket is narrower than the tolerance.
    pub converged: bool,
    /// Every evaluation, in the order of the search.
    pub evaluations: Vec<Evaluation>,
}

/// The value of the variable at which `g` crosses `g(target)` between `(a, ga)` and
/// `(b, gb)` (extrapolated outside; the midpoint for a flat segment).
fn crossing(a: f64, ga: f64, b: f64, gb: f64, gt: f64) -> f64 {
    if (gb - ga).abs() < 1e-300 || !ga.is_finite() || !gb.is_finite() {
        return 0.5 * (a + b);
    }
    a + (b - a) * (gt - ga) / (gb - ga)
}

/// The conditions of the other axes: the grid without `along`, in order, unique.
fn combinations(grid: &Grid, along: &str) -> Vec<Condition> {
    let mut out: Vec<Condition> = Vec::new();
    for c in grid.conditions() {
        let mut d = Condition::new();
        for (k, v) in c.entries() {
            if k != along {
                d.set(k, v.clone());
            }
        }
        if !out.contains(&d) {
            out.push(d);
        }
    }
    out
}

/// The sweep of the first points of the search of `spec`: every combination of the other
/// axes, with the searched variable at the lower end of the range (what `comnum check`
/// runs); `None` without a `search` section.
pub(crate) fn first_points(spec: &Spec) -> Option<Spec> {
    let s = spec.search.as_ref()?;
    let conditions = combinations(&spec.grid, &s.along).into_iter().map(|c| c.with(&s.along, s.range[0])).collect();
    Some(Spec { grid: Grid::from_conditions(conditions), search: None, ..spec.clone() })
}

/// Run the search of a specification.
///
/// # Errors
/// [`Error::Config`] without a `search` section, or when the target is not between the
/// values of the metric at the ends of the range; the errors of the runs.
pub fn search(spec: &Spec, opts: &RunOptions) -> Result<Vec<SearchResult>> {
    let s = spec.search.as_ref().ok_or_else(|| Error::Config(format!("{}: no search section", spec.name)))?;
    let combos = combinations(&spec.grid, &s.along);
    let info = spec.bare_runner(opts).run_info(&spec.name, combos.len());
    let t0 = std::time::Instant::now();
    let mut results = Vec::new();
    for base in combos {
        results.push(bisect(spec, opts, s, base)?);
    }
    if let Some(p) = opts.csv.as_ref().or(spec.csv.as_ref()) {
        write_csv(p, &results)?;
    }
    if let Some(p) = opts.json.as_ref().or(spec.json.as_ref()) {
        write_json(p, &info, t0.elapsed().as_secs_f64(), &results)?;
    }
    Ok(results)
}

/// The metric of the search at `x` for the combination `base` of the other axes.
fn evaluate(spec: &Spec, opts: &RunOptions, s: &SearchSpec, base: &Condition, x: f64) -> Result<Evaluation> {
    let cond = base.clone().with(&s.along, x);
    // a single condition: index 0, so every value of the variable sees the same
    // random streams (common random numbers)
    let grid = Grid::from_conditions(vec![cond.clone()]);
    let r = spec.bare_runner(opts).run(&SpecScenario::new(spec), &grid)?.remove(0);
    let est = r
        .metric(&s.metric)
        .ok_or_else(|| Error::Config(format!("search: no metric {:?} in ({})", s.metric, cond.label())))?;
    let (lo, hi) = est.interval(spec.confidence);
    if opts.progress.unwrap_or(spec.progress) {
        eprintln!("[{}] ({}): {} = {:.4e} [{lo:.3e}, {hi:.3e}]", spec.name, cond.label(), s.metric, est.value());
    }
    Ok(Evaluation { x, value: est.value(), lo, hi, trials: r.trials })
}

/// The bisection of one combination `base` of the other axes: the ends of the range,
/// then the midpoint of the bracket until it is narrower than the tolerance or the
/// evaluations run out.
///
/// # Errors
/// [`Error::Config`] when the target is not between the values at the ends of the
/// range; the errors of the runs.
fn bisect(spec: &Spec, opts: &RunOptions, s: &SearchSpec, base: Condition) -> Result<SearchResult> {
    let eval = |x: f64| evaluate(spec, opts, s, &base, x);
    let (mut a, mut b) = (eval(s.range[0])?, eval(s.range[1])?);
    let mut evaluations = vec![a.clone(), b.clone()];
    let side = |e: &Evaluation| (e.value - s.target).signum();
    if side(&a) * side(&b) >= 0.0 {
        return Err(Error::Config(format!(
            "search ({}): the target {} of {} is not between {:.4e} at {} = {} and {:.4e} at {} = {}",
            base.label(),
            s.target,
            s.metric,
            a.value,
            s.along,
            a.x,
            b.value,
            s.along,
            b.x
        )));
    }
    while b.x - a.x > s.tolerance && evaluations.len() < s.max_evaluations {
        let m = eval(0.5 * (a.x + b.x))?;
        evaluations.push(m.clone());
        if side(&m) == side(&a) {
            a = m;
        } else {
            b = m;
        }
    }
    Ok(operating_point(s, base, &a, &b, evaluations))
}

/// The operating point of the final bracket `[a, b]`: the crossing of the target by the
/// estimate and by the bounds of its interval, interpolated linearly (in $\log_{10}$ of
/// the metric for a log search).
fn operating_point(
    s: &SearchSpec,
    base: Condition,
    a: &Evaluation,
    b: &Evaluation,
    evaluations: Vec<Evaluation>,
) -> SearchResult {
    let g = |v: f64| if s.log { v.max(1e-300).log10() } else { v };
    let gt = g(s.target);
    let value = crossing(a.x, g(a.value), b.x, g(b.value), gt);
    let x1 = crossing(a.x, g(a.lo), b.x, g(b.lo), gt);
    let x2 = crossing(a.x, g(a.hi), b.x, g(b.hi), gt);
    SearchResult {
        condition: base,
        along: s.along.clone(),
        metric: s.metric.clone(),
        target: s.target,
        value,
        lo: x1.min(x2).min(value),
        hi: x1.max(x2).max(value),
        converged: b.x - a.x <= s.tolerance,
        evaluations,
    }
}

/// One row per combination: its keys, then the variable at the target, its interval,
/// the convergence and the number of evaluations.
fn write_csv(path: &Path, results: &[SearchResult]) -> Result<()> {
    create_parent_dir(path)?;
    let mut out = String::new();
    if let Some(first) = results.first() {
        let keys: Vec<&str> = first.condition.keys().collect();
        let mut header: Vec<String> = keys.iter().map(|k| csv_field(k)).collect();
        let a = &first.along;
        header.extend([
            "metric".into(),
            "target".into(),
            a.clone(),
            format!("{a}_lo"),
            format!("{a}_hi"),
            "converged".into(),
            "evaluations".into(),
        ]);
        out.push_str(&(header.join(",") + "\n"));
        for r in results {
            let mut row: Vec<String> = keys
                .iter()
                .map(|k| r.condition.get(k).map(|v| csv_field(&v.to_string())).unwrap_or_default())
                .collect();
            row.extend([
                r.metric.clone(),
                format!("{:e}", r.target),
                format!("{}", r.value),
                format!("{}", r.lo),
                format!("{}", r.hi),
                r.converged.to_string(),
                r.evaluations.len().to_string(),
            ]);
            out.push_str(&(row.join(",") + "\n"));
        }
    }
    std::fs::write(path, out)?;
    Ok(())
}

/// The JSON report of a search: the header of the reports of the runs
/// ([`comnum_sim::loggers::run_header`]), the wall time, and every operating point with its
/// evaluations.
fn write_json(path: &Path, info: &RunInfo, wall_s: f64, results: &[SearchResult]) -> Result<()> {
    let items: Vec<String> = results
        .iter()
        .map(|r| {
            let cond: Vec<(&str, String)> =
                r.condition.entries().iter().map(|(k, v)| (k.as_str(), value_json(v))).collect();
            let evals: Vec<String> = r
                .evaluations
                .iter()
                .map(|e| {
                    json::object(&[
                        ("x", json::number(e.x)),
                        ("value", json::number(e.value)),
                        ("lo", json::number(e.lo)),
                        ("hi", json::number(e.hi)),
                        ("trials", e.trials.to_string()),
                    ])
                })
                .collect();
            json::object(&[
                ("condition", json::object(&cond)),
                ("along", json::string(&r.along)),
                ("metric", json::string(&r.metric)),
                ("target", json::number(r.target)),
                ("value", json::number(r.value)),
                ("lo", json::number(r.lo)),
                ("hi", json::number(r.hi)),
                ("converged", r.converged.to_string()),
                ("evaluations", json::array(&evals)),
            ])
        })
        .collect();
    let mut fields = run_header(info);
    fields.push(("wall_s", json::number(wall_s)));
    fields.push(("search", json::array(&items)));
    write_json_file(path, &json::object(&fields))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn spec(extra: &str) -> Spec {
        Spec::from_yaml(&format!(
            "name: s\nseed: 4\ngrid: {{m: [4, 16]}}\nlink: [{{type: source, constellation: {{kind: qam, m: $m}}, n_symbols: 4000}}, \
             {{type: awgn_ebn0, ebn0_db: $ebn0_db}}]\nmetrics: {{report: [ber], scale: none}}\n\
             stop: {{min_errors: {{ber: 300}}, max_trials: 300}}\n{extra}"
        ))
        .unwrap()
    }

    #[test]
    fn the_operating_points_follow_the_theory() {
        let s = spec("search: {along: ebn0_db, range: [2, 16], metric: ber, target: 1.0e-3, tolerance: 0.05}");
        let r = search(&s, &RunOptions::default()).unwrap();
        assert_eq!(r.len(), 2);
        for (res, m) in r.iter().zip([4usize, 16]) {
            let theory =
                comnum_mapping::theory::ebn0_for_ber(comnum_mapping::constellation::ConstellationKind::Qam, m, 1e-3)
                    .unwrap();
            assert!(res.converged && res.evaluations.len() <= 12);
            assert!(res.lo - 0.1 <= theory && theory <= res.hi + 0.1, "M = {m}: {res:?} against {theory}");
            assert!(res.hi - res.lo < 0.5);
        }
    }

    #[test]
    fn an_unbracketed_target_is_an_error() {
        let s = spec("search: {along: ebn0_db, range: [12, 16], metric: ber, target: 1.0e-1}");
        let e = search(&s, &RunOptions::default()).unwrap_err().to_string();
        assert!(e.contains("not between"), "{e}");
        assert!(
            Spec::from_yaml("name: x\nlink: [source]\nsearch: {along: x, range: [2, 1], metric: ber, target: 1}")
                .is_err()
        );
    }

    #[test]
    fn the_first_points_of_a_search_are_a_sweep() {
        let s = spec("search: {along: ebn0_db, range: [4, 12], metric: ber, target: 1.0e-2}");
        // the searched variable is not in the grid: the link cannot be built as such
        assert!(s.link_for(&s.conditions()[0]).is_err());
        let sweep = first_points(&s).unwrap();
        let labels: Vec<String> = sweep.conditions().iter().map(Condition::label).collect();
        assert_eq!(labels, ["m=4, ebn0_db=4", "m=16, ebn0_db=4"]);
        assert!(sweep.search.is_none() && sweep.link_for(&sweep.conditions()[1]).is_ok());
        assert!(first_points(&sweep).is_none());
    }

    #[test]
    fn reports_are_written() {
        let dir = std::env::temp_dir().join(format!("comnum-search-{}", std::process::id()));
        let s = spec("search: {along: ebn0_db, range: [4, 12], metric: ber, target: 1.0e-2, tolerance: 0.5}");
        let opts = RunOptions { csv: Some(dir.join("s.csv")), json: Some(dir.join("s.json")), ..RunOptions::default() };
        search(&s, &opts).unwrap();
        let csv = std::fs::read_to_string(dir.join("s.csv")).unwrap();
        assert!(csv.starts_with("m,metric,target,ebn0_db,ebn0_db_lo,ebn0_db_hi,converged,evaluations\n"), "{csv}");
        assert_eq!(csv.lines().count(), 3);
        let json = std::fs::read_to_string(dir.join("s.json")).unwrap();
        // the header of the reports of the runs
        for key in ["\"evaluations\"", "\"batch\"", "\"threads\"", "\"stop\"", "\"commit\"", "\"wall_s\""] {
            assert!(json.contains(key), "{key} missing");
        }
        std::fs::remove_dir_all(&dir).ok();
    }
}