1use std::collections::BTreeMap;
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::pixel::PixelType;
10use crate::provenance::{Confidence, ProvenanceMap};
11
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
14pub struct FormatDescriptor {
15 pub id: String,
17 pub name: String,
19 pub vendor: String,
21 pub extensions: Vec<String>,
23 pub family: String,
25 pub can_read: bool,
27 pub can_write: bool,
29 pub confidence: Confidence,
31 pub known_gaps: Vec<String>,
33}
34
35#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
37pub struct PhysicalSize {
38 #[serde(skip_serializing_if = "Option::is_none")]
40 pub x: Option<f64>,
41 #[serde(skip_serializing_if = "Option::is_none")]
43 pub y: Option<f64>,
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub z: Option<f64>,
47 pub unit: String,
49}
50
51impl PhysicalSize {
52 pub fn micrometres(x: Option<f64>, y: Option<f64>, z: Option<f64>) -> Self {
56 let keep = |v: Option<f64>| v.filter(|v| v.is_finite() && *v > 0.0);
57 PhysicalSize {
58 x: keep(x),
59 y: keep(y),
60 z: keep(z),
61 unit: "µm".into(),
62 }
63 }
64 pub fn is_empty(&self) -> bool {
66 self.x.is_none() && self.y.is_none() && self.z.is_none()
67 }
68}
69
70#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
72pub struct ChannelInfo {
73 pub index: u32,
75 #[serde(skip_serializing_if = "Option::is_none")]
77 pub name: Option<String>,
78 #[serde(skip_serializing_if = "Option::is_none")]
80 pub fluorophore: Option<String>,
81 #[serde(skip_serializing_if = "Option::is_none")]
83 pub excitation_nm: Option<f64>,
84 #[serde(skip_serializing_if = "Option::is_none")]
89 pub emission_nm: Option<f64>,
90 #[serde(skip_serializing_if = "Option::is_none")]
92 pub emission_range_nm: Option<[f64; 2]>,
93 #[serde(default, skip_serializing_if = "Option::is_none")]
96 pub emission_band_start_nm: Option<f64>,
97 #[serde(default, skip_serializing_if = "Option::is_none")]
99 pub emission_band_end_nm: Option<f64>,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
102 pub emission_band_center_nm: Option<f64>,
103 #[serde(skip_serializing_if = "Option::is_none")]
105 pub color: Option<String>,
106 #[serde(skip_serializing_if = "Option::is_none")]
110 pub acquisition_mode: Option<String>,
111 #[serde(skip_serializing_if = "Option::is_none")]
113 pub exposure_ms: Option<f64>,
114}
115
116#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
118pub struct ObjectiveInfo {
119 #[serde(skip_serializing_if = "Option::is_none")]
121 pub model: Option<String>,
122 #[serde(skip_serializing_if = "Option::is_none")]
124 pub nominal_magnification: Option<f64>,
125 #[serde(skip_serializing_if = "Option::is_none")]
127 pub lens_na: Option<f64>,
128 #[serde(skip_serializing_if = "Option::is_none")]
130 pub immersion: Option<String>,
131}
132
133#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
135pub struct InstrumentInfo {
136 #[serde(skip_serializing_if = "Option::is_none")]
138 pub manufacturer: Option<String>,
139 #[serde(skip_serializing_if = "Option::is_none")]
141 pub model: Option<String>,
142 #[serde(skip_serializing_if = "Option::is_none")]
144 pub software: Option<String>,
145 #[serde(skip_serializing_if = "Option::is_none")]
147 pub software_version: Option<String>,
148 #[serde(skip_serializing_if = "Option::is_none")]
150 pub detector: Option<String>,
151}
152
153#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
155pub struct MosaicInfo {
156 pub tile_count: u32,
158 #[serde(skip_serializing_if = "Option::is_none")]
160 pub tile_width: Option<u32>,
161 #[serde(skip_serializing_if = "Option::is_none")]
163 pub tile_height: Option<u32>,
164 pub stitched_on_read: bool,
166}
167
168#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
170pub struct ImageInfo {
171 pub index: u32,
173 #[serde(skip_serializing_if = "Option::is_none")]
175 pub name: Option<String>,
176 pub size_x: u32,
178 pub size_y: u32,
180 pub size_z: u32,
182 pub size_c: u32,
184 pub size_t: u32,
186 pub dimension_order: String,
188 pub pixel_type: PixelType,
190 pub samples_per_pixel: u32,
192 pub physical_size: PhysicalSize,
194 #[serde(skip_serializing_if = "Option::is_none")]
196 pub time_increment_s: Option<f64>,
197 pub channels: Vec<ChannelInfo>,
199 #[serde(skip_serializing_if = "Option::is_none")]
201 pub objective: Option<ObjectiveInfo>,
202 #[serde(skip_serializing_if = "Option::is_none")]
204 pub instrument: Option<InstrumentInfo>,
205 #[serde(skip_serializing_if = "Option::is_none")]
207 pub acquired_at: Option<String>,
208 #[serde(skip_serializing_if = "Option::is_none")]
210 pub mosaic: Option<MosaicInfo>,
211 pub pyramid_levels: u32,
213 #[serde(default, skip_serializing_if = "Vec::is_empty")]
217 pub resolution_levels: Vec<crate::region::ResolutionLevel>,
218 pub plane_count: u64,
220 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
222 pub extra: BTreeMap<String, serde_json::Value>,
223}
224
225impl ImageInfo {
226 pub fn new(index: u32, size_x: u32, size_y: u32, pixel_type: PixelType) -> Self {
228 ImageInfo {
229 index,
230 name: None,
231 size_x,
232 size_y,
233 size_z: 1,
234 size_c: 1,
235 size_t: 1,
236 dimension_order: "XYCZT".into(),
237 pixel_type,
238 samples_per_pixel: 1,
239 physical_size: PhysicalSize::micrometres(None, None, None),
240 time_increment_s: None,
241 channels: Vec::new(),
242 objective: None,
243 instrument: None,
244 acquired_at: None,
245 mosaic: None,
246 pyramid_levels: 1,
247 resolution_levels: Vec::new(),
248 plane_count: 1,
249 extra: BTreeMap::new(),
250 }
251 }
252
253 pub fn finish(mut self) -> Self {
256 self.plane_count = u64::from(self.size_z)
258 .saturating_mul(u64::from(self.size_c))
259 .saturating_mul(u64::from(self.size_t));
260 for c in &mut self.channels {
261 c.fill_band();
262 c.acquisition_mode = c
264 .acquisition_mode
265 .as_deref()
266 .and_then(crate::acquisition_mode::label);
267 }
268 for v in [
271 &mut self.physical_size.x,
272 &mut self.physical_size.y,
273 &mut self.physical_size.z,
274 &mut self.time_increment_s,
275 ] {
276 *v = v.map(round_noise);
277 }
278 if let Some(o) = &mut self.objective {
279 o.lens_na = o.lens_na.map(round_noise);
280 o.nominal_magnification = o.nominal_magnification.map(round_noise);
281 }
282 if let Some(o) = &mut self.objective {
284 o.model = trimmed(o.model.take());
285 o.immersion = o.immersion.as_deref().and_then(immersion_label);
286 }
287 if let Some(i) = &mut self.instrument {
288 for s in [
289 &mut i.manufacturer,
290 &mut i.model,
291 &mut i.software,
292 &mut i.software_version,
293 &mut i.detector,
294 ] {
295 *s = trimmed(s.take());
296 }
297 }
298 self
299 }
300}
301
302pub fn round_noise(v: f64) -> f64 {
305 if !v.is_finite() || v == 0.0 {
306 return v;
307 }
308 format!("{v:.14e}").parse().unwrap_or(v)
309}
310
311fn trimmed(s: Option<String>) -> Option<String> {
312 let s = s?;
313 let t = s.trim();
314 if t.is_empty() {
315 None
316 } else if t.len() == s.len() {
317 Some(s)
318 } else {
319 Some(t.to_string())
320 }
321}
322
323pub(crate) fn immersion_label(raw: &str) -> Option<String> {
327 let t = raw.trim();
328 if t.is_empty() {
329 return None;
330 }
331 Some(
332 match t.to_ascii_lowercase().as_str() {
333 "oil" => "Oil",
334 "water" => "Water",
335 "air" | "dry" => "Air",
336 "glycerol" | "glycerin" | "glycerine" => "Glycerol",
337 "multi" => "Multi",
338 "other" => "Other",
339 _ => t,
340 }
341 .to_string(),
342 )
343}
344
345impl ChannelInfo {
346 pub fn set_band(&mut self, start_nm: f64, end_nm: f64) {
349 if start_nm.is_finite() && end_nm.is_finite() && start_nm > 0.0 && end_nm >= start_nm {
350 self.emission_range_nm = Some([start_nm, end_nm]);
351 self.emission_band_start_nm = Some(start_nm);
352 self.emission_band_end_nm = Some(end_nm);
353 self.emission_band_center_nm = Some(f64::midpoint(start_nm, end_nm));
354 }
355 }
356
357 pub fn fill_band(&mut self) {
359 if self.emission_band_start_nm.is_none()
360 && let Some([a, b]) = self.emission_range_nm
361 {
362 let (lo, hi) = if a <= b { (a, b) } else { (b, a) };
363 if lo.is_finite() && hi.is_finite() {
364 self.emission_band_start_nm = Some(lo);
365 self.emission_band_end_nm = Some(hi);
366 self.emission_band_center_nm = Some(f64::midpoint(lo, hi));
367 }
368 }
369 }
370}
371
372#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
374pub struct FileInfo {
375 pub path: String,
377 pub size_bytes: u64,
379 pub format: FormatDescriptor,
381 #[serde(skip_serializing_if = "Option::is_none")]
383 pub format_version: Option<String>,
384 pub images: Vec<ImageInfo>,
386 #[serde(default, skip_serializing_if = "Vec::is_empty")]
388 pub tables: Vec<TableInfo>,
389 #[serde(default, skip_serializing_if = "Vec::is_empty")]
391 pub spectra: Vec<SpectraInfo>,
392 #[serde(default, skip_serializing_if = "Vec::is_empty")]
394 pub traces: Vec<TraceInfo>,
395 pub plane_count: u64,
397 #[serde(default, skip_serializing_if = "Vec::is_empty")]
399 pub notes: Vec<String>,
400}
401
402#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
404pub struct Dump {
405 pub file: crate::experiment::InfoOutput,
407 #[serde(skip_serializing_if = "Option::is_none")]
409 pub vendor: Option<serde_json::Value>,
410 pub provenance: ProvenanceMap,
412}
413
414#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
416pub struct AttachmentInfo {
417 pub index: u32,
419 pub name: String,
421 pub content_type: String,
423 pub extension: String,
425 #[serde(skip_serializing_if = "Option::is_none")]
427 pub offset: Option<u64>,
428 pub size: u64,
430 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
432 pub extra: BTreeMap<String, serde_json::Value>,
433}
434
435#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
437pub struct ExtractOutput {
438 pub path: String,
440 pub format: String,
442 pub attachment: AttachmentInfo,
444 pub output: String,
446 pub bytes_written: u64,
448 pub xxh3: String,
450 pub verified: bool,
452}
453
454#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
456pub struct LsEntry {
457 pub kind: String,
459 pub name: String,
461 #[serde(skip_serializing_if = "Option::is_none")]
463 pub offset: Option<u64>,
464 #[serde(skip_serializing_if = "Option::is_none")]
466 pub size: Option<u64>,
467 #[serde(skip_serializing_if = "Option::is_none")]
469 pub image: Option<u32>,
470 #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
472 pub details: serde_json::Value,
473}
474
475#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
477pub struct Listing {
478 pub path: String,
480 pub format: String,
482 pub entries: Vec<LsEntry>,
484 #[serde(default, skip_serializing_if = "Option::is_none")]
487 pub acquisition: Option<crate::live::Acquisition>,
488 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
490 pub entries_total: BTreeMap<String, u64>,
491 #[serde(default, skip_serializing_if = "Vec::is_empty")]
493 pub notes: Vec<String>,
494}
495
496impl Listing {
497 pub fn cap(&mut self, max: usize, kind: Option<&str>, first: usize) {
503 let mut totals: BTreeMap<String, u64> = BTreeMap::new();
504 for e in &self.entries {
505 *totals.entry(e.kind.clone()).or_default() += 1;
506 }
507 let total = self.entries.len();
508 let mut seen: BTreeMap<String, usize> = BTreeMap::new();
509 self.entries.retain(|e| {
510 let n = seen.entry(e.kind.clone()).or_default();
511 let i = *n;
512 *n += 1;
513 match kind {
514 Some(k) => e.kind == k && i >= first && (max == 0 || i - first < max),
515 None => max == 0 || i < max,
516 }
517 });
518 if self.entries.len() == total {
519 return;
520 }
521 let left_out: Vec<String> = totals
522 .iter()
523 .filter_map(|(k, &n)| {
524 let shown = self.entries.iter().filter(|e| &e.kind == k).count() as u64;
525 (shown < n).then(|| format!("{k} {shown} of {n}"))
526 })
527 .collect();
528 self.notes.push(format!(
529 "entries lists part of the {total} elements ({}); pass kind and first_entry to page through one kind, or max_entries 0 for all",
530 left_out.join(", ")
531 ));
532 self.entries_total = totals;
533 }
534}
535
536#[derive(
538 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
539)]
540#[serde(rename_all = "lowercase")]
541#[non_exhaustive]
542pub enum Severity {
543 Info,
545 Warning,
547 Error,
549}
550
551impl Severity {
552 pub fn as_str(self) -> &'static str {
554 match self {
555 Severity::Info => "info",
556 Severity::Warning => "warning",
557 Severity::Error => "error",
558 }
559 }
560}
561
562#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
564pub struct Finding {
565 pub severity: Severity,
567 pub code: String,
569 pub message: String,
571 #[serde(skip_serializing_if = "Option::is_none")]
573 pub offset: Option<u64>,
574}
575
576impl Finding {
577 pub fn error(code: &str, message: impl Into<String>) -> Self {
579 Finding {
580 severity: Severity::Error,
581 code: code.into(),
582 message: message.into(),
583 offset: None,
584 }
585 }
586 pub fn warning(code: &str, message: impl Into<String>) -> Self {
588 Finding {
589 severity: Severity::Warning,
590 code: code.into(),
591 message: message.into(),
592 offset: None,
593 }
594 }
595 pub fn info(code: &str, message: impl Into<String>) -> Self {
597 Finding {
598 severity: Severity::Info,
599 code: code.into(),
600 message: message.into(),
601 offset: None,
602 }
603 }
604 pub fn at(mut self, offset: u64) -> Self {
606 self.offset = Some(offset);
607 self
608 }
609}
610
611#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
613pub struct CheckReport {
614 pub path: String,
616 pub format: String,
618 pub ok: bool,
620 pub checks_performed: Vec<String>,
622 pub findings: Vec<Finding>,
624 #[serde(default, skip_serializing_if = "Option::is_none")]
628 pub acquisition: Option<crate::live::Acquisition>,
629 #[serde(default, skip_serializing_if = "Option::is_none")]
633 pub assurance: Option<crate::assurance::Assurance>,
634}
635
636impl CheckReport {
637 pub fn new(path: impl Into<String>, format: impl Into<String>) -> Self {
639 CheckReport {
640 path: path.into(),
641 format: format.into(),
642 ok: true,
643 checks_performed: Vec::new(),
644 findings: Vec::new(),
645 acquisition: None,
646 assurance: None,
647 }
648 }
649 pub fn performed(&mut self, what: impl Into<String>) {
651 self.checks_performed.push(what.into());
652 }
653 pub fn push(&mut self, f: Finding) {
655 if f.severity == Severity::Error {
656 self.ok = false;
657 }
658 self.findings.push(f);
659 }
660}
661
662#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
664pub struct FormatsOutput {
665 pub formats: Vec<FormatDescriptor>,
667}
668
669#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
671pub struct DetectOutput {
672 pub path: String,
674 pub format: String,
676 pub name: String,
678 pub confidence: DetectConfidence,
680 #[serde(skip_serializing_if = "Option::is_none")]
682 pub note: Option<String>,
683}
684
685#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
687#[serde(rename_all = "kebab-case")]
688#[non_exhaustive]
689pub enum DetectConfidence {
690 Definite,
692 Likely,
694 ExtensionOnly,
696}
697
698#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
700pub struct PlaneHash {
701 pub image: u32,
703 #[serde(default, skip_serializing_if = "is_zero")]
705 pub level: u32,
706 #[serde(default, skip_serializing_if = "Option::is_none")]
709 pub region: Option<crate::region::Region>,
710 pub c: u32,
712 pub z: u32,
714 pub t: u32,
716 pub width: u32,
718 pub height: u32,
720 pub pixel_type: PixelType,
722 pub samples_per_pixel: u32,
724 pub xxh3: String,
726}
727
728#[allow(clippy::trivially_copy_pass_by_ref)]
729fn is_zero(v: &u32) -> bool {
730 *v == 0
731}
732
733#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
735pub struct PlanesOutput {
736 pub path: String,
738 pub format: String,
740 pub planes: Vec<PlaneHash>,
742 #[serde(default, skip_serializing_if = "Option::is_none")]
745 pub acquisition: Option<crate::live::Acquisition>,
746}
747
748#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
752pub struct ColumnInfo {
753 pub index: u32,
755 pub name: String,
757 #[serde(skip_serializing_if = "Option::is_none")]
759 pub label: Option<String>,
760 pub dtype: String,
762 #[serde(skip_serializing_if = "Option::is_none")]
764 pub unit: Option<String>,
765 #[serde(skip_serializing_if = "Option::is_none")]
767 pub range: Option<[f64; 2]>,
768 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
770 pub extra: BTreeMap<String, serde_json::Value>,
771}
772
773#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
775pub struct TableInfo {
776 pub index: u32,
778 #[serde(skip_serializing_if = "Option::is_none")]
780 pub name: Option<String>,
781 pub row_count: u64,
783 pub columns: Vec<ColumnInfo>,
785 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
787 pub extra: BTreeMap<String, serde_json::Value>,
788}
789
790#[derive(Debug, Clone, Default)]
792pub struct Table {
793 pub table: u32,
795 pub first_row: u64,
797 pub columns: Vec<Vec<f64>>,
799}
800
801#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
803pub struct TableSlice {
804 pub path: String,
806 pub format: String,
808 pub table: u32,
810 pub first_row: u64,
812 pub total_rows: u64,
814 pub columns: Vec<String>,
816 pub labels: Vec<Option<String>>,
818 pub rows: Vec<Vec<f64>>,
821 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
825 pub texts: BTreeMap<String, Vec<String>>,
826 pub truncated: bool,
828 #[serde(default, skip_serializing_if = "Option::is_none")]
831 pub processing: Option<crate::flow::TableProcessing>,
832 #[serde(default, skip_serializing_if = "Option::is_none")]
835 pub filter: Option<TableFilterSummary>,
836}
837
838impl TableSlice {
839 pub fn attach_texts(&mut self, table: &TableInfo) {
842 for (c, col) in table.columns.iter().enumerate() {
843 let Some(cats) = col.extra.get("categories").and_then(|v| v.as_array()) else {
844 continue;
845 };
846 let text = |v: f64| -> String {
847 if v.is_finite() && v >= 0.0 {
848 cats.get(v as usize)
849 .and_then(|t| t.as_str())
850 .unwrap_or_default()
851 .to_string()
852 } else {
853 String::new()
854 }
855 };
856 let values = self
857 .rows
858 .iter()
859 .map(|r| r.get(c).copied().map(text).unwrap_or_default())
860 .collect();
861 self.texts.insert(col.name.clone(), values);
862 }
863 }
864}
865
866#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
868pub struct TableFilterSummary {
869 pub conditions: Vec<String>,
872 pub matched_rows: u64,
874 pub total_rows: u64,
876 pub percent: f64,
878 pub values: String,
881}
882
883#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
885pub struct TableExportReport {
886 pub input: String,
888 pub output: String,
890 pub format: String,
892 pub table: u32,
894 pub first_row: u64,
896 pub rows_written: u64,
898 pub columns_written: u32,
900 pub header_lines: u32,
902 pub bytes_written: u64,
904 pub verified: bool,
906}
907
908#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
910pub struct TraceExportReport {
911 pub input: String,
913 pub output: String,
915 pub format: String,
917 pub trace: u32,
919 pub sweep: u32,
921 pub first_sample: u64,
923 pub samples_written: u64,
925 pub channels_written: u32,
927 pub bytes_written: u64,
929 pub verified: bool,
931}
932
933#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
935pub struct SpectraInfo {
936 pub index: u32,
938 #[serde(skip_serializing_if = "Option::is_none")]
940 pub name: Option<String>,
941 pub scan_count: u64,
943 pub ms_levels: Vec<u32>,
945 #[serde(skip_serializing_if = "Option::is_none")]
947 pub rt_range_s: Option<[f64; 2]>,
948 #[serde(skip_serializing_if = "Option::is_none")]
950 pub instrument: Option<InstrumentInfo>,
951 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
953 pub extra: BTreeMap<String, serde_json::Value>,
954}
955
956#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
958pub struct Spectrum {
959 pub index: u64,
961 pub scan_number: u64,
963 pub ms_level: u32,
965 #[serde(default)]
968 pub rt_s: Option<f64>,
969 pub polarity: String,
971 pub centroided: bool,
973 #[serde(skip_serializing_if = "Option::is_none")]
975 pub precursor_mz: Option<f64>,
976 #[serde(skip_serializing_if = "Option::is_none")]
978 pub precursor_charge: Option<i32>,
979 #[serde(skip_serializing_if = "Option::is_none")]
981 pub scan_filter: Option<String>,
982 #[serde(skip_serializing_if = "Option::is_none")]
984 pub total_ion_current: Option<f64>,
985 #[serde(default, skip_serializing_if = "Option::is_none")]
987 pub native_id: Option<String>,
988 #[serde(default, skip_serializing_if = "Option::is_none")]
990 pub base_peak_mz: Option<f64>,
991 #[serde(default, skip_serializing_if = "Option::is_none")]
993 pub base_peak_intensity: Option<f64>,
994 #[serde(default, skip_serializing_if = "Option::is_none")]
996 pub precursor_intensity: Option<f64>,
997 #[serde(default, skip_serializing_if = "Option::is_none")]
999 pub isolation_window_mz: Option<[f64; 2]>,
1000 #[serde(default, skip_serializing_if = "Option::is_none")]
1002 pub activation: Option<String>,
1003 #[serde(default, skip_serializing_if = "Option::is_none")]
1005 pub collision_energy: Option<f64>,
1006 #[serde(default, skip_serializing_if = "Option::is_none")]
1008 pub inverse_reduced_mobility: Option<f64>,
1009 #[serde(default, skip_serializing_if = "Option::is_none")]
1011 pub scan_window_mz: Option<[f64; 2]>,
1012 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1014 pub extra: BTreeMap<String, serde_json::Value>,
1015 pub mz: Vec<f64>,
1017 pub intensity: Vec<f32>,
1019}
1020
1021#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
1023pub struct SpectrumOutput {
1024 pub path: String,
1026 pub format: String,
1028 pub run: u32,
1030 pub view: String,
1032 pub point_count: u64,
1034 pub truncated: bool,
1037 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1039 pub peaks: Vec<crate::scans::MsPeak>,
1040 pub spectrum: Spectrum,
1042}
1043
1044#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
1048pub struct SignalChannelInfo {
1049 pub index: u32,
1051 pub name: String,
1053 #[serde(skip_serializing_if = "Option::is_none")]
1055 pub unit: Option<String>,
1056 pub dtype: String,
1058 pub scale: f64,
1060 pub offset: f64,
1062 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1064 pub extra: BTreeMap<String, serde_json::Value>,
1065}
1066
1067#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
1069pub struct TraceInfo {
1070 pub index: u32,
1072 #[serde(skip_serializing_if = "Option::is_none")]
1074 pub name: Option<String>,
1075 pub sample_rate_hz: f64,
1077 pub sample_count: u64,
1079 pub sweep_count: u32,
1081 pub channels: Vec<SignalChannelInfo>,
1083 #[serde(skip_serializing_if = "Option::is_none")]
1085 pub start_s: Option<f64>,
1086 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1088 pub extra: BTreeMap<String, serde_json::Value>,
1089}
1090
1091#[derive(Debug, Clone, Default)]
1093pub struct Trace {
1094 pub trace: u32,
1096 pub sweep: u32,
1098 pub first_sample: u64,
1100 pub channels: Vec<Vec<f64>>,
1102}
1103
1104#[cfg(test)]
1105mod tests {
1106 use super::*;
1107
1108 fn listing(tiles: usize) -> Listing {
1109 let entry = |kind: &str, i: usize| LsEntry {
1110 kind: kind.into(),
1111 name: format!("{kind} {i}"),
1112 offset: Some(i as u64),
1113 size: None,
1114 image: None,
1115 details: serde_json::Value::Null,
1116 };
1117 let mut entries = vec![entry("header", 0)];
1118 entries.extend((0..tiles).map(|i| entry("tile", i)));
1119 entries.push(entry("attachment", 0));
1120 Listing {
1121 entries,
1122 ..Listing::default()
1123 }
1124 }
1125
1126 #[test]
1127 fn structure_listings_are_capped_per_kind() {
1128 let mut l = listing(3);
1130 l.cap(50, None, 0);
1131 assert_eq!(l.entries.len(), 5);
1132 assert!(l.entries_total.is_empty() && l.notes.is_empty());
1133 let mut l = listing(1000);
1135 l.cap(50, None, 0);
1136 assert_eq!(l.entries.len(), 52);
1137 assert_eq!(l.entries.last().unwrap().kind, "attachment");
1138 assert_eq!(l.entries_total["tile"], 1000);
1139 assert!(l.notes[0].contains("tile 50 of 1000"), "{:?}", l.notes);
1140 let mut l = listing(1000);
1142 l.cap(10, Some("tile"), 995);
1143 let names: Vec<&str> = l.entries.iter().map(|e| e.name.as_str()).collect();
1144 assert_eq!(
1145 names,
1146 ["tile 995", "tile 996", "tile 997", "tile 998", "tile 999"]
1147 );
1148 let mut l = listing(1000);
1150 l.cap(0, None, 0);
1151 assert_eq!(l.entries.len(), 1002);
1152 }
1153
1154 #[test]
1155 #[allow(clippy::float_cmp)]
1156 fn finish_removes_conversion_noise() {
1157 let mut im = ImageInfo::new(0, 1, 1, PixelType::Uint8);
1158 im.physical_size = PhysicalSize::micrometres(Some(1e-7 * 1e6), Some(0.1 + 0.2), None);
1159 im.objective = Some(ObjectiveInfo {
1160 lens_na: Some(0.1 * 3.0),
1161 ..ObjectiveInfo::default()
1162 });
1163 let im = im.finish();
1164 assert_eq!(im.physical_size.x, Some(0.1));
1165 assert_eq!(im.physical_size.y, Some(0.3));
1166 assert_eq!(im.objective.unwrap().lens_na, Some(0.3));
1167 assert_eq!(round_noise(0.123_456_789_012_345_67), 0.123_456_789_012_346);
1168 assert!(round_noise(f64::NAN).is_nan());
1169 }
1170
1171 #[test]
1172 fn finish_normalizes_modes_immersion_and_padding() {
1173 let mut im = ImageInfo::new(0, 1, 1, PixelType::Uint8);
1174 im.channels = vec![ChannelInfo {
1175 acquisition_mode: Some("LaserScanningConfocalMicroscopy".into()),
1176 ..ChannelInfo::default()
1177 }];
1178 im.objective = Some(ObjectiveInfo {
1179 model: Some("HC PL APO 10x/0.40 DRY ".into()),
1180 immersion: Some("DRY".into()),
1181 ..ObjectiveInfo::default()
1182 });
1183 im.instrument = Some(InstrumentInfo {
1184 software: Some(" LAS X".into()),
1185 detector: Some(" ".into()),
1186 ..InstrumentInfo::default()
1187 });
1188 let im = im.finish();
1189 assert_eq!(
1190 im.channels[0].acquisition_mode.as_deref(),
1191 Some("Laser Scanning Confocal")
1192 );
1193 let o = im.objective.unwrap();
1194 assert_eq!(o.model.as_deref(), Some("HC PL APO 10x/0.40 DRY"));
1195 assert_eq!(o.immersion.as_deref(), Some("Air"));
1196 let i = im.instrument.unwrap();
1197 assert_eq!(i.software.as_deref(), Some("LAS X"));
1198 assert_eq!(i.detector, None);
1199 assert_eq!(immersion_label("water").as_deref(), Some("Water"));
1200 assert_eq!(immersion_label("Silicone").as_deref(), Some("Silicone"));
1201 }
1202
1203 #[test]
1204 fn physical_size_drops_uncalibrated_axes() {
1205 let p = PhysicalSize::micrometres(Some(0.5), Some(0.0), Some(f64::NAN));
1206 assert_eq!(p.x, Some(0.5));
1207 assert_eq!(p.y, None);
1208 assert_eq!(p.z, None);
1209 assert!(
1210 PhysicalSize::micrometres(None, Some(-1.0), None)
1211 .y
1212 .is_none()
1213 );
1214 }
1215}