nmr 0.1.3

NMR data tools for Rust
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
use crate::Complex64;
use crate::axis::AxisRole;
use crate::processed::{ProcessedAxis, ProcessedDataset, ProcessedDescriptor, ProcessedOrigin};
use crate::processing::contracts::{
    error::*, history::*, operation::*, options::*, prepared_step::*, profile::*,
};
use crate::processing::kernels::fft::fft_work;
use crate::processing::kernels::spectrum;
use crate::raw::RawDataset;

pub(in crate::processing) fn step_work(step: &PreparedStep) -> Result<u128, ProcessingError> {
    let count = (descriptor_bytes(&step.before)? / 8) as u128;
    let work = match &step.resolved {
        ResolvedOperation::Spectrum(operation) => spectrum::work(
            operation,
            step.before.axes()[step.axis()].points(),
            count as usize,
        )?,
        ResolvedOperation::FourierTransform(_)
        | ResolvedOperation::TimeDomainShiftFoldV1 { .. } => {
            let axis = &step.before.axes()[step.axis()];
            let traces = count / (axis.points() * axis.component_count()) as u128;
            traces
                * fft_work(axis.points())
                * if matches!(
                    step.resolved,
                    ResolvedOperation::TimeDomainShiftFoldV1 { .. }
                ) {
                    2
                } else {
                    1
                }
        }
        ResolvedOperation::BaselineCorrection(_) => count * PositivePeaksV1::MAX_SOLVES as u128,
        ResolvedOperation::ComponentTransform { .. } => count * 2,
        _ => count,
    };
    Ok(work)
}

pub(in crate::processing) fn descriptor_bytes(
    descriptor: &ProcessedDescriptor,
) -> Result<usize, ProcessingError> {
    descriptor
        .axes()
        .iter()
        .try_fold(std::mem::size_of::<f64>(), |bytes, axis| {
            bytes
                .checked_mul(axis.points())?
                .checked_mul(axis.component_count())
        })
        .ok_or(ProcessingError::SizeOverflow)
}

pub(in crate::processing) fn check_working_limit(
    before: &ProcessedDescriptor,
    after: &ProcessedDescriptor,
    axis: Option<usize>,
    resolved: &ResolvedOperation,
    options: ProcessingOptions,
) -> Result<(), ProcessingError> {
    if matches!(resolved, ResolvedOperation::BaselineCorrection(_)) {
        check_resource(
            crate::resource::ResourceKind::MetadataBytes,
            checked_sum(&[
                metadata_reservation(options)?,
                checked_times(
                    before.axes()[axis.expect("baseline has an axis")].points(),
                    std::mem::size_of::<f64>(),
                )?,
            ])?,
            options.max_metadata_bytes,
        )?;
    }
    check_sample_peak(
        numerical_working_bytes(before, after, axis, resolved)?,
        0,
        options,
    )
}

pub(in crate::processing) fn check_sample_peak(
    peak: usize,
    retained: usize,
    options: ProcessingOptions,
) -> Result<(), ProcessingError> {
    let metadata = metadata_reservation(options)?;
    if metadata > options.max_metadata_bytes {
        return Err(crate::resource::LimitExceeded {
            resource: crate::resource::ResourceKind::MetadataBytes,
            limit: options.max_metadata_bytes,
            required: metadata,
        }
        .into());
    }
    check_resource(
        crate::resource::ResourceKind::WorkingBytes,
        checked_sum(&[peak, retained, metadata])?,
        options.max_working_bytes,
    )
}

pub(in crate::processing) fn check_resource(
    resource: crate::resource::ResourceKind,
    required: usize,
    limit: usize,
) -> Result<(), ProcessingError> {
    if required > limit {
        return Err(crate::resource::LimitExceeded {
            resource,
            required,
            limit,
        }
        .into());
    }
    Ok(())
}

pub(in crate::processing) fn metadata_reservation(
    options: ProcessingOptions,
) -> Result<usize, ProcessingError> {
    checked_sum(&[
        options.prepared_bytes,
        options.axis_bytes,
        options.state_bytes,
        options.container_bytes,
        options.context_bytes,
    ])
}

