openreadout-em 0.4.0

Readers for electron-microscopy files, part of OpenReadout
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
//! Assurance profiles (`docs/assurance.md`) of the electron-microscopy readers (MRC, Gatan DM,
//! TIA SER, Velox EMD): the variant features of a file and the feature values the development
//! corpus validates. The tables between the GENERATED markers are written by
//! `cargo xtask assurance-audit --write`.

use openreadout_core::FileInfo;
use openreadout_core::assurance::{
    self as a, AssuranceProfile, Basis, FeatureKind as K, Observations, Scope, Validated,
};
use openreadout_core::provenance::Confidence;

pub(crate) static MRC: AssuranceProfile = AssuranceProfile {
    format_id: "mrc",
    observe: observe_mrc,
    validated: MRC_VALIDATED,
    confidence: MRC_CONFIDENCE,
    basis: Basis::OpenSpec,
};

pub(crate) static DM: AssuranceProfile = AssuranceProfile {
    format_id: "dm",
    observe: observe_dm,
    validated: DM_VALIDATED,
    confidence: DM_CONFIDENCE,
    basis: Basis::PriorArt,
};

pub(crate) static SER: AssuranceProfile = AssuranceProfile {
    format_id: "ser",
    observe: observe_ser,
    validated: SER_VALIDATED,
    confidence: SER_CONFIDENCE,
    basis: Basis::PriorArt,
};

pub(crate) static EMD: AssuranceProfile = AssuranceProfile {
    format_id: "emd",
    observe: observe_emd,
    validated: EMD_VALIDATED,
    confidence: EMD_CONFIDENCE,
    basis: Basis::PriorArt,
};

pub(crate) static MDOC: AssuranceProfile = AssuranceProfile {
    format_id: "mdoc",
    observe: observe_mdoc,
    validated: MDOC_VALIDATED,
    confidence: MDOC_CONFIDENCE,
    basis: Basis::PriorArt,
};

fn observe_mdoc(info: &FileInfo) -> Observations {
    let mut o = Observations::default();
    for t in &info.tables {
        if let Some(k) = t
            .extra
            .get("mdoc")
            .and_then(|m| m.get("section_kind"))
            .and_then(|v| v.as_str())
        {
            o.feature(K::Layout, format!("sections {k}"), &[Scope::Metadata]);
        }
    }
    o
}

fn truncated(o: &mut Observations, info: &FileInfo) {
    if info
        .notes
        .iter()
        .any(|n| n.contains("appears truncated") || n.contains("could not be read to the end"))
    {
        o.undecoded(
            "truncated data",
            &[Scope::Pixels],
            "the file ends before the data its header describes; `check` gives the extent",
        );
    }
}

fn observe_mrc(info: &FileInfo) -> Observations {
    let mut o = Observations::default();
    let version = info
        .format_version
        .as_deref()
        .unwrap_or("pre-2014 (no NVERSION)");
    o.feature(K::FormatVersion, version, &[Scope::Metadata, Scope::Pixels]);
    for im in &info.images {
        o.feature(K::SampleLayout, im.pixel_type.ome_name(), &[Scope::Pixels]);
        if let Some(m) = a::extra_text(&im.extra, "mode") {
            o.feature(K::SampleLayout, format!("mode {m}"), &[Scope::Pixels]);
        }
        if let Some(l) = a::extra_str(&im.extra, "layout") {
            o.feature(K::Layout, l, &[Scope::Pixels, Scope::Metadata]);
        }
        if let Some(t) = im
            .extra
            .get("extended_header")
            .and_then(|e| e.get("type"))
            .and_then(serde_json::Value::as_str)
            .filter(|t| !t.is_empty())
        {
            o.feature(
                K::Record,
                format!("extended header {t}"),
                &[Scope::Metadata],
            );
        }
    }
    a::writer_context(&mut o, info);
    if a::has_note(info, "MAPC/MAPR/MAPS = ") {
        o.feature(K::Layout, "permuted axes", &[Scope::Pixels]);
    }
    if a::has_note(info, "gzip-compressed: ") {
        o.feature(K::Codec, "gzip", &[Scope::Pixels, Scope::Metadata]);
    }
    if a::has_note(info, "mode 0 bytes are unsigned") {
        o.feature(K::SampleLayout, "unsigned mode 0 (IMOD)", &[Scope::Pixels]);
    }
    if let Some(n) = a::note_with(info, "pixel size is exactly 1 Ã…") {
        o.assumed("images[].physical_size", n.to_string());
    }
    truncated(&mut o, info);
    o
}

