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
//! The command-line tool `comnum`: run declarative Monte Carlo campaigns.
//!
//! ```text
//! comnum run SPEC... [--out DIR] [--threads N] [--fast-dev-run] [--profile] [--quiet] [--json] [--max-seconds S]
//! comnum check SPEC... [--json]
//! comnum verify SPEC... [--out DIR] [--threads N] [--quiet] [--json] [--max-seconds S]
//! comnum conditions SPEC
//! comnum blocks [NAME...] [--json]
//! comnum schema
//! comnum guide
//! ```
//!
//! `run` writes the CSV and JSON reports given by the specification, or
//! `DIR/<name>.csv` and `DIR/<name>.json` (`DIR` = `results` by default), and prints
//! every metric with its confidence interval (for a specification with a `search`, the
//! operating point of every combination of the other axes); `--profile` adds the mean
//! time of every block ([`profile`](crate::profile)). `check` builds the link of every
//! condition and runs one trial of each, without writing anything (for a specification
//! with a `search`, at the lower end of its range for every combination of the other
//! axes). `verify` runs the specifications and checks their expected results (closed
//! forms, published curves), prints every check, writes the reports of the run and
//! `DIR/<name>.verify.json`, and fails if a check fails. `conditions` lists the
//! conditions of the grid; `blocks` documents the blocks of the links (their parameters
//! with types, units and accepted values, and their ports; as JSON with `--json`);
//! `schema` prints the JSON Schema of a block of a link, for editors and graphical
//! front ends; `guide` prints the guide for programs and agents
//! (`GUIDE_AGENTS.md` of the crate).
//!
//! For programs and agents ([`agent`]): `--json` prints one compact JSON document per
//! specification on standard output (the results with their confidence intervals and
//! their provenance, the checks of `verify`) and the errors as JSON with a kind, a hint
//! and a suggestion for a misspelt block or parameter; `--max-seconds S` bounds the
//! wall time of a run (300 s by default with `--json`, 0 for none), spread over its
//! conditions, and marks the conditions it cut short (`time_limited`: their results
//! then depend on the machine). A specification `-` is read from standard input (YAML
//! or JSON).
//!
//! The binary `comnum` of this crate is [`main`] on the arguments of the process; a crate
//! that adds blocks ([`blocks::register`]) wraps the same function in its own binary,
//! so that its command lists, checks and runs them with the others.

use crate::{Format, RunOptions, Spec, blocks, run, search, verify};
use comnum_sim::stopping::Stop;
use serde_json::json;
use std::path::PathBuf;
use std::process::ExitCode;

pub mod agent;

const USAGE: &str = "usage:
  comnum run SPEC... [--out DIR] [--threads N] [--fast-dev-run] [--profile] [--quiet] [--json] [--max-seconds S]
  comnum check SPEC... [--json]
  comnum verify SPEC... [--out DIR] [--threads N] [--quiet] [--json] [--max-seconds S]
  comnum conditions SPEC
  comnum blocks [NAME...] [--json]
  comnum schema
  comnum guide

SPEC is a YAML, JSON or TOML specification (see the documentation of the crate comnum-spec), or -
for standard input. --json: one JSON document per specification on standard output, for programs
and agents (a time budget of 300 s unless --max-seconds is given; --max-seconds 0 for none).";

#[allow(clippy::struct_excessive_bools, reason = "the flags of the command line")]
struct Args {
    command: String,
    files: Vec<PathBuf>,
    out: PathBuf,
    threads: Option<usize>,
    fast_dev_run: bool,
    profile: bool,
    quiet: bool,
    json: bool,
    max_seconds: Option<f64>,
}