pub(in crate::processing) fn estimate_resources(
    descriptor: &ProcessedDescriptor,
    steps: &[PreparedStep],
    options: ProcessingOptions,
) -> Result<crate::resource::ResourceEstimate, ProcessingError> {
    let output = descriptor_bytes(descriptor)?;
    let metadata = metadata_reservation(options)?;
    let mut numerical = output;
    let mut coordinate_bytes = 0;
    for step in steps {
        numerical = numerical.max(numerical_working_bytes(
            &step.before,
            &step.after,
            step.axis,
            &step.resolved,
        )?);
        if matches!(step.resolved, ResolvedOperation::BaselineCorrection(_)) {
            coordinate_bytes = coordinate_bytes.max(checked_times(
                step.before.axes()[step.axis()].points(),
                std::mem::size_of::<f64>(),
            )?);
        }
    }
    let metadata_with_coordinates = metadata
        .checked_add(coordinate_bytes)
        .ok_or(ProcessingError::SizeOverflow)?;
    if metadata_with_coordinates > options.max_metadata_bytes {
        return Err(crate::resource::LimitExceeded {
            resource: crate::resource::ResourceKind::MetadataBytes,
            limit: options.max_metadata_bytes,
            required: metadata_with_coordinates,
        }
        .into());
    }
    Ok(crate::resource::ResourceEstimate::new(
        output,
        metadata_with_coordinates,
        metadata
            .checked_add(numerical)
            .ok_or(ProcessingError::SizeOverflow)?,
    ))
}

// Scope: step vector, its two descriptor handle arrays per step, final
// descriptor handle array, and the requested-operation vector when present.
// ProcessedAxis clones share their backing storage. New backing storage and
// output/history allocations need separate accounting; this is not their bound.
pub(crate) fn prepared_retained_bytes(
    rank: usize,
    steps: usize,
    requested: bool,
) -> Result<usize, ProcessingError> {
    let descriptors = steps
        .checked_mul(2)
        .and_then(|n| n.checked_add(1))
        .and_then(|n| n.checked_mul(rank))
        .and_then(|n| n.checked_mul(std::mem::size_of::<ProcessedAxis>()))
        .ok_or(ProcessingError::SizeOverflow)?;
    let per_step = std::mem::size_of::<PreparedStep>()
        .checked_add(if requested {
            std::mem::size_of::<ProcessingOperation>()
        } else {
            // Segmented replay keeps the original IntoIter allocation while
            // collecting one segment's steps into its own vector.
            std::mem::size_of::<PreparedStep>()
        })
        .ok_or(ProcessingError::SizeOverflow)?;
    steps
        .checked_mul(per_step)
        .and_then(|bytes| bytes.checked_add(descriptors))
        .filter(|&bytes| bytes <= isize::MAX as usize)
        .ok_or(ProcessingError::SizeOverflow)
}

pub(in crate::processing) fn reserve_prepared(
    rank: usize,
    steps: usize,
    requested: bool,
    mut options: ProcessingOptions,
) -> Result<ProcessingOptions, ProcessingError> {
    if !(1..=3).contains(&rank) {
        return Err(ProcessingError::UnsupportedRank { rank });
    }
    options.prepared_bytes = prepared_retained_bytes(rank, steps, requested)?;
    check_sample_peak(0, 0, options)?;
    Ok(options)
}

#[derive(Clone, Copy, Default)]
pub(in crate::processing) struct AxisStorage {
    pub(in crate::processing) bytes: usize,
    pub(in crate::processing) points: usize,
    pub(in crate::processing) signal: bool,
}

pub(in crate::processing) fn raw_axis_storage(
    axes: &[crate::raw::RawAxis],
) -> Result<[AxisStorage; 3], ProcessingError> {
    let mut storage = [AxisStorage::default(); 3];
    if axes.len() > storage.len() {
        return Err(ProcessingError::UnsupportedRank { rank: axes.len() });
    }
    for (slot, axis) in storage.iter_mut().zip(axes) {
        *slot = AxisStorage {
            bytes: ProcessedAxis::backing_bytes(axis.label(), axis.nucleus(), axis.coordinates())
                .ok_or(ProcessingError::SizeOverflow)?,
            signal: axis.role().is_signal(),
            points: axis.points(),
        };
    }
    Ok(storage)
}

