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
//! The tidy table: typed cells, columns with a role and a unit, and rows assembled from the
//! name/value pairs measures produce.

use std::collections::HashMap;
use std::fmt;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// One cell.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(untagged)]
pub enum Value {
    /// No value (JSON `null`, an empty CSV field).
    #[default]
    Null,
    /// `true` / `false`.
    Bool(bool),
    /// A whole number.
    Int(i64),
    /// A real number (NaN and infinities are written as `null`).
    Float(f64),
    /// Text.
    Text(String),
}

impl Value {
    /// The value as a number (integers, floats, and text that is a plain decimal number).
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Int(v) => Some(*v as f64),
            Value::Float(v) if v.is_finite() => Some(*v),
            Value::Text(s) => parse_number(s),
            _ => None,
        }
    }
    /// True for [`Value::Null`] (and non-finite floats).
    pub fn is_null(&self) -> bool {
        matches!(self, Value::Null) || matches!(self, Value::Float(v) if !v.is_finite())
    }
    /// The value as text: empty for null, shortest round-trip form for numbers.
    pub fn text(&self) -> String {
        match self {
            Value::Null => String::new(),
            Value::Bool(b) => b.to_string(),
            Value::Int(v) => v.to_string(),
            Value::Float(v) if !v.is_finite() => String::new(),
            Value::Float(v) => fmt_float(*v),
            Value::Text(s) => s.clone(),
        }
    }
    /// A text value, or null for `None`/empty text.
    pub fn text_opt(s: Option<&str>) -> Value {
        match s.map(str::trim) {
            Some(t) if !t.is_empty() => Value::Text(t.to_string()),
            _ => Value::Null,
        }
    }
    /// A float, or null for `None` and non-finite values.
    pub fn float_opt(v: Option<f64>) -> Value {
        match v {
            Some(x) if x.is_finite() => Value::Float(x),
            _ => Value::Null,
        }
    }
    /// An unsigned count (saturating at `i64::MAX`).
    pub fn count(v: u64) -> Value {
        Value::Int(i64::try_from(v).unwrap_or(i64::MAX))
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.text())
    }
}

impl From<f64> for Value {
    fn from(v: f64) -> Self {
        Value::float_opt(Some(v))
    }
}
impl From<i64> for Value {
    fn from(v: i64) -> Self {
        Value::Int(v)
    }
}
impl From<u64> for Value {
    fn from(v: u64) -> Self {
        Value::count(v)
    }
}
impl From<u32> for Value {
    fn from(v: u32) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<bool> for Value {
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}
impl From<String> for Value {
    fn from(v: String) -> Self {
        Value::Text(v)
    }
}
impl From<&str> for Value {
    fn from(v: &str) -> Self {
        Value::Text(v.to_string())
    }
}
impl<T: Into<Value>> From<Option<T>> for Value {
    fn from(v: Option<T>) -> Self {
        v.map_or(Value::Null, Into::into)
    }
}

/// Shortest text that parses back to the same f64; integers without a fraction.
pub fn fmt_float(v: f64) -> String {
    if v.fract() == 0.0 && v.abs() < 1e15 {
        format!("{v:.0}")
    } else {
        format!("{v}")
    }
}

/// Parse a plain decimal number (`-1.5`, `2.1e3`, `+7`, `1,000` is not one). Rejects `NaN`,
/// `inf`, thousands separators and decimal commas.
pub fn parse_number(s: &str) -> Option<f64> {
    let t = s.trim();
    if t.is_empty() || t.len() > 64 {
        return None;
    }
    let body = t.strip_prefix(['+', '-']).unwrap_or(t);
    let mut digits = 0;
    let mut seen_e = false;
    let mut seen_dot = false;
    let mut prev = ' ';
    for c in body.chars() {
        match c {
            '0'..='9' => digits += 1,
            '.' if !seen_e && !seen_dot => seen_dot = true,
            'e' | 'E' if digits > 0 && !seen_e => seen_e = true,
            '+' | '-' if matches!(prev, 'e' | 'E') => {}
            _ => return None,
        }
        prev = c;
    }
    if digits == 0 {
        return None;
    }
    t.parse::<f64>().ok().filter(|v| v.is_finite())
}

/// Column type, inferred from the cells.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ColumnType {
    /// Text.
    String,
    /// Whole numbers.
    Integer,
    /// Real numbers (a column mixing integers and reals is `float`).
    Float,
    /// `true`/`false`.
    Boolean,
}

/// What a column holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// Which data set a row comes from: `path`, `format`, `files`.
    Id,
    /// The row's position within its data set (the measure's grain): `channel`, `population`,
    /// `well`, `parameter`, …
    Key,
    /// Joined from a sample sheet or plate layout.
    Annotation,
    /// Recorded in the file's own metadata (sample id, well, instrument).
    Metadata,
    /// A measured or computed value.
    Value,
    /// `error`, `error_code`.
    Error,
}