fn parse_args(args: impl IntoIterator<Item = String>) -> Result<Args, String> {
    let mut it = args.into_iter();
    let command = it.next().ok_or_else(|| USAGE.to_string())?;
    let mut a = Args {
        command,
        files: Vec::new(),
        out: PathBuf::from("results"),
        threads: None,
        fast_dev_run: false,
        profile: false,
        quiet: false,
        json: false,
        max_seconds: None,
    };
    while let Some(arg) = it.next() {
        match arg.as_str() {
            "--out" => a.out = PathBuf::from(it.next().ok_or("--out needs a directory")?),
            "--threads" => {
                let n = it.next().ok_or("--threads needs a number")?;
                a.threads = Some(n.parse().map_err(|_| format!("--threads: {n:?} is not a number"))?);
            }
            "--fast-dev-run" => a.fast_dev_run = true,
            "--profile" => a.profile = true,
            "--quiet" => a.quiet = true,
            "--json" => a.json = true,
            "--max-seconds" => {
                let s = it.next().ok_or("--max-seconds needs a number of seconds")?;
                let v: f64 = s.parse().map_err(|_| format!("--max-seconds: {s:?} is not a number"))?;
                if !(v.is_finite() && v >= 0.0) {
                    return Err(format!("--max-seconds: {s} must be a number of seconds >= 0"));
                }
                a.max_seconds = Some(v);
            }
            "-h" | "--help" => return Err(USAGE.to_string()),
            s if s.starts_with("--") => return Err(format!("unknown option {s}\n\n{USAGE}")),
            "-" => a.files.push(PathBuf::from("-")),
            s => a.files.push(PathBuf::from(s)),
        }
    }
    Ok(a)
}

/// The options of `run` and `verify`: the threads, the progress and the reports, those
/// of the specification or `DIR/<name>.csv` and `DIR/<name>.json`.
fn report_options(spec: &Spec, a: &Args) -> RunOptions {
    RunOptions {
        threads: a.threads,
        progress: Some(!a.quiet && !a.json),
        csv: Some(spec.csv.clone().unwrap_or_else(|| a.out.join(format!("{}.csv", spec.name)))),
        json: Some(spec.json.clone().unwrap_or_else(|| a.out.join(format!("{}.json", spec.name)))),
        ..RunOptions::default()
    }
}

/// Say where the reports went, unless `--quiet`.
fn written(opts: &RunOptions, a: &Args) {
    if let (false, false, Some(csv), Some(json)) = (a.quiet, a.json, &opts.csv, &opts.json) {
        eprintln!("written {} and {}", csv.display(), json.display());
    }
}

/// Read a specification from a file, or from standard input for `-`.
fn load(path: &PathBuf) -> comnum_core::Result<Spec> {
    if path.as_os_str() == "-" {
        let mut text = String::new();
        std::io::Read::read_to_string(&mut std::io::stdin(), &mut text)?;
        return Spec::parse(&text, Format::Yaml).map_err(|e| e.context("standard input"));
    }
    Spec::from_path(path).map_err(|e| match e {
        comnum_core::Error::Io(io) => {
            comnum_core::Error::Io(std::io::Error::new(io.kind(), format!("{}: {io}", path.display())))
        }
        other => other,
    })
}

/// The specification with the time budget of the options, and the rule of the
/// specification when a budget applies.
fn budgeted(path: &PathBuf, a: &Args) -> comnum_core::Result<(Spec, Option<Stop>)> {
    let mut spec = load(path)?;
    let original = agent::budget(a.max_seconds, a.json).map(|s| agent::apply_budget(&mut spec, s));
    Ok((spec, original))
}