pub(in crate::processing) fn axis_storage<'a>(
    axes: impl Iterator<Item = &'a ProcessedAxis>,
) -> Result<[AxisStorage; 3], ProcessingError> {
    let mut storage = [AxisStorage::default(); 3];
    for (index, axis) in axes.enumerate() {
        let slot = storage
            .get_mut(index)
            .ok_or(ProcessingError::UnsupportedRank { rank: index + 1 })?;
        *slot = AxisStorage {
            bytes: ProcessedAxis::backing_bytes(axis.label(), axis.nucleus(), axis.coordinates())
                .ok_or(ProcessingError::SizeOverflow)?,
            signal: axis.role().is_signal(),
            points: axis.points(),
        };
    }
    Ok(storage)
}

pub(in crate::processing) fn axis_reservation<'a>(
    mut axes: [AxisStorage; 3],
    operations: impl Iterator<Item = &'a ProcessingOperation>,
    raw_baseline: bool,
) -> Result<usize, ProcessingError> {
    let mut bytes = if raw_baseline {
        axes[0]
            .bytes
            .checked_add(axes[1].bytes)
            .and_then(|bytes| bytes.checked_add(axes[2].bytes))
            .ok_or(ProcessingError::SizeOverflow)?
    } else {
        0
    };
    for operation in operations {
        if let Some(index) = operation.axis().filter(|i| *i < axes.len()) {
            let stored = &mut axes[index];
            match operation {
                ProcessingOperation::ZeroFill { zero_fill, .. } => {
                    stored.points = stored.points.max(zero_fill.target_points())
                }
                ProcessingOperation::StandardZeroFill { .. } => {
                    stored.points = stored
                        .points
                        .checked_mul(2)
                        .and_then(usize::checked_next_power_of_two)
                        .ok_or(ProcessingError::SizeOverflow)?
                }
                ProcessingOperation::Spectrum { .. } => {
                    // Includes coordinate materialization before transition allocation.
                    bytes = checked_sum(&[bytes, checked_times(stored.points, 8)?])?;
                }
                _ => {}
            }
        }
        let rebuild = match operation {
            ProcessingOperation::ZeroFill { .. }
            | ProcessingOperation::StandardZeroFill { .. }
            | ProcessingOperation::FourierTransform { .. }
            | ProcessingOperation::ComponentTransform { .. }
            | ProcessingOperation::ResolveFrequencyFrame { .. }
            | ProcessingOperation::ReverseAxis { .. }
            | ProcessingOperation::DigitalFilterCorrection {
                correction: DigitalFilterCorrection::TimeDomainShiftFoldV1 { .. },
                ..
            } => true,
            ProcessingOperation::Projection { .. } | ProcessingOperation::Spectrum { .. } => true,
            ProcessingOperation::Window { .. }
            | ProcessingOperation::PhaseCorrection { .. }
            | ProcessingOperation::BaselineCorrection { .. }
            | ProcessingOperation::DigitalFilterCorrection { .. } => false,
        };
        if !rebuild {
            continue;
        }
        for (index, axis) in axes.iter().enumerate() {
            let selected = if matches!(
                operation,
                ProcessingOperation::Spectrum {
                    operation: SpectrumOperation::Slice { .. },
                    ..
                }
            ) {
                // Shared-complex slicing can rebuild the surviving axis to
                // own a Cartesian pair, including its explicit coordinates.
                true
            } else if matches!(operation, ProcessingOperation::Projection { .. }) {
                axis.signal
            } else {
                Some(index) == operation.axis()
            };
            if selected {
                bytes = bytes
                    .checked_add(axis.bytes)
                    .ok_or(ProcessingError::SizeOverflow)?;
            }
        }
        if let ProcessingOperation::Spectrum { axis, operation } = operation {
            if operation.removes_axis() && *axis < 2 {
                if *axis == 0 {
                    axes[0] = axes[1];
                }
                axes[1] = AxisStorage::default();
            }
        }
    }
    Ok(bytes)
}

pub(crate) fn reserved_raw_axis_bytes(
    raw: &RawDataset,
    operations: &[ProcessingOperation],
) -> Result<usize, ProcessingError> {
    raw_plan_axis_bytes(raw.descriptor().axes(), operations)
}

pub(in crate::processing) fn raw_plan_axis_bytes(
    axes: &[crate::raw::RawAxis],
    operations: &[ProcessingOperation],
) -> Result<usize, ProcessingError> {
    let storage = raw_axis_storage(axes)?;
    checked_sum(&[
        checked_times(axis_reservation(storage, std::iter::empty(), true)?, 4)?,
        checked_times(axis_reservation(storage, operations.iter(), false)?, 2)?,
    ])
}

