openreadout-batch 0.2.0

Measurements across many instrument files as one table, with sample-sheet joins and group summaries
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Running a [`Measure`] over many inputs.
//!
//! 1. **Inputs.** Paths, directories (with `recursive`, their sub-directories), glob patterns
//!    and the data sets an index query selects are expanded in a deterministic order
//!    ([`openreadout_core::batch::expand`]; a directory a reader recognises, such as a Bruker
//!    `.d`, is one input). Excluded paths (the sample sheet itself) are left out.
//! 2. **Probe** (parallel). Every input is opened for its headers: format, the files it names
//!    ([`openreadout_core::Dataset::member_files`]) and the sample fields of its experiment.
//!    A file found by walking a directory that no reader recognises is skipped and counted;
//!    one named explicitly becomes an error row.
//! 3. **Group.** Files that name each other form one multi-file data set, whose primary is
//!    chosen like the index does ([`openreadout_index::group`]); the others are not measured
//!    again and are reported as members.
//! 4. **Measure** (parallel, results in input order). A failure is an error row, never an
//!    abort. With a journal, every finished data set is appended to it and a rerun reuses the
//!    rows of unchanged data sets measured with the same options.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::io::{BufRead, Write as _};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

use openreadout_core::batch::{ExpandOptions, expand};
use openreadout_core::{Error, Registry, Result};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

use crate::measure::{Item, Measure};
use crate::table::{ColumnDoc, Role, Row, Table, Value, assemble};

/// Data sets an index query selects (`openreadout search INDEX "QUERY"`).
#[derive(Debug, Clone, Default)]
pub struct IndexQuery {
    /// The index directory.
    pub index_dir: PathBuf,
    /// The query (`format=fcs channel~CD4`); empty selects every data set.
    pub query: String,
}

/// What to run over.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct BatchRequest {
    /// Files, directories and glob patterns.
    pub inputs: Vec<PathBuf>,
    /// Walk sub-directories of directory inputs.
    pub recursive: bool,
    /// Add the data sets an index query selects.
    pub index: Option<IndexQuery>,
    /// Paths never measured (sample sheets and layouts passed alongside).
    pub exclude: Vec<PathBuf>,
    /// Only data sets of these format ids (`fcs`, `czi`, …); others are skipped and counted.
    pub formats: Vec<String>,
    /// Leave out explicitly named inputs no reader recognises (walked ones always are).
    pub skip_unknown: bool,
    /// Stop at the first failure (an error is returned instead of a table).
    pub fail_fast: bool,
    /// Append finished data sets here, and reuse the rows of unchanged ones found in it.
    pub journal: Option<PathBuf>,
}

/// Sample fields a data set records (from its experiment model), used to join sample sheets.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SampleKeys {
    /// `experiment.sample.id`.
    pub id: Option<String>,
    /// `experiment.sample.name`.
    pub name: Option<String>,
    /// `experiment.sample.well`.
    pub well: Option<String>,
    /// `experiment.sample.barcode`.
    pub barcode: Option<String>,
    /// `experiment.sample.sequence_position`.
    pub position: Option<String>,
    /// `experiment.acquisition.started_at`.
    pub started_at: Option<String>,
}

/// One data set of the batch (a row source).
#[derive(Debug, Clone, Default)]
pub struct DatasetRef {
    /// The path as expanded (as given, or found under a directory argument).
    pub path: PathBuf,
    /// The path relative to the argument it was found under.
    pub relative: PathBuf,
    /// Format id, when recognised.
    pub format: Option<String>,
    /// Other files of a multi-file data set.
    pub members: Vec<PathBuf>,
    /// Its sample fields.
    pub keys: SampleKeys,
}