/// One column.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Column {
    /// Name (stable; units that never change are part of it, e.g. `sample_rate_hz`).
    pub name: String,
    /// Inferred type.
    #[serde(rename = "type")]
    pub kind: ColumnType,
    /// Unit of every value in the column, when fixed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
    /// What the column holds.
    pub role: Role,
    /// One line on its meaning.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// What a measure says about a column it produces (a name ending in `*` covers every column
/// with that prefix, e.g. `median:*`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDoc {
    /// Name or `prefix*`.
    pub name: &'static str,
    /// Role (default [`Role::Value`]).
    pub role: Role,
    /// Unit, when fixed.
    pub unit: Option<&'static str>,
    /// Meaning.
    pub description: &'static str,
}

impl ColumnDoc {
    /// A value column.
    pub const fn value(name: &'static str, description: &'static str) -> Self {
        ColumnDoc {
            name,
            role: Role::Value,
            unit: None,
            description,
        }
    }
    /// A value column with a unit.
    pub const fn unit(name: &'static str, unit: &'static str, description: &'static str) -> Self {
        ColumnDoc {
            name,
            role: Role::Value,
            unit: Some(unit),
            description,
        }
    }
    /// A key (grain) column.
    pub const fn key(name: &'static str, description: &'static str) -> Self {
        ColumnDoc {
            name,
            role: Role::Key,
            unit: None,
            description,
        }
    }
    /// A metadata column.
    pub const fn meta(name: &'static str, description: &'static str) -> Self {
        ColumnDoc {
            name,
            role: Role::Metadata,
            unit: None,
            description,
        }
    }
    fn matches(&self, name: &str) -> bool {
        match self.name.strip_suffix('*') {
            Some(prefix) => name.starts_with(prefix),
            None => self.name == name,
        }
    }
}

/// One row as a measure builds it: ordered name/value pairs.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Row {
    /// Cells in column order.
    pub cells: Vec<(String, Value)>,
}

impl Row {
    /// An empty row.
    pub fn new() -> Self {
        Row::default()
    }
    /// Append (or replace) a cell.
    pub fn set(&mut self, name: impl Into<String>, value: impl Into<Value>) -> &mut Self {
        let name = name.into();
        let value = value.into();
        if let Some(c) = self.cells.iter_mut().find(|(n, _)| *n == name) {
            c.1 = value;
        } else {
            self.cells.push((name, value));
        }
        self
    }
    /// Builder form of [`Row::set`].
    #[must_use]
    pub fn with(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
        self.set(name, value);
        self
    }
    /// The value of `name`, if set.
    pub fn get(&self, name: &str) -> Option<&Value> {
        self.cells.iter().find(|(n, _)| n == name).map(|(_, v)| v)
    }
}

/// A table: columns and rows of cells in column order.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Table {
    /// Columns.
    pub columns: Vec<Column>,
    /// Rows; each has one cell per column.
    pub rows: Vec<Vec<Value>>,
}

impl Table {
    /// Index of the column called `name`.
    pub fn index_of(&self, name: &str) -> Option<usize> {
        self.columns.iter().position(|c| c.name == name)
    }
    /// The cell of `row` in column `name` (null when the column is absent).
    pub fn get(&self, row: usize, name: &str) -> &Value {
        static NULL: Value = Value::Null;
        self.index_of(name)
            .and_then(|i| self.rows.get(row).and_then(|r| r.get(i)))
            .unwrap_or(&NULL)
    }
    /// Keep only these columns (in this order); unknown names are an error.
    pub fn select(&mut self, names: &[String]) -> Result<(), String> {
        let mut idx = Vec::new();
        for n in names {
            match self.index_of(n) {
                Some(i) => idx.push(i),
                None => {
                    return Err(format!(
                        "no column `{n}`; the table has: {}",
                        self.columns
                            .iter()
                            .map(|c| c.name.as_str())
                            .collect::<Vec<_>>()
                            .join(", ")
                    ));
                }
            }
        }
        self.columns = idx.iter().map(|&i| self.columns[i].clone()).collect();
        for r in &mut self.rows {
            *r = idx.iter().map(|&i| r[i].clone()).collect();
        }
        Ok(())
    }
    /// Re-infer every column's type from its cells (numbers in text cells stay text).
    pub fn infer_types(&mut self) {
        for (i, c) in self.columns.iter_mut().enumerate() {
            c.kind = infer(self.rows.iter().map(|r| &r[i]));
        }
    }
}