fn run_one(path: &PathBuf, a: &Args) -> comnum_core::Result<()> {
    let (spec, original) = budgeted(path, a)?;
    let opts =
        RunOptions { fast_dev_run: a.fast_dev_run, profile: a.profile.then_some(true), ..report_options(&spec, a) };
    let started = std::time::Instant::now();
    if a.json {
        let mut doc = json!({
            "ok": true,
            "command": "run",
            "spec": spec.name,
            "description": spec.description,
            "provenance": agent::provenance(&spec, &opts),
            "budget_s": agent::budget(a.max_seconds, a.json),
        });
        if spec.search.is_some() {
            let found = search::search(&spec, &opts)?;
            doc["operating_points"] = found.iter().map(agent::operating_point).collect();
        } else {
            let results = run(&spec, &opts)?;
            doc["conditions"] =
                results.iter().map(|r| agent::condition(r, spec.confidence, original.as_ref())).collect();
        }
        doc["wall_s"] = json!(started.elapsed().as_secs_f64());
        println!("{doc}");
        return Ok(());
    }
    if let Some(sp) = &spec.search {
        if !a.quiet {
            eprintln!(
                "{}: {} (search of {} for {} = {:e})",
                spec.name, spec.description, sp.along, sp.metric, sp.target
            );
        }
        for r in search::search(&spec, &opts)? {
            println!(
                "{} | {} = {:.4} [{:.4}, {:.4}] | {} evaluations{}",
                r.condition.label(),
                r.along,
                r.value,
                r.lo,
                r.hi,
                r.evaluations.len(),
                if r.converged { "" } else { " (not converged)" }
            );
        }
        written(&opts, a);
        return Ok(());
    }
    if !a.quiet {
        eprintln!("{}: {} ({} conditions)", spec.name, spec.description, spec.conditions().len());
    }
    let results = run(&spec, &opts)?;
    for r in &results {
        let metrics: Vec<String> = r
            .record
            .iter()
            .map(|(k, e)| {
                let (lo, hi) = e.interval(spec.confidence);
                format!("{k}={:.4e} [{lo:.3e}, {hi:.3e}]", e.value())
            })
            .collect();
        println!("{} | {} | {} trials | {}", r.condition.label(), r.status.as_str(), r.trials, metrics.join("  "));
    }
    let profile = crate::profile_of(&spec, &results)?;
    if !profile.is_empty() {
        println!("\nprofile: mean time per trial over {} conditions", results.len());
        for row in &profile {
            println!("{:>24}  {:>10.3} ms  {:>5.1} %", row.label, row.time_ms, 100.0 * row.share);
        }
    }
    written(&opts, a);
    Ok(())
}

/// Run a specification and check its expectations; returns the number of failures.
fn verify_one(path: &PathBuf, a: &Args) -> comnum_core::Result<usize> {
    let (spec, _) = budgeted(path, a)?;
    // the reports of the run, as with `run`
    let opts = report_options(&spec, a);
    let (_, checks) = verify::verify_spec(&spec, &opts)?;
    let failed = checks.iter().filter(|c| !c.passed).count();
    let report = a.out.join(format!("{}.verify.json", spec.name));
    verify::write_report(&report, &spec, &opts, &checks)?;
    if a.json {
        let doc = json!({
            "ok": failed == 0,
            "command": "verify",
            "spec": spec.name,
            "provenance": agent::provenance(&spec, &opts),
            "budget_s": agent::budget(a.max_seconds, a.json),
            "checks": checks.iter().map(agent::check_json).collect::<Vec<_>>(),
            "failed": failed,
            "report": report.display().to_string(),
        });
        println!("{doc}");
        return Ok(failed);
    }
    println!("{}: {} checks, {} failed", spec.name, checks.len(), failed);
    for c in &checks {
        println!(
            "  {} {} | {} | {}: expected {:.4e}, measured {:.4e} [{:.3e}, {:.3e}]",
            if c.passed { "ok  " } else { "FAIL" },
            c.name,
            c.condition,
            c.metric,
            c.expected,
            c.measured.value,
            c.measured.lo,
            c.measured.hi
        );
    }
    Ok(failed)
}

fn check_one(path: &PathBuf, json_out: bool) -> comnum_core::Result<()> {
    let spec = load(path)?;
    // a search: the first point it evaluates, for every combination of the other axes
    let mut spec = search::first_points(&spec).unwrap_or(spec);
    // nothing is written
    (spec.csv, spec.json) = (None, None);
    let opts = RunOptions { fast_dev_run: true, batch: Some(1), progress: Some(false), ..RunOptions::default() };
    let r = run(&spec, &opts)?;
    let names: Vec<&String> = r.first().map(|c| c.record.iter().map(|(k, _)| k).collect()).unwrap_or_default();
    if json_out {
        let doc = json!({
            "ok": true,
            "command": "check",
            "spec": spec.name,
            "conditions": r.len(),
            "blocks": spec.link.len(),
            "metrics": names,
        });
        println!("{doc}");
    } else {
        println!("{}: ok, {} conditions, {} blocks, metrics {:?}", path.display(), r.len(), spec.link.len(), names);
    }
    Ok(())
}