fn observe_dm(info: &FileInfo) -> Observations {
    let mut o = Observations::default();
    if let Some(v) = &info.format_version {
        o.feature(K::FormatVersion, v, &[Scope::Metadata, Scope::Pixels]);
    }
    for im in &info.images {
        if let Some(d) = a::extra_str(&im.extra, "data_type") {
            o.feature(K::SampleLayout, d, &[Scope::Pixels]);
        }
        if let Some(dims) = im
            .extra
            .get("dimensions")
            .and_then(serde_json::Value::as_array)
        {
            o.feature(K::Layout, format!("{}-D", dims.len()), &[Scope::Pixels]);
        }
        if let Some(m) = im
            .extra
            .get("microscope")
            .and_then(|m| m.get("operation_mode"))
            .and_then(serde_json::Value::as_str)
        {
            o.context(K::Acquisition, m);
        }
    }
    a::writer_context(&mut o, info);
    a::instrument_context(&mut o, info);
    for n in info
        .notes
        .iter()
        .filter(|n| n.contains("described but not decoded"))
    {
        o.undecoded("unsupported DataType", &[], n.clone());
    }
    if let Some(n) = a::note_with(info, "axes after the second are flattened into T") {
        o.assumed("images[].size_t", n.to_string());
    }
    truncated(&mut o, info);
    o
}

fn observe_ser(info: &FileInfo) -> Observations {
    let mut o = Observations::default();
    if let Some(v) = &info.format_version {
        o.feature(K::FormatVersion, v, &[Scope::Metadata, Scope::Pixels]);
    }
    for im in &info.images {
        if let Some(d) = a::extra_str(&im.extra, "element_data_type") {
            o.feature(K::SampleLayout, d, &[Scope::Pixels]);
        }
        if let Some(k) = a::extra_str(&im.extra, "element_kind") {
            o.feature(K::Layout, format!("{k} elements"), &[Scope::Pixels]);
        }
    }
    a::writer_context(&mut o, info);
    a::instrument_context(&mut o, info);
    if a::has_note(info, "no .emi sidecar found") {
        o.feature(K::Layout, "without .emi", &[Scope::Metadata]);
    } else {
        o.feature(K::Layout, "with .emi", &[Scope::Metadata]);
    }
    if let Some(n) = a::note_with(info, "taken as metres")
        && info.images.iter().any(|i| i.physical_size.x.is_some())
    {
        o.assumed("images[].physical_size", n.to_string());
    }
    if let Some(n) = a::note_with(info, "complex element data is described but not decoded") {
        o.undecoded("complex elements", &[], n.to_string());
    }
    o
}

fn observe_emd(info: &FileInfo) -> Observations {
    let mut o = Observations::default();
    if let Some(v) = &info.format_version {
        o.feature(K::FormatVersion, v, &[Scope::Metadata, Scope::Pixels]);
    }
    for im in &info.images {
        o.feature(K::SampleLayout, im.pixel_type.ome_name(), &[Scope::Pixels]);
        if let Some(d) = im.instrument.as_ref().and_then(|i| i.detector.as_deref()) {
            o.context(K::Record, format!("detector {d}"));
        }
    }
    a::writer_context(&mut o, info);
    a::instrument_context(&mut o, info);
    if a::has_note(info, "multi-frame Velox images") {
        o.feature(K::Layout, "multi-frame", &[Scope::Pixels]);
    }
    if a::has_note(info, "EDS spectrum images are assembled") {
        o.feature(K::Layout, "eds spectrum image", &[Scope::Pixels]);
        o.feature(K::Codec, "event stream uint16", &[Scope::Pixels]);
    }
    if !info.traces.is_empty() {
        o.feature(K::Record, "eds spectra", &[Scope::Traces]);
    }
    if a::has_note(info, "EELS spectrum images (Data/EelsSpectrumImage)") {
        o.feature(
            K::Layout,
            "eels spectrum image",
            &[Scope::Pixels, Scope::Metadata],
        );
    }
    if let Some(n) = a::note_with(
        info,
        "Velox data other than images is listed but not decoded",
    ) {
        o.undecoded(
            "Velox data other than images and EDS spectra",
            &[],
            n.to_string(),
        );
    }
    truncated(&mut o, info);
    o
}