/// How the inputs turned into data sets.
#[derive(Debug, Clone, Default, Serialize, schemars::JsonSchema)]
pub struct InputReport {
    /// Data sets measured (rows come from these), including those that failed.
    pub datasets: u64,
    /// Data sets measured without error.
    pub ok: u64,
    /// Data sets that failed (their rows carry `error`).
    pub failed: u64,
    /// Files found by walking a directory that no reader recognises (not in the table).
    pub skipped_unknown: u64,
    /// Data sets of other formats left out by the format filter.
    pub skipped_format: u64,
    /// Sample sheets and layouts found among the inputs (never measured).
    pub skipped_sheets: u64,
    /// Files that belong to another input's multi-file data set (measured once, with it).
    pub grouped_members: u64,
    /// A few of the skipped paths, with why.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skipped_examples: Vec<String>,
    /// Data sets whose rows were reused from the journal of an interrupted run.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub reused: u64,
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_zero(v: &u64) -> bool {
    *v == 0
}

/// A batch run's result: the table and what went into it.
#[derive(Debug, Clone, Default)]
pub struct BatchResult {
    /// Measure id.
    pub measure: String,
    /// The measure's grain keys.
    pub grain: Vec<String>,
    /// Counts.
    pub inputs: InputReport,
    /// The table (identity, keys, metadata, values, errors).
    pub table: Table,
    /// The data sets, in order.
    pub datasets: Vec<DatasetRef>,
    /// For each table row, its data set (index into `datasets`).
    pub row_dataset: Vec<usize>,
    /// Things to know (grouping, skipped inputs).
    pub warnings: Vec<String>,
}

/// Columns the runner adds.
pub fn identity_docs() -> Vec<ColumnDoc> {
    vec![
        ColumnDoc {
            name: "path",
            role: Role::Id,
            unit: None,
            description: "the data set (file or directory data set), as found",
        },
        ColumnDoc {
            name: "format",
            role: Role::Id,
            unit: None,
            description: "format id",
        },
        ColumnDoc {
            name: "files",
            role: Role::Id,
            unit: None,
            description: "files in a multi-file data set (the path plus the members grouped with it)",
        },
        ColumnDoc::meta(
            "sample_id",
            "experiment.sample.id: the sample id the file records",
        ),
        ColumnDoc::meta(
            "sample_well",
            "experiment.sample.well: the plate well the file records",
        ),
        ColumnDoc {
            name: "error",
            role: Role::Error,
            unit: None,
            description: "why this data set has no values (null when it has)",
        },
        ColumnDoc {
            name: "error_code",
            role: Role::Error,
            unit: None,
            description: "unknown_format, corrupt_file, unsupported_feature, io, usage or error",
        },
    ]
}

/// Progress callback: (data sets done, total, current path).
pub type Progress<'a> = &'a (dyn Fn(u64, u64, &str) + Sync);

/// Rows of one data set, handed to a sink in input order as soon as they are ready.
pub type Sink<'a> = &'a (dyn Fn(&DatasetRef, &[Row]) + Sync);

struct Probe {
    ds: DatasetRef,
    explicit: bool,
    size: u64,
    mtime_ns: Option<i128>,
    error: Option<Error>,
    /// Rows of a headers-only measure.
    rows: Option<Result<Vec<Row>>>,
}

fn stamp(p: &Path) -> (u64, Option<i128>) {
    std::fs::metadata(p).map_or((0, None), |m| {
        let t = m
            .modified()
            .ok()
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_nanos() as i128);
        (m.len(), t)
    })
}

fn keys_of(
    ds: &dyn openreadout_core::Dataset,
    info: &openreadout_core::model::FileInfo,
) -> SampleKeys {
    let e = openreadout_core::experiment::of_dataset(ds, info);
    let s = e.sample.unwrap_or_default();
    SampleKeys {
        id: s.id,
        name: s.name,
        well: s.well,
        barcode: s.barcode,
        position: s.sequence_position,
        started_at: e.acquisition.and_then(|a| a.started_at),
    }
}