/// Pretty JSON, or the error as a configuration error.
fn to_json(v: &impl serde::Serialize) -> comnum_core::Result<String> {
    serde_json::to_string_pretty(v).map_err(|e| comnum_core::Error::Config(format!("cannot serialise the report: {e}")))
}

/// Run the command line on its arguments (without the program name); the exit code is
/// 0 on success, 1 on a failure and 2 on a usage error.
pub fn main(args: impl IntoIterator<Item = String>) -> ExitCode {
    let args: Vec<String> = args.into_iter().collect();
    let json_out = args.iter().any(|s| s == "--json");
    let usage = |msg: &str| {
        if json_out {
            let first = msg.lines().next().unwrap_or(msg);
            println!("{}", json!({"ok": false, "command": args.first(), "error": agent::error("usage", first)}));
        } else {
            eprintln!("{msg}");
        }
        ExitCode::from(2)
    };
    let a = match parse_args(args.clone()) {
        Ok(a) => a,
        Err(msg) => return usage(&msg),
    };
    if ["run", "check", "verify", "conditions"].contains(&a.command.as_str()) && a.files.is_empty() {
        return usage(&format!("no specification given\n\n{USAGE}"));
    }
    if a.json && ["run", "check", "verify"].contains(&a.command.as_str()) {
        return main_json(&a);
    }
    let result: comnum_core::Result<()> = match a.command.as_str() {
        "blocks" => {
            let all = blocks::registry();
            let wanted: Vec<String> = a.files.iter().map(|f| f.display().to_string()).collect();
            let shown: Vec<_> =
                all.iter().filter(|b| wanted.is_empty() || wanted.iter().any(|w| w == b.name)).collect();
            if shown.is_empty() {
                Err(comnum_core::Error::Config(format!("no block {wanted:?}")))
            } else if a.json {
                let all: Vec<_> = shown.iter().map(|b| b.to_json()).collect();
                to_json(&all).map(|j| println!("{j}"))
            } else {
                for b in shown {
                    println!("{}", b.describe());
                }
                Ok(())
            }
        }
        "schema" => to_json(&blocks::json_schema()).map(|j| println!("{j}")),
        "guide" => {
            print!("{}", include_str!("../../GUIDE_AGENTS.md"));
            Ok(())
        }
        "conditions" => a.files.iter().try_for_each(|f| {
            let spec = load(f)?;
            for (i, c) in spec.conditions().iter().enumerate() {
                println!("{i}: {}", c.label());
            }
            Ok(())
        }),
        "check" => a.files.iter().try_for_each(|f| check_one(f, a.json)),
        "run" => a.files.iter().try_for_each(|f| run_one(f, &a)),
        "verify" => {
            let mut failed = 0;
            let r = a.files.iter().try_for_each(|f| verify_one(f, &a).map(|n| failed += n));
            if r.is_ok() && failed > 0 {
                eprintln!("{failed} checks failed");
                return ExitCode::FAILURE;
            }
            r
        }
        other => return usage(&format!("unknown command {other:?}\n\n{USAGE}")),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::FAILURE
        }
    }
}

/// `run`, `check` and `verify` with `--json`: one line per specification, an error as a
/// line of its own (the other specifications still run); the exit code is 1 if one
/// failed.
fn main_json(a: &Args) -> ExitCode {
    let mut failed = false;
    for f in &a.files {
        let r = match a.command.as_str() {
            "run" => run_one(f, a).map(|()| 0),
            "check" => check_one(f, true).map(|()| 0),
            _ => verify_one(f, a),
        };
        match r {
            Ok(n) => failed |= n > 0,
            Err(e) => {
                failed = true;
                let message = e.to_string();
                println!(
                    "{}",
                    json!({"ok": false, "command": a.command, "file": f.display().to_string(), "error": agent::error(agent::error_kind(&e), &message)})
                );
            }
        }
    }
    if failed { ExitCode::FAILURE } else { ExitCode::SUCCESS }
}