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
use crate::Complex64;
use crate::execution::{ExecutionContext, ExecutionStage, ProgressTotal};
use crate::internal::numeric::scaled_l2;
use crate::processed::{ComponentBasis, ProcessedDataset};
use crate::processing::contracts::polarity::PolarityState;
use crate::processing::contracts::{
    error::*, history::*, operation::*, options::*, prepared_step::*, profile::*, state::*,
};
use crate::processing::engine::execution::*;
use crate::processing::kernels::acme::PhaseOptimizationError;
use crate::processing::kernels::buffer::*;
use crate::processing::kernels::tensor::*;
use crate::processing::prepare::resources::*;
use crate::provenance::CanonicalDigest;

/// A bounded automatic-phase analysis borrowing its immutable aggregate input.
///
/// Resource bounds cover selection, optimization, correction or the requested
/// fallback, and new history/context. Preparation does not inspect samples or
/// assert phase quality. Execution consumes this object and checks the supplied
/// work ledger before selecting a trace or copying history.
#[derive(Debug)]
pub struct PreparedAutoPhase<'a> {
    pub(in crate::processing) input: &'a crate::Dataset,
    pub(in crate::processing) profile: NormalizedAcmeV1,
    pub(in crate::processing) axis: usize,
    pub(in crate::processing) polarity: PolarityState,
    pub(in crate::processing) failure_policy: PhaseFailurePolicy,
    pub(in crate::processing) budget: AutoPhaseBudget,
}

impl PreparedAutoPhase<'_> {
    /// Returns the conservative capacity bounds, excluding borrowed input storage.
    pub fn resources(&self) -> crate::resource::ResourceEstimate {
        self.budget.resources
    }

    /// Returns the maximum selection, optimizer and correction work for this call.
    pub fn estimated_work(&self) -> u128 {
        self.budget.work
    }

    /// Returns the immutable input identity accepted by this prepared analysis.
    pub fn input_digest(&self) -> CanonicalDigest {
        self.input.canonical_digests().dataset()
    }

    /// Returns the selected current descriptor position.
    pub fn target(&self) -> OperationTarget {
        OperationTarget::Axis(self.axis)
    }

    /// Runs bounded analysis and records the actual correction or allowed failure.
    /// New unverified-quality failures always remain errors, even under fallback.
    pub fn execute(self) -> Result<crate::Dataset, ProcessingError> {
        self.execute_with_context(&mut ExecutionContext::default())
    }
    /// Executes automatic phasing with shared execution control.
    pub fn execute_with_context(
        self,
        control: &mut ExecutionContext<'_>,
    ) -> Result<crate::Dataset, ProcessingError> {
        let target = OperationTarget::Axis(self.axis);
        let result = execute_normalized_acme(
            self.profile,
            self.input.as_processed().expect("checked processed input"),
            self.axis,
            self.polarity,
            self.failure_policy,
            control,
            self.budget,
        )
        .map_err(|e| e.located(Some(0), Some(target), ProcessingPhase::Execution))?;
        Ok(self.input.derived_processed(result))
    }
}

/// Named automatic-phase request; optimization occurs only during execution.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AutoPhaseRequest {
    /// Current Cartesian frequency axis to phase.
    pub axis: crate::AxisIndex,
    /// Caller-established peak polarity.
    pub polarity: PolarityState,
    /// Explicit behavior on a recoverable phase failure.
    pub failure_policy: PhaseFailurePolicy,
}