fn probe(
    reg: &Registry,
    measure: &dyn Measure,
    path: &Path,
    relative: &Path,
    explicit: bool,
) -> Probe {
    let (size, mtime_ns) = stamp(path);
    let mut p = Probe {
        ds: DatasetRef {
            path: path.to_path_buf(),
            relative: relative.to_path_buf(),
            ..Default::default()
        },
        explicit,
        size,
        mtime_ns,
        error: None,
        rows: None,
    };
    let (det, mut ds) = match reg.open(path) {
        Ok(x) => x,
        Err(e) => {
            if !matches!(e, Error::UnknownFormat { .. }) {
                p.ds.format = reg.detect(path).ok().map(|(_, d)| d.format_id.to_string());
            }
            p.error = Some(e);
            return p;
        }
    };
    p.ds.format = Some(det.format_id.to_string());
    let info = match ds.info() {
        Ok(i) => i,
        Err(e) => {
            p.error = Some(e);
            return p;
        }
    };
    p.ds.members = ds.member_files();
    p.ds.keys = keys_of(ds.as_ref(), &info);
    if measure.headers_only() {
        p.rows = Some(measure.rows(&mut Item {
            path,
            registry: reg,
            dataset: ds.as_mut(),
            info: &info,
        }));
    }
    p
}

fn measure_one(reg: &Registry, measure: &dyn Measure, path: &Path) -> Result<Vec<Row>> {
    let (_, mut ds) = reg.open(path)?;
    let info = ds.info()?;
    measure.rows(&mut Item {
        path,
        registry: reg,
        dataset: ds.as_mut(),
        info: &info,
    })
}

#[derive(Serialize, Deserialize)]
struct JournalEntry {
    path: String,
    size: u64,
    mtime_ns: Option<i128>,
    fingerprint: String,
    #[serde(default)]
    rows: Vec<Vec<(String, Value)>>,
    #[serde(default)]
    error: Option<(String, String)>,
}

fn read_journal(p: &Path) -> HashMap<String, JournalEntry> {
    let mut out = HashMap::new();
    let Ok(f) = std::fs::File::open(p) else {
        return out;
    };
    for line in std::io::BufReader::new(f)
        .lines()
        .map_while(std::result::Result::ok)
    {
        if let Ok(e) = serde_json::from_str::<JournalEntry>(&line) {
            out.insert(e.path.clone(), e);
        }
    }
    out
}

/// An error rebuilt from a journal entry (code and message only).
fn journal_error(code: &str, message: &str) -> Error {
    match code {
        "corrupt_file" => Error::corrupt("batch", message.to_string()),
        "unsupported_feature" => Error::Unsupported {
            format: "batch",
            feature: message.to_string(),
            hint: None,
        },
        "usage" => Error::Usage(message.to_string()),
        _ => Error::Other(message.to_string()),
    }
}

fn norm(p: &Path) -> PathBuf {
    std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
}