pub(in crate::processing) fn reserved_processed_axis_bytes(
    input: &ProcessedDataset,
    operations: &[ProcessingOperation],
) -> Result<usize, ProcessingError> {
    let new_axes = axis_reservation(
        axis_storage(input.descriptor().axes().iter())?,
        operations.iter(),
        false,
    )?;
    let old_axes = input
        .provenance()
        .history()
        .map_or(Ok(0), history_axis_bytes)?;
    let raw_baseline = if let ProcessedOrigin::DerivedRaw(origin) = input.provenance().origin() {
        axis_reservation(
            raw_axis_storage(origin.snapshot().descriptor().axes())?,
            std::iter::empty(),
            true,
        )?
    } else {
        0
    };
    checked_sum(&[checked_times(new_axes, 2)?, old_axes, raw_baseline])
}

pub(in crate::processing) fn history_axis_bytes(
    history: &ProcessingHistory,
) -> Result<usize, ProcessingError> {
    history.records().iter().try_fold(0usize, |bytes, record| {
        let extra = match record {
            ProcessingRecord::Applied {
                requested,
                input_descriptor,
                ..
            } => axis_reservation(
                axis_storage(input_descriptor.axes().iter())?,
                requested.explicit().into_iter(),
                false,
            )?,
            ProcessingRecord::Attempted { .. } => 0,
        };
        bytes
            .checked_add(extra)
            .ok_or(ProcessingError::SizeOverflow)
    })
}

pub(in crate::processing) fn checked_sum(values: &[usize]) -> Result<usize, ProcessingError> {
    values
        .iter()
        .try_fold(0usize, |sum, &value| sum.checked_add(value))
        .ok_or(ProcessingError::SizeOverflow)
}

pub(in crate::processing) fn checked_times(
    value: usize,
    count: usize,
) -> Result<usize, ProcessingError> {
    value
        .checked_mul(count)
        .ok_or(ProcessingError::SizeOverflow)
}

pub(crate) fn state_storage_bytes(
    rank: usize,
    map_points: usize,
) -> Result<usize, ProcessingError> {
    // Four independent ownership slots: history seed, resolving state, previous
    // segment output, and new output. Sum their bounds even when lifetimes differ.
    // Arc construction may temporarily coexist with its source Vec.
    let origin = checked_times(rank, std::mem::size_of::<usize>())?;
    let mapping = checked_times(map_points, std::mem::size_of::<usize>())?;
    if origin > isize::MAX as usize || mapping > isize::MAX as usize {
        return Err(ProcessingError::SizeOverflow);
    }
    let arc_header = 3 * std::mem::size_of::<usize>();
    let per_state = checked_sum(&[
        checked_times(
            rank,
            std::mem::size_of::<crate::processing::contracts::state::AxisState>(),
        )?,
        checked_times(origin, 2)?,
        arc_header,
        if map_points == 0 {
            0
        } else {
            checked_sum(&[checked_times(mapping, 2)?, arc_header])?
        },
    ])?;
    // Additional descriptor ownership slots and kernel/output index arrays.
    // Their audited inventory is recorded in ADR 0005; the counts bound actual
    // holders and do not scale with sample count or history length.
    checked_sum(&[
        checked_times(per_state, 4)?,
        checked_times(rank, 10 * std::mem::size_of::<ProcessedAxis>())?,
        checked_times(rank, 9 * std::mem::size_of::<usize>())?,
        checked_times(rank, std::mem::size_of::<crate::provenance::InputAxisRef>())?,
    ])
}

pub(in crate::processing) fn reserve_states(
    rank: usize,
    map_points: usize,
    mut options: ProcessingOptions,
) -> Result<ProcessingOptions, ProcessingError> {
    options.state_bytes = options
        .state_bytes
        .max(state_storage_bytes(rank, map_points)?);
    check_sample_peak(0, 0, options)?;
    Ok(options)
}

pub(in crate::processing) fn raw_map_points(raw: &RawDataset) -> usize {
    raw.sampling_schedule()
        .map_or(0, |_| raw.descriptor().axes()[0].points())
}