impl NormalizedAcmeV1 {
    /// Previews bounded automatic phasing without inspecting samples or spending work.
    pub fn preflight<'a>(
        self,
        input: &'a crate::Dataset,
        request: AutoPhaseRequest,
        options: ProcessingOptions,
    ) -> Result<PreparedAutoPhase<'a>, ProcessingError> {
        let AutoPhaseRequest {
            axis,
            polarity,
            failure_policy,
        } = request;
        let axis = axis.index();
        let dataset = input.as_processed().ok_or(ProcessingError::Mapping(
            "automatic phasing requires processed input",
        ))?;
        let options = reserve_context(input.metadata(), options)?;
        Ok(PreparedAutoPhase {
            input,
            profile: self,
            axis,
            polarity,
            failure_policy,
            budget: auto_phase_budget(dataset, axis, options)?,
        })
    }
    /// Applies automatic phasing while preserving complete aggregate context.
    pub fn apply(
        self,
        input: &crate::Dataset,
        request: AutoPhaseRequest,
    ) -> Result<crate::Dataset, ProcessingError> {
        self.apply_with_context(
            input,
            request,
            ProcessingOptions::default(),
            &mut ExecutionContext::default(),
        )
    }
    /// Applies automatic phasing with cancellation, progress and a shared work budget.
    pub fn apply_with_context(
        self,
        input: &crate::Dataset,
        request: AutoPhaseRequest,
        options: ProcessingOptions,
        control: &mut ExecutionContext<'_>,
    ) -> Result<crate::Dataset, ProcessingError> {
        control.begin(ExecutionStage::Preflight, Some(0), None)?;
        self.preflight(input, request, options)
            .map_err(|error| {
                error.located(
                    Some(0),
                    Some(OperationTarget::Axis(request.axis.index())),
                    ProcessingPhase::Preflight,
                )
            })?
            .execute_with_context(control)
    }
    /// Applies automatic phasing to a low-level processed value.
    pub fn apply_processed(
        self,
        input: &ProcessedDataset,
        request: AutoPhaseRequest,
    ) -> Result<ProcessedDataset, ProcessingError> {
        self.apply_processed_with_context(
            input,
            request,
            ProcessingOptions::default(),
            &mut ExecutionContext::default(),
        )
    }
    /// Applies automatic phasing to a low-level value under shared execution control.
    pub fn apply_processed_with_context(
        self,
        input: &ProcessedDataset,
        request: AutoPhaseRequest,
        options: ProcessingOptions,
        control: &mut ExecutionContext<'_>,
    ) -> Result<ProcessedDataset, ProcessingError> {
        apply_normalized_acme(
            self,
            input,
            request.axis.index(),
            request.polarity,
            request.failure_policy,
            control,
            options,
        )
    }
}

#[derive(Clone, Copy, Debug)]
pub(in crate::processing) struct AutoPhaseBudget {
    pub(in crate::processing) options: ProcessingOptions,
    pub(in crate::processing) resources: crate::resource::ResourceEstimate,
    pub(in crate::processing) work: u128,
}