/// The type of a column holding these cells.
pub fn infer<'a>(cells: impl Iterator<Item = &'a Value>) -> ColumnType {
    let (mut int, mut float, mut boolean, mut text) = (false, false, false, false);
    for v in cells {
        match v {
            Value::Null => {}
            Value::Int(_) => int = true,
            Value::Float(_) => float = true,
            Value::Bool(_) => boolean = true,
            Value::Text(_) => text = true,
        }
    }
    if text || (boolean && (int || float)) {
        ColumnType::String
    } else if float {
        ColumnType::Float
    } else if int {
        ColumnType::Integer
    } else if boolean {
        ColumnType::Boolean
    } else {
        ColumnType::String
    }
}

/// Assemble rows into a table: columns in first-seen order (a column first seen in a later
/// row goes after the columns of its role that came before it), typed from their cells,
/// described by `docs`. `role_of` gives the role of columns no doc covers.
pub fn assemble(rows: Vec<Row>, docs: &[ColumnDoc], role_of: &dyn Fn(&str) -> Role) -> Table {
    let mut names: Vec<String> = Vec::new();
    let mut at: HashMap<String, usize> = HashMap::new();
    for r in &rows {
        for (n, _) in &r.cells {
            if !at.contains_key(n) {
                at.insert(n.clone(), names.len());
                names.push(n.clone());
            }
        }
    }
    let role = |n: &str| {
        docs.iter()
            .find(|d| d.matches(n))
            .map_or_else(|| role_of(n), |d| d.role)
    };
    // stable order by role group, first-seen within a group
    let rank = |r: Role| match r {
        Role::Id => 0,
        Role::Key => 1,
        Role::Metadata => 2,
        Role::Annotation => 3,
        Role::Value => 4,
        Role::Error => 5,
    };
    let mut order: Vec<usize> = (0..names.len()).collect();
    order.sort_by_key(|&i| (rank(role(&names[i])), i));
    let pos: HashMap<&str, usize> = order
        .iter()
        .enumerate()
        .map(|(p, &i)| (names[i].as_str(), p))
        .collect();
    let mut out_rows = Vec::with_capacity(rows.len());
    for r in rows {
        let mut cells = vec![Value::Null; names.len()];
        for (n, v) in r.cells {
            if let Some(&p) = pos.get(n.as_str()) {
                cells[p] = v;
            }
        }
        out_rows.push(cells);
    }
    let columns = order
        .iter()
        .enumerate()
        .map(|(p, &i)| {
            let n = &names[i];
            let doc = docs.iter().find(|d| d.matches(n));
            Column {
                name: n.clone(),
                kind: infer(out_rows.iter().map(|r| &r[p])),
                unit: doc.and_then(|d| d.unit.map(str::to_string)),
                role: role(n),
                description: doc.map(|d| d.description.to_string()),
            }
        })
        .collect();
    Table {
        columns,
        rows: out_rows,
    }
}

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

    #[test]
    fn numbers_parse_strictly() {
        assert_eq!(parse_number(" 1.5 "), Some(1.5));
        assert_eq!(parse_number("-2e3"), Some(-2000.0));
        assert_eq!(parse_number("1,000"), None);
        assert_eq!(parse_number("NaN"), None);
        assert_eq!(parse_number("."), None);
        assert_eq!(parse_number("1.2.3"), None);
        assert_eq!(parse_number("A1"), None);
    }

    #[test]
    fn assemble_orders_by_role_then_first_seen_and_types_columns() {
        let docs = [
            ColumnDoc {
                name: "path",
                role: Role::Id,
                unit: None,
                description: "file",
            },
            ColumnDoc::key("channel", "channel"),
            ColumnDoc::unit("rate_hz", "Hz", "rate"),
            ColumnDoc {
                name: "error",
                role: Role::Error,
                unit: None,
                description: "why",
            },
        ];
        let rows = vec![
            Row::new()
                .with("path", "a")
                .with("mean", 1.5)
                .with("channel", 0u32),
            Row::new()
                .with("path", "b")
                .with("error", "bad")
                .with("rate_hz", 10u32)
                .with("mean", Value::Int(2)),
        ];
        let t = assemble(rows, &docs, &|_| Role::Value);
        let names: Vec<&str> = t.columns.iter().map(|c| c.name.as_str()).collect();
        assert_eq!(names, ["path", "channel", "mean", "rate_hz", "error"]);
        assert_eq!(t.columns[2].kind, ColumnType::Float);
        assert_eq!(t.columns[3].unit.as_deref(), Some("Hz"));
        assert_eq!(t.rows[1][1], Value::Null);
        assert_eq!(t.get(1, "error"), &Value::Text("bad".into()));
    }
}