pub(in crate::processing) fn reserve_axes(
    bytes: usize,
    mut options: ProcessingOptions,
) -> Result<ProcessingOptions, ProcessingError> {
    options.axis_bytes = options.axis_bytes.max(bytes);
    check_sample_peak(0, 0, options)?;
    Ok(options)
}

pub(in crate::processing) fn reserve_containers(
    bytes: usize,
    mut options: ProcessingOptions,
) -> Result<ProcessingOptions, ProcessingError> {
    options.container_bytes = options.container_bytes.max(bytes);
    check_sample_peak(0, 0, options)?;
    Ok(options)
}

pub(crate) fn reserve_context(
    metadata: &crate::dataset::DatasetMetadata,
    mut options: ProcessingOptions,
) -> Result<ProcessingOptions, ProcessingError> {
    options.context_bytes = crate::processing::prepare::memory::aggregate(metadata)?;
    check_sample_peak(0, 0, options)?;
    Ok(options)
}

pub(in crate::processing) fn numerical_working_bytes(
    before: &ProcessedDescriptor,
    after: &ProcessedDescriptor,
    axis: Option<usize>,
    resolved: &ResolvedOperation,
) -> Result<usize, ProcessingError> {
    let current = descriptor_bytes(before)?;
    let next = descriptor_bytes(after)?;
    let points = axis.map(|axis| before.axes()[axis].points());
    let gather = match resolved {
        ResolvedOperation::FourierTransform(_)
        | ResolvedOperation::FrequencyDomainPhaseRampV1 { .. }
        | ResolvedOperation::TimeDomainShiftFoldV1 { .. }
        | ResolvedOperation::PhaseCorrection(_)
        | ResolvedOperation::AutomaticPhase { .. } => points
            .expect("single-axis kernel has a target")
            .checked_mul(std::mem::size_of::<Complex64>())
            .ok_or(ProcessingError::SizeOverflow)?,
        ResolvedOperation::ComponentTransform { transform, .. } => transform
            .input_lanes()
            .checked_mul(std::mem::size_of::<Complex64>())
            .ok_or(ProcessingError::SizeOverflow)?,
        ResolvedOperation::Projection {
            projection: Projection::Magnitude,
            ..
        } => before
            .axes()
            .iter()
            .try_fold(std::mem::size_of::<f64>(), |bytes, axis| {
                bytes.checked_mul(axis.component_count())
            })
            .ok_or(ProcessingError::SizeOverflow)?,
        _ => 0,
    };
    let scratch = match resolved {
        ResolvedOperation::Spectrum(_) => {
            spectrum::working_bytes(points.expect("spectrum operation has an axis"))?
        }
        ResolvedOperation::FourierTransform(_) => {
            crate::processing::kernels::fft::working_bytes(points.expect("FFT has an axis"), 1)?
        }
        ResolvedOperation::TimeDomainShiftFoldV1 { .. } => {
            crate::processing::kernels::fft::working_bytes(
                points.expect("shift/fold has an axis"),
                2,
            )?
        }
        ResolvedOperation::BaselineCorrection(_) => points
            .expect("single-axis kernel has a target")
            .checked_mul(18)
            .and_then(|values| values.checked_mul(std::mem::size_of::<f64>()))
            .ok_or(ProcessingError::SizeOverflow)?,
        _ => 0,
    };
    let tail = match resolved {
        ResolvedOperation::TimeDomainShiftFoldV1 { fold, .. } => fold
            .checked_mul(std::mem::size_of::<Complex64>())
            .ok_or(ProcessingError::SizeOverflow)?,
        _ => 0,
    };
    [current, next, gather, scratch, tail]
        .into_iter()
        .try_fold(0usize, |total, bytes| total.checked_add(bytes))
        .ok_or(ProcessingError::SizeOverflow)
}

pub(in crate::processing) fn validate_processing_axes(
    dataset: &ProcessedDataset,
) -> Result<(), ProcessingError> {
    let axes = dataset.descriptor().axes();
    let rank = axes.len();
    if !(1..=3).contains(&rank) {
        return Err(ProcessingError::UnsupportedRank { rank });
    }
    if let Some(axis) = axes[..rank - 1]
        .iter()
        .position(|axis| axis.role() == AxisRole::DirectAcquisition)
    {
        return Err(ProcessingError::MissingCapability {
            capability: "direct acquisition axis in fastest storage position",
            axis: Some(axis),
        });
    }
    Ok(())
}