/// Run `measure` over the request's inputs (see the module documentation).
pub fn run(
    reg: &Registry,
    measure: &dyn Measure,
    req: &BatchRequest,
    progress: Option<Progress<'_>>,
    sink: Option<Sink<'_>>,
) -> Result<BatchResult> {
    // 1. inputs
    let mut args = req.inputs.clone();
    if let Some(q) = &req.index {
        let mut sr = openreadout_index::SearchRequest::default();
        sr.query = q.query.clone();
        sr.limit = Some(0);
        sr.fields = vec!["path".into()];
        let hits = openreadout_index::search(&q.index_dir, &sr)?;
        for r in hits.results {
            if let Some(p) = r.get("path").and_then(|v| v.as_str()) {
                args.push(PathBuf::from(p));
            }
        }
        if args.is_empty() {
            return Err(Error::Usage(format!(
                "the index query `{}` selects no data set",
                q.query
            )));
        }
    }
    if args.is_empty() {
        return Err(Error::Usage("no input given".into()));
    }
    let exp = expand(reg, &args, {
        let mut o = ExpandOptions::default();
        o.recursive = req.recursive;
        o.glob = true;
        o.dataset_dir = Some(openreadout_index::walk::is_dataset_dir);
        o
    });
    let excluded: HashSet<PathBuf> = req.exclude.iter().map(|p| norm(p)).collect();
    let named: HashSet<PathBuf> = args.iter().map(|p| norm(p)).collect();
    let mut report = InputReport::default();
    let mut warnings = Vec::new();
    let mut items = Vec::new();
    let mut expansion_errors = Vec::new();
    for it in exp.items {
        if let Some(e) = it.error {
            expansion_errors.push((it.path, it.relative, e));
            continue;
        }
        let n = norm(&it.path);
        if excluded.contains(&n) {
            report.skipped_sheets += 1;
            continue;
        }
        let explicit = named.contains(&n);
        items.push((it.path, it.relative, explicit));
    }
    // 2. probe
    let total = items.len() as u64;
    let done = AtomicU64::new(0);
    let stop = AtomicBool::new(false);
    // Data sets run on a pool of their own: a measure may itself read planes in parallel from a
    // helper thread on the global pool and wait for it (`openreadout_core::parallel`); if the
    // data sets ran on the global pool too, every worker could end up waiting on work only a
    // worker can do.
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(rayon::current_num_threads().max(1))
        .thread_name(|i| format!("batch-{i}"))
        .build()
        .map_err(|e| Error::Other(format!("cannot start the batch thread pool: {e}")))?;
    let probes: Vec<Probe> = pool.install(|| {
        items
            .par_iter()
            .map(|(p, rel, explicit)| {
                let r = probe(reg, measure, p, rel, *explicit);
                if let Some(cb) = progress {
                    let d = done.fetch_add(1, Ordering::Relaxed) + 1;
                    if measure.headers_only() {
                        cb(d, total, &p.display().to_string());
                    }
                }
                r
            })
            .collect()
    });
    // skip unknown walked files, filtered formats
    let mut kept: Vec<Probe> = Vec::new();
    for p in probes {
        let unknown = matches!(p.error, Some(Error::UnknownFormat { .. }));
        if unknown && (!p.explicit || req.skip_unknown) {
            report.skipped_unknown += 1;
            if report.skipped_examples.len() < 10 {
                let what = crate::companion::classify(&p.ds.path)
                    .map_or("not an instrument file", |k| k.describe());
                report
                    .skipped_examples
                    .push(format!("{} ({what})", p.ds.path.display()));
            }
            continue;
        }
        if !req.formats.is_empty()
            && !p
                .ds
                .format
                .as_ref()
                .is_some_and(|f| req.formats.iter().any(|w| w.eq_ignore_ascii_case(f)))
            && (!unknown || !p.explicit)
        {
            report.skipped_format += 1;
            if report.skipped_examples.len() < 10 {
                report.skipped_examples.push(format!(
                    "{} (format {})",
                    p.ds.path.display(),
                    p.ds.format.as_deref().unwrap_or("unknown")
                ));
            }
            continue;
        }
        kept.push(p);
    }
    // 3. group multi-file data sets
    let claims: Vec<openreadout_index::group::Claim> = kept
        .iter()
        .enumerate()
        .filter(|(_, p)| !p.ds.members.is_empty())
        .map(|(i, p)| openreadout_index::group::Claim {
            path: norm(&p.ds.path).to_string_lossy().into_owned(),
            ok: p.error.is_none(),
            members: p
                .ds
                .members
                .iter()
                .map(|m| norm(m).to_string_lossy().into_owned())
                .collect(),
            seq: i as u64,
        })
        .collect();
    let member_of = openreadout_index::group::primaries(&claims);
    let mut grouped: BTreeMap<String, Vec<PathBuf>> = BTreeMap::new();
    let mut primaries: Vec<Probe> = Vec::new();
    for p in kept {
        let key = norm(&p.ds.path).to_string_lossy().into_owned();
        if let Some(primary) = member_of.get(&key) {
            report.grouped_members += 1;
            grouped
                .entry(primary.clone())
                .or_default()
                .push(p.ds.path.clone());
            continue;
        }
        primaries.push(p);
    }
    if report.grouped_members > 0 {
        warnings.push(format!(
            "{} file(s) belong to multi-file data sets and were measured once with them (see the `files` column)",
            report.grouped_members
        ));
    }
    // 4. measure
    let journal = req.journal.as_ref().map(|j| read_journal(j));
    let journal_out = match &req.journal {
        Some(j) => Some(Mutex::new(
            std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(j)
                .map_err(|e| Error::io(j, e))?,
        )),
        None => None,
    };
    let fp = measure.fingerprint();
    let reused = AtomicU64::new(0);
    done.store(0, Ordering::Relaxed);
    let total = primaries.len() as u64;
    // in-order delivery to the sink
    let pending: Mutex<(usize, BTreeMap<usize, Vec<Row>>)> = Mutex::new((0, BTreeMap::new()));
    let with_identity = |p: &Probe, rows: Result<Vec<Row>>| -> Vec<Row> {
        let files = 1 + member_count(&p.ds, &grouped);
        let ident = |mut r: Row| -> Row {
            let mut out = Row::new()
                .with("path", p.ds.path.display().to_string())
                .with("format", Value::text_opt(p.ds.format.as_deref()));
            if files > 1 {
                out.set("files", files as u64);
            }
            for (n, v) in r.cells.drain(..) {
                out.cells.push((n, v));
            }
            if out.get("sample_id").is_none() && p.ds.keys.id.is_some() {
                out.set("sample_id", Value::text_opt(p.ds.keys.id.as_deref()));
            }
            if out.get("well").is_none()
                && out.get("sample_well").is_none()
                && p.ds.keys.well.is_some()
            {
                out.set("sample_well", Value::text_opt(p.ds.keys.well.as_deref()));
            }
            out
        };
        match rows {
            Ok(rows) if !rows.is_empty() => rows.into_iter().map(ident).collect(),
            Ok(_) => vec![ident(Row::new())],
            Err(e) => vec![
                ident(Row::new())
                    .with("error", e.to_string())
                    .with("error_code", e.code()),
            ],
        }
    };
    let outcomes: Vec<(Vec<Row>, bool)> = pool.install(|| {
        primaries
            .par_iter()
            .enumerate()
            .map(|(i, p)| {
                if stop.load(Ordering::Relaxed) {
                    return (Vec::new(), false);
                }
                let path_s = p.ds.path.display().to_string();
                let rows: Result<Vec<Row>> = if let Some(e) = &p.error {
                    Err(clone_error(e))
                } else if let Some(r) = &p.rows {
                    match r {
                        Ok(rows) => Ok(rows.clone()),
                        Err(e) => Err(clone_error(e)),
                    }
                } else if let Some(prev) = journal
                    .as_ref()
                    .and_then(|j| j.get(&path_s))
                    .filter(|e| e.size == p.size && e.mtime_ns == p.mtime_ns && e.fingerprint == fp)
                {
                    reused.fetch_add(1, Ordering::Relaxed);
                    match &prev.error {
                        Some((code, msg)) => Err(journal_error(code, msg)),
                        None => Ok(prev
                            .rows
                            .iter()
                            .map(|cells| Row {
                                cells: cells.clone(),
                            })
                            .collect()),
                    }
                } else {
                    let r = measure_one(reg, measure, &p.ds.path);
                    if let Some(j) = &journal_out {
                        let entry = JournalEntry {
                            path: path_s.clone(),
                            size: p.size,
                            mtime_ns: p.mtime_ns,
                            fingerprint: fp.clone(),
                            rows: r
                                .as_ref()
                                .map(|rows| rows.iter().map(|r| r.cells.clone()).collect())
                                .unwrap_or_default(),
                            error: r
                                .as_ref()
                                .err()
                                .map(|e| (e.code().to_string(), e.to_string())),
                        };
                        if let (Ok(line), Ok(mut f)) = (serde_json::to_string(&entry), j.lock()) {
                            let _ = writeln!(f, "{line}");
                            let _ = f.flush();
                        }
                    }
                    r
                };
                let failed = rows.is_err();
                if failed && req.fail_fast {
                    stop.store(true, Ordering::Relaxed);
                }
                let rows = with_identity(p, rows);
                if let Some(cb) = progress {
                    let d = done.fetch_add(1, Ordering::Relaxed) + 1;
                    cb(d, total, &path_s);
                }
                if let Some(s) = sink
                    && let Ok(mut g) = pending.lock()
                {
                    g.1.insert(i, rows.clone());
                    loop {
                        let next = g.0;
                        let Some(r) = g.1.remove(&next) else { break };
                        s(&primaries[next].ds, &r);
                        g.0 += 1;
                    }
                }
                (rows, !failed)
            })
            .collect()
    });
    if req.fail_fast
        && let Some((i, _)) = outcomes.iter().enumerate().find(|(_, (_, ok))| !ok)
    {
        let p = &primaries[i];
        let msg = outcomes[i]
            .0
            .first()
            .and_then(|r| r.get("error"))
            .map(Value::text)
            .unwrap_or_default();
        return Err(match &p.error {
            Some(e) => clone_error(e),
            None => Error::Other(format!("{}: {msg}", p.ds.path.display())),
        });
    }
    report.reused = reused.load(Ordering::Relaxed);
    // expansion errors (missing paths, bad globs) become error rows at the end
    let mut all_rows = Vec::new();
    let mut row_dataset = Vec::new();
    let mut datasets = Vec::new();
    for (p, (rows, ok)) in primaries.iter().zip(outcomes) {
        report.datasets += 1;
        if ok {
            report.ok += 1;
        } else {
            report.failed += 1;
        }
        let mut d = p.ds.clone();
        if let Some(m) = grouped.get(&norm(&p.ds.path).to_string_lossy().into_owned()) {
            for x in m {
                if !d.members.contains(x) {
                    d.members.push(x.clone());
                }
            }
        }
        for r in rows {
            all_rows.push(r);
            row_dataset.push(datasets.len());
        }
        datasets.push(d);
    }
    for (path, relative, e) in expansion_errors {
        report.datasets += 1;
        report.failed += 1;
        all_rows.push(
            Row::new()
                .with("path", path.display().to_string())
                .with("format", Value::Null)
                .with("error", e.to_string())
                .with("error_code", e.code()),
        );
        row_dataset.push(datasets.len());
        datasets.push(DatasetRef {
            path,
            relative,
            ..Default::default()
        });
    }
    if report.skipped_unknown > 0 {
        warnings.push(format!(
            "{} file(s) found in directories are not instrument files and were left out",
            report.skipped_unknown
        ));
    }
    let mut docs = identity_docs();
    docs.extend(measure.columns());
    let grain = measure.grain();
    let table = assemble(all_rows, &docs, &|n| {
        if grain.iter().any(|g| g == n) {
            Role::Key
        } else {
            Role::Value
        }
    });
    Ok(BatchResult {
        measure: measure.id().to_string(),
        grain,
        inputs: report,
        table,
        datasets,
        row_dataset,
        warnings,
    })
}