// BEGIN GENERATED mrc (cargo xtask assurance-audit --write; do not edit)
const MRC_CONFIDENCE: Confidence = Confidence::High;
#[rustfmt::skip]
const MRC_VALIDATED: &[Validated] = &[
    a::row(K::Codec, "gzip", 4, 2, 4),
    a::row(K::Field, "experiment.acquisition.started_at", 0, 0, 2),
    a::row(K::Field, "experiment.instrument.model", 0, 0, 2),
    a::row(K::FormatVersion, "20140", 4, 3, 4),
    a::row(K::FormatVersion, "20141", 4, 4, 4),
    a::row(K::FormatVersion, "pre-2014 (no NVERSION)", 15, 11, 15),
    a::row(K::Layout, "image-stack", 12, 11, 12),
    a::row(K::Layout, "permuted axes", 3, 2, 3),
    a::row(K::Layout, "volume", 11, 7, 11),
    a::row(K::Record, "extended header FEI1", 1, 1, 1),
    a::row(K::Record, "extended header FEI2", 1, 1, 1),
    a::row(K::SampleLayout, "float", 16, 13, 16),
    a::row(K::SampleLayout, "int16", 2, 2, 2),
    a::row(K::SampleLayout, "int8", 2, 2, 2),
    a::row(K::SampleLayout, "mode 0", 2, 2, 2),
    a::row(K::SampleLayout, "mode 1", 2, 2, 2),
    a::row(K::SampleLayout, "mode 2", 16, 13, 16),
    a::row(K::SampleLayout, "mode 6", 3, 2, 3),
    a::row(K::SampleLayout, "uint16", 3, 2, 3),
    a::row(K::Writer, "EPU", 1, 1, 1),
    a::row(K::Writer, "Fei EPU", 1, 1, 1),
    a::row(K::WriterVersion, "EPU 2.9", 1, 1, 1),
    a::row(K::WriterVersion, "Fei EPU 1.2", 1, 1, 1),
];
// END GENERATED mrc

// BEGIN GENERATED dm (cargo xtask assurance-audit --write; do not edit)
const DM_CONFIDENCE: Confidence = Confidence::High;
#[rustfmt::skip]
const DM_VALIDATED: &[Validated] = &[
    a::row(K::Acquisition, "DIFFRACTION", 6, 6, 6),
    a::row(K::Acquisition, "GIF SCANNING", 2, 2, 2),
    a::row(K::Acquisition, "IMAGING", 8, 7, 8),
    a::row(K::Acquisition, "Imaging", 1, 1, 1),
    a::row(K::Acquisition, "SCANNING", 21, 14, 21),
    a::row(K::Acquisition, "STEM", 2, 1, 2),
    a::row(K::Field, "experiment.acquisition.started_at", 0, 0, 22),
    a::row(K::Field, "experiment.instrument.model", 0, 0, 39),
    a::row(K::FormatVersion, "DM3", 51, 19, 51),
    a::row(K::FormatVersion, "DM4", 55, 23, 55),
    a::row(K::FormatVersion, "DM5", 8, 2, 8),
    a::row(K::Instrument, "ARM 200F", 1, 1, 1),
    a::row(K::Instrument, "Example Microscope", 1, 1, 1),
    a::row(K::Instrument, "FEI Tecnai", 3, 2, 3),
    a::row(K::Instrument, "FEI Tecnai Remote TCPIP", 4, 4, 4),
    a::row(K::Instrument, "FEI Titan", 6, 2, 6),
    a::row(K::Instrument, "FEI Titan ultimate", 1, 1, 1),
    a::row(K::Instrument, "JEM-ARM200F", 2, 2, 2),
    a::row(K::Instrument, "JEOL COM", 7, 7, 7),
    a::row(K::Instrument, "JEOL COM, F200", 1, 1, 1),
    a::row(K::Instrument, "NCEM TEAM 0.5", 1, 1, 1),
    a::row(K::Instrument, "NeoARM", 1, 1, 1),
    a::row(K::Instrument, "Nion UltraSTEM 200", 1, 1, 1),
    a::row(K::Instrument, "Tecnai 12", 2, 1, 2),
    a::row(K::Instrument, "Tecnai F20", 1, 1, 1),
    a::row(K::Instrument, "Titan Holo", 1, 1, 1),
    a::row(K::Instrument, "TitanX", 3, 3, 3),
    a::row(K::Instrument, "Ultra55", 2, 1, 2),
    a::row(K::Instrument, "Unknown", 1, 1, 1),
    a::row(K::Instrument, "Zeiss SEM COM", 1, 1, 1),
    a::row(K::Layout, "1-D", 17, 8, 17),
    a::row(K::Layout, "2-D", 67, 30, 67),
    a::row(K::Layout, "3-D", 32, 11, 32),
    a::row(K::SampleLayout, "binary", 2, 1, 2),
    a::row(K::SampleLayout, "complex128", 4, 1, 4),
    a::row(K::SampleLayout, "complex64", 4, 1, 4),
    a::row(K::SampleLayout, "float32", 65, 26, 65),
    a::row(K::SampleLayout, "float64", 2, 1, 2),
    a::row(K::SampleLayout, "int16", 5, 3, 5),
    a::row(K::SampleLayout, "int32", 7, 5, 7),
    a::row(K::SampleLayout, "int8", 2, 1, 2),
    a::row(K::SampleLayout, "packed-complex64", 2, 2, 2),
    a::row(K::SampleLayout, "rgba", 4, 1, 4),
    a::row(K::SampleLayout, "uint16", 8, 5, 8),
    a::row(K::SampleLayout, "uint32", 9, 5, 9),
    a::row(K::SampleLayout, "uint8", 2, 1, 2),
    a::row(K::Writer, "Gatan DigitalMicrograph", 114, 39, 114),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 2.31", 6, 3, 6),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 2.32", 5, 3, 5),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.0", 2, 1, 2),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.20", 2, 1, 2),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.21", 3, 3, 3),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.22", 1, 1, 1),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.31", 2, 1, 2),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.40", 1, 1, 1),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.42", 1, 1, 1),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.43", 4, 4, 4),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.44", 1, 1, 1),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.50", 1, 1, 1),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.51", 3, 3, 3),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.53", 5, 5, 5),
    a::row(K::WriterVersion, "Gatan DigitalMicrograph 3.60", 1, 1, 1),
];
// END GENERATED dm