pub(in crate::processing) fn auto_phase_budget(
    dataset: &ProcessedDataset,
    axis: usize,
    options: ProcessingOptions,
) -> Result<AutoPhaseBudget, ProcessingError> {
    // Immutable aggregate constructors already validated samples and history.
    // All checks here read existing state and sizes without cloning either.
    validate_processing_axes(dataset)?;
    validate_auto_phase_axis(dataset.processing_state(), axis)?;
    let descriptor = dataset.descriptor();
    let rank = descriptor.axes().len();
    let points = descriptor.axes()[axis].points();
    let requests = [
        ProcessingOperation::PhaseCorrection {
            axis,
            correction: PhaseCorrection::new(0.0, 0.0, 0.0)?,
        },
        ProcessingOperation::Projection {
            projection: Projection::UnphasedReal,
            polarity: PolarityState::Ambiguous180,
        },
    ];
    let options = reserve_prepared(rank, requests.len(), true, options)?;
    let options = reserve_axes(reserved_processed_axis_bytes(dataset, &requests)?, options)?;
    let options = reserve_states(
        rank,
        dataset
            .processing_state()
            .observation_ordinals
            .as_ref()
            .map_or(0, |map| map.len()),
        options,
    )?;
    let options = reserve_containers(
        checked_sum(&[
            crate::processing::prepare::memory::processed_apply(dataset, requests.len())?,
            std::mem::size_of::<ProcessingDiagnostic>(),
        ])?,
        options,
    )?;
    let output = descriptor_bytes(descriptor)?;
    check_resource(
        crate::resource::ResourceKind::OutputBytes,
        output,
        options.max_output_bytes,
    )?;
    let trace = checked_times(points, std::mem::size_of::<Complex64>())?;
    let other_components = descriptor
        .axes()
        .iter()
        .enumerate()
        .filter(|(index, _)| *index != axis)
        .try_fold(1usize, |count, (_, axis)| {
            count.checked_mul(axis.component_count())
        })
        .ok_or(ProcessingError::SizeOverflow)?;
    // Selection retains its best trace alongside a candidate and all fields.
    let selection_peak = checked_sum(&[
        checked_times(trace, 2)?,
        checked_times(trace, other_components)?,
        checked_times(rank, 10 * std::mem::size_of::<usize>())?,
    ])?;
    let optimizer_peak = checked_sum(&[
        trace,
        crate::processing::kernels::acme::working_bytes(points)?,
    ])?;
    let correction_peak = checked_sum(&[checked_times(output, 2)?, trace])?;
    let numerical = selection_peak.max(optimizer_peak).max(correction_peak);
    check_sample_peak(numerical, 0, options)?;
    let correction_work = dataset.data().samples().len() as u128;
    let selection_work = correction_work
        .checked_mul(2)
        .ok_or(ProcessingError::SizeOverflow)?;
    let work = selection_work
        .checked_add(crate::processing::kernels::acme::estimated_work(points)?)
        .and_then(|work| work.checked_add(correction_work))
        .ok_or(ProcessingError::SizeOverflow)?;
    let metadata = metadata_reservation(options)?;
    Ok(AutoPhaseBudget {
        options,
        resources: crate::resource::ResourceEstimate::new(
            output,
            metadata,
            checked_sum(&[metadata, numerical])?,
        ),
        work,
    })
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn apply_normalized_acme(
    profile: NormalizedAcmeV1,
    dataset: &ProcessedDataset,
    axis: usize,
    polarity: PolarityState,
    failure_policy: PhaseFailurePolicy,
    control: &mut ExecutionContext<'_>,
    options: ProcessingOptions,
) -> Result<ProcessedDataset, ProcessingError> {
    let budget = auto_phase_budget(dataset, axis, options)?;
    execute_normalized_acme(
        profile,
        dataset,
        axis,
        polarity,
        failure_policy,
        control,
        budget,
    )
}

#[allow(clippy::too_many_arguments)]
pub(in crate::processing) fn execute_normalized_acme(
    profile: NormalizedAcmeV1,
    dataset: &ProcessedDataset,
    axis: usize,
    polarity: PolarityState,
    failure_policy: PhaseFailurePolicy,
    control: &mut ExecutionContext<'_>,
    budget: AutoPhaseBudget,
) -> Result<ProcessedDataset, ProcessingError> {
    // An insufficient ledger must fail before copying history, selecting traces,
    // or performing the first optimizer allocation. Reservation is conservative;
    // the original ledger is charged only for work actually entered below.
    control.ensure_work(budget.work)?;
    control.observe_payload(budget.resources.working_bytes());
    control.begin(
        ExecutionStage::AutoPhase,
        Some(0),
        Some(ProgressTotal::UpperBound(budget.work)),
    )?;
    let options = budget.options;
    let HistoryState {
        initial_descriptor: base,
        input,
        mut records,
        mut state,
    } = history_state(dataset)?;
    records
        .try_reserve_exact(2)
        .map_err(|_| ProcessingError::AllocationFailure)?;
    validate_auto_phase_axis(&state, axis)?;
    let trace = representative_trace_with_context(dataset, axis, control)?;
    let request = ProcessingRequest::AutoPhase {
        axis,
        profile,
        polarity,
        failure_policy,
    };
    let solution = profile.optimize_with_context(&trace, polarity, control);
    drop(trace);
    match solution {
        Ok(solution) => {
            let before = state.descriptor()?;
            transition_auto_phase(&mut state, axis)?;
            let after = state.descriptor()?;
            let resolved = ResolvedOperation::PhaseCorrection(solution.correction());
            check_working_limit(&before, &after, Some(axis), &resolved, options)?;
            check_resource(
                crate::resource::ResourceKind::OutputBytes,
                descriptor_bytes(&after)?,
                options.max_output_bytes,
            )?;
            let step = PreparedStep {
                step_index: 0,
                axis: Some(axis),
                before,
                after: after.clone(),
                resolved: resolved.clone(),
            };

            let samples = execute_step(control, dataset.data().samples(), &step)?;
            records.push(ProcessingRecord::applied(
                request,
                resolved,
                step.before.clone(),
                step.after.clone(),
                Vec::new(),
            ));
            derived_dataset(control, dataset, base, input, records, after, samples)
        }
        Err(error) => {
            let Some(failure) = recoverable_phase_failure(error) else {
                return Err(error.into());
            };
            if failure_policy == PhaseFailurePolicy::Fail {
                return Err(error.into());
            }
            state.record_phase_failure(axis)?;
            records.push(ProcessingRecord::attempted(request, failure, Vec::new()));
            let projection_request = ProcessingOperation::Projection {
                projection: Projection::UnphasedReal,
                polarity,
            };
            let before = state.descriptor()?;
            let resolved = transition_projection(&mut state, Projection::UnphasedReal, polarity)?;
            let after = state.descriptor()?;
            check_working_limit(&before, &after, None, &resolved, options)?;
            check_resource(
                crate::resource::ResourceKind::OutputBytes,
                descriptor_bytes(&after)?,
                options.max_output_bytes,
            )?;
            let step = PreparedStep {
                step_index: 0,
                axis: None,
                before,
                after: after.clone(),
                resolved: resolved.clone(),
            };

            let samples = execute_step(control, dataset.data().samples(), &step)?;
            records.push(ProcessingRecord::applied(
                projection_request,
                resolved,
                step.before.clone(),
                step.after.clone(),
                vec![ProcessingDiagnostic::ContinuedAfterPhaseFailure],
            ));
            derived_dataset(control, dataset, base, input, records, after, samples)
        }
    }
}

pub(in crate::processing) fn recoverable_phase_failure(
    error: PhaseOptimizationError,
) -> Option<AttemptFailure> {
    match error {
        PhaseOptimizationError::NoUsableSignal => Some(AttemptFailure::NoUsableSignal),
        PhaseOptimizationError::ObjectiveUndefined => Some(AttemptFailure::ObjectiveUndefined),
        PhaseOptimizationError::OptimizationDidNotConverge => {
            Some(AttemptFailure::OptimizationDidNotConverge)
        }
        PhaseOptimizationError::NumericalInvariantViolation
        | PhaseOptimizationError::QualityUnverified
        | PhaseOptimizationError::WorkLimit
        | PhaseOptimizationError::Execution(_)
        | PhaseOptimizationError::AllocationFailure => None,
    }
}

pub(in crate::processing) fn representative_trace_with_context(
    dataset: &ProcessedDataset,
    axis: usize,
    control: &mut ExecutionContext<'_>,
) -> Result<Vec<Complex64>, ProcessingError> {
    #[cfg(test)]
    REPRESENTATIVE_SELECTIONS.with(|count| count.set(count.get() + 1));
    let descriptor = dataset.descriptor();
    let selected = descriptor
        .axes()
        .get(axis)
        .ok_or(ProcessingError::InvalidState {
            axis,
            operation: "representative selection",
            reason: "axis index is out of bounds",
        })?;
    if !matches!(selected.component_basis(), ComponentBasis::Cartesian) {
        return Err(ProcessingError::InvalidState {
            axis,
            operation: "representative selection",
            reason: "requires one Cartesian component pair",
        });
    }
    let shape = descriptor.logical_shape();
    let component_counts = descriptor.component_counts();
    let line_count = other_line_count(&shape, axis)?;
    let other_component_count = component_counts
        .iter()
        .enumerate()
        .filter(|(index, _)| *index != axis)
        .try_fold(1usize, |total, (_, count)| total.checked_mul(*count))
        .ok_or(ProcessingError::SizeOverflow)?;
    let mut best: Option<(f64, f64, Vec<Complex64>)> = None;
    for line in 0..line_count {
        control.check_cancelled()?;
        let other_logical = other_coordinate(&shape, axis, line)?;
        let mut canonical_components = try_zeroed_usize(shape.len())?;
        let mut trace = try_vec_capacity(selected.points())?;
        for point in 0..selected.points() {
            charge_block(control, point, selected.points())?;
            let logical = merge_coordinate(shape.len(), axis, &other_logical, point)?;
            canonical_components[axis] = 0;
            let real = tensor_sample(
                dataset.data().samples(),
                descriptor,
                &logical,
                &canonical_components,
            )?;
            canonical_components[axis] = 1;
            let imaginary = tensor_sample(
                dataset.data().samples(),
                descriptor,
                &logical,
                &canonical_components,
            )?;
            trace.push(Complex64::new(real, imaginary));
        }
        let primary = scaled_l2(trace.iter().flat_map(|value| [value.re, value.im]));
        let mut all_fields = Vec::new();
        all_fields
            .try_reserve_exact(
                selected
                    .points()
                    .checked_mul(2)
                    .and_then(|value| value.checked_mul(other_component_count))
                    .ok_or(ProcessingError::SizeOverflow)?,
            )
            .map_err(|_| ProcessingError::AllocationFailure)?;
        for other_component in 0..other_component_count {
            let compact_counts = component_counts
                .iter()
                .enumerate()
                .filter_map(|(index, count)| (index != axis).then_some(*count))
                .collect::<Vec<_>>();
            let compact = if compact_counts.is_empty() {
                Vec::new()
            } else {
                unflatten(&compact_counts, other_component)?
            };
            for point in 0..selected.points() {
                charge_block(control, point, selected.points())?;
                let logical = merge_coordinate(shape.len(), axis, &other_logical, point)?;
                for selected_component in 0..2 {
                    let components =
                        merge_coordinate(shape.len(), axis, &compact, selected_component)?;
                    all_fields.push(tensor_sample(
                        dataset.data().samples(),
                        descriptor,
                        &logical,
                        &components,
                    )?);
                }
            }
        }
        let total = scaled_l2(all_fields);
        let replace = best.as_ref().is_none_or(|(best_primary, best_total, _)| {
            primary.total_cmp(best_primary) == std::cmp::Ordering::Greater
                || primary.total_cmp(best_primary) == std::cmp::Ordering::Equal
                    && total.total_cmp(best_total) == std::cmp::Ordering::Greater
        });
        if replace {
            best = Some((primary, total, trace));
        }
    }
    best.map(|(_, _, trace)| trace)
        .ok_or(ProcessingError::Mapping("no representative trace"))
}