fn member_count(d: &DatasetRef, grouped: &BTreeMap<String, Vec<PathBuf>>) -> usize {
    let key = norm(&d.path).to_string_lossy().into_owned();
    let mut all: HashSet<String> = d
        .members
        .iter()
        .map(|m| norm(m).to_string_lossy().into_owned())
        .collect();
    if let Some(g) = grouped.get(&key) {
        all.extend(g.iter().map(|m| norm(m).to_string_lossy().into_owned()));
    }
    all.remove(&key);
    all.len()
}

/// Errors are not `Clone`; rebuild one with the same code and message.
pub fn clone_error(e: &Error) -> Error {
    match e {
        Error::UnknownFormat { path } => Error::UnknownFormat { path: path.clone() },
        Error::Unsupported {
            format,
            feature,
            hint,
        } => Error::Unsupported {
            format,
            feature: feature.clone(),
            hint: hint.clone(),
        },
        Error::Corrupt {
            format,
            detail,
            offset,
        } => Error::Corrupt {
            format,
            detail: detail.clone(),
            offset: *offset,
        },
        Error::Usage(m) => Error::Usage(m.clone()),
        Error::Io { path, source } => Error::io(
            path.clone(),
            std::io::Error::new(source.kind(), source.to_string()),
        ),
        other => Error::Other(other.to_string()),
    }
}