// BEGIN GENERATED ser (cargo xtask assurance-audit --write; do not edit)
const SER_CONFIDENCE: Confidence = Confidence::High;
#[rustfmt::skip]
const SER_VALIDATED: &[Validated] = &[
    a::row(K::Field, "experiment.instrument.model", 0, 0, 19),
    a::row(K::FormatVersion, "0x0210", 15, 3, 15),
    a::row(K::FormatVersion, "0x0220", 7, 3, 7),
    a::row(K::Instrument, "Microscope TalosF200X 200 kV D6308 XTwin", 4, 1, 4),
    a::row(K::Instrument, "Microscope Tecnai 200 kV D2267 SuperTwin", 3, 1, 3),
    a::row(K::Instrument, "Microscope Tecnai 300 kV D248 SuperTwin", 5, 1, 5),
    a::row(K::Instrument, "Microscope TecnaiOsiris 200 kV D675 AnalyticalTwin", 2, 1, 2),
    a::row(K::Instrument, "Microscope Titan 300 kV D3165 SuperTwin", 1, 1, 1),
    a::row(K::Instrument, "Microscope TitanCubed 300 kV D3128 UltraTwin", 2, 2, 2),
    a::row(K::Instrument, "Microscope TitanCubed 300 kV D3320 SuperTwin", 1, 1, 1),
    a::row(K::Instrument, "TitanCubed D3147 S-Twin", 1, 1, 1),
    a::row(K::Layout, "1d elements", 9, 1, 9),
    a::row(K::Layout, "2d elements", 13, 5, 13),
    a::row(K::Layout, "with .emi", 20, 4, 20),
    a::row(K::Layout, "without .emi", 2, 2, 2),
    a::row(K::SampleLayout, "float32", 1, 1, 1),
    a::row(K::SampleLayout, "int32", 8, 1, 8),
    a::row(K::SampleLayout, "uint16", 10, 5, 10),
    a::row(K::SampleLayout, "uint32", 3, 1, 3),
    a::row(K::Writer, "TIA / ES Vision", 20, 4, 20),
];
// END GENERATED ser

// BEGIN GENERATED emd (cargo xtask assurance-audit --write; do not edit)
const EMD_CONFIDENCE: Confidence = Confidence::High;
#[rustfmt::skip]
const EMD_VALIDATED: &[Validated] = &[
    a::row(K::Codec, "event stream uint16", 1, 1, 1),
    a::row(K::Field, "experiment.acquisition.started_at", 0, 0, 22),
    a::row(K::Field, "experiment.instrument.model", 0, 0, 22),
    a::row(K::FormatVersion, "Berkeley EMD", 4, 1, 4),
    a::row(K::FormatVersion, "Berkeley EMD 0.2", 5, 2, 5),
    a::row(K::FormatVersion, "Berkeley EMD null.null", 4, 1, 4),
    a::row(K::FormatVersion, "Velox 10", 6, 4, 6),
    a::row(K::FormatVersion, "Velox 11", 5, 4, 5),
    a::row(K::FormatVersion, "Velox 7", 2, 1, 2),
    a::row(K::FormatVersion, "Velox 8", 6, 3, 6),
    a::row(K::FormatVersion, "Velox 9", 5, 3, 5),
    a::row(K::Instrument, "Spectra", 11, 9, 11),
    a::row(K::Instrument, "Talos F200S", 1, 1, 1),
    a::row(K::Instrument, "Titan", 9, 4, 9),
    a::row(K::Instrument, "Titan Cubed", 1, 1, 1),
    a::row(K::Layout, "eds spectrum image", 1, 1, 1),
    a::row(K::Layout, "eels spectrum image", 1, 1, 1),
    a::row(K::Layout, "multi-frame", 4, 4, 4),
    a::row(K::Record, "detector BF", 2, 2, 2),
    a::row(K::Record, "detector BF-S", 1, 1, 1),
    a::row(K::Record, "detector BM-Ceta", 6, 4, 6),
    a::row(K::Record, "detector DF-S", 1, 1, 1),
    a::row(K::Record, "detector DF2", 2, 2, 2),
    a::row(K::Record, "detector DF4", 4, 4, 4),
    a::row(K::Record, "detector EELS Strip Detector", 1, 1, 1),
    a::row(K::Record, "detector HAADF", 14, 12, 14),
    a::row(K::Record, "detector SuperXG2", 1, 1, 1),
    a::row(K::Record, "detector SuperXG23 + SuperXG21 + SuperXG24 + SuperXG22", 1, 1, 1),
    a::row(K::Record, "eds spectra", 3, 2, 3),
    a::row(K::SampleLayout, "complex", 5, 2, 5),
    a::row(K::SampleLayout, "double", 1, 1, 1),
    a::row(K::SampleLayout, "float", 7, 3, 7),
    a::row(K::SampleLayout, "int16", 5, 4, 5),
    a::row(K::SampleLayout, "int32", 5, 2, 5),
    a::row(K::SampleLayout, "int64", 2, 2, 2),
    a::row(K::SampleLayout, "uint16", 17, 13, 17),
    a::row(K::SampleLayout, "uint32", 1, 1, 1),
    a::row(K::Writer, "Velox", 22, 14, 22),
    a::row(K::WriterVersion, "Velox 1.15", 1, 1, 1),
    a::row(K::WriterVersion, "Velox 2.11", 3, 2, 3),
    a::row(K::WriterVersion, "Velox 2.15", 4, 3, 4),
    a::row(K::WriterVersion, "Velox 2.9", 3, 1, 3),
    a::row(K::WriterVersion, "Velox 3.12", 3, 2, 3),
    a::row(K::WriterVersion, "Velox 3.17", 6, 5, 6),
    a::row(K::WriterVersion, "Velox 3.21", 1, 1, 1),
    a::row(K::WriterVersion, "Velox 3.24", 1, 1, 1),
];
// END GENERATED emd

// BEGIN GENERATED mdoc (cargo xtask assurance-audit --write; do not edit)
const MDOC_CONFIDENCE: Confidence = Confidence::Medium;
#[rustfmt::skip]
const MDOC_VALIDATED: &[Validated] = &[
    a::row(K::Field, "experiment.acquisition.started_at", 0, 0, 4),
    a::row(K::Field, "experiment.instrument.model", 0, 0, 2),
    a::row(K::Layout, "sections FrameSet", 1, 1, 1),
    a::row(K::Layout, "sections ZValue", 3, 3, 3),
];
// END GENERATED mdoc