openbnct-core 0.1.1

Backend-neutral BNCT geometry and physical-dose contracts
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
// SPDX-License-Identifier: Apache-2.0

//! External photon/hadron dose import (`openbnct.external-dose/0.1.0`) and
//! dose-field resampling.
//!
//! This is the single-scalar counterpart of the component-dose interchange:
//! an external pipeline (treatment-planning export, MCNP/PHITS/Geant4 dose
//! tallies, a research code) contributes one absolute absorbed-dose field on
//! a declared grid plus the fractionation the dose was delivered in. The
//! importer validates it into an [`ExternalDoseBundle`] whose provenance
//! binds the document hash (`external-dose:<system>:sha256:<hash>`).
//!
//! Fractionation is declared, never guessed: `uniform` splits the total into
//! `count` equal per-fraction doses; `explicit` carries the full per-fraction
//! dose arrays. The biological layer (`openbnct-bio`) turns either into a
//! BED/EQD2 field; combining that field with a BNCT biological bundle is a
//! separate explicit step with its own compatibility gates.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{ExternalProducer, GridGeometry, ValidationError, grid_geometry_equivalent};

/// Current schema token for external dose documents.
pub const EXTERNAL_DOSE_SCHEMA: &str = "openbnct.external-dose/0.1.0";

/// What the imported dose field physically is. `physical` is absorbed dose;
/// `rbe_weighted` declares the producer already applied an RBE model — the
/// basis is carried through BED conversion so a weighted field can never be
/// silently read as physical dose downstream.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExternalDoseQuantity {
    Physical,
    RbeWeighted,
}

/// How the total dose divides into fractions.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum ExternalFractionation {
    /// `count` identical fractions; per-fraction dose is `values / count`.
    Uniform { count: u32 },
    /// Explicit per-fraction dose arrays, each voxel-aligned to `geometry`.
    Explicit { doses: Vec<Vec<f64>> },
}

/// A `openbnct.external-dose/0.1.0` document as emitted by the producer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExternalDoseDocument {
    #[serde(deserialize_with = "crate::deserialize_contract_id")]
    pub schema_version: String,
    pub case_id: String,
    /// DICOM frame-of-reference UID tying the grid to a patient space.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frame_of_reference_uid: Option<String>,
    pub geometry: GridGeometry,
    pub producer: ExternalProducer,
    /// What the dose physically is; `rbe_weighted` producers must say so.
    pub quantity: ExternalDoseQuantity,
    /// Absolute absorbed dose in gray over the whole course.
    pub values: Vec<f64>,
    /// Optional per-voxel 1-sigma absolute uncertainty, same unit.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub absolute_standard_uncertainty: Option<Vec<f64>>,
    /// The fractionation the course was delivered in.
    pub fractionation: ExternalFractionation,
}

/// The validated, provenance-bound form produced by [`import_external_dose`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExternalDoseBundle {
    #[serde(deserialize_with = "crate::deserialize_contract_id")]
    pub schema_version: String,
    pub case_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frame_of_reference_uid: Option<String>,
    pub geometry: GridGeometry,
    pub producer: ExternalProducer,
    pub quantity: ExternalDoseQuantity,
    pub values: Vec<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub absolute_standard_uncertainty: Option<Vec<f64>>,
    pub fractionation: ExternalFractionation,
    /// `external-dose:<system>:sha256:<document hash>`.
    pub provenance_id: String,
}

impl ExternalDoseBundle {
    /// Number of fractions declared by the producer.
    #[must_use]
    pub fn fraction_count(&self) -> usize {
        match &self.fractionation {
            ExternalFractionation::Uniform { count } => *count as usize,
            ExternalFractionation::Explicit { doses } => doses.len(),
        }
    }

    /// Per-fraction dose at `voxel`, expanding a uniform split.
    pub fn fraction_dose(&self, fraction: usize, voxel: usize) -> f64 {
        match &self.fractionation {
            ExternalFractionation::Uniform { count } => self.values[voxel] / f64::from(*count),
            ExternalFractionation::Explicit { doses } => doses[fraction][voxel],
        }
    }
}

#[derive(Debug, Error)]
pub enum ExternalDoseError {
    #[error("unsupported schema_version {0:?}")]
    UnsupportedSchema(String),
    #[error("field {0} must be nonempty")]
    EmptyField(&'static str),
    #[error("invalid geometry: {0}")]
    InvalidGeometry(#[from] ValidationError),
    #[error("dose values are not finite non-negative voxel values")]
    InvalidDoseValues,
    #[error("uncertainty values are not finite non-negative voxel values")]
    InvalidUncertainty,
    #[error("fractionation declares {0} doses; each must cover the grid")]
    InvalidFractionDoses(usize),
    #[error("fraction count must be at least one")]
    ZeroFractions,
}

/// Validate an external-dose document into a provenance-bound bundle.
///
/// `document_sha256` is the SHA-256 of the document's bytes — the caller
/// hashes the exact file so provenance binds content, not a name.
pub fn import_external_dose(
    document: &ExternalDoseDocument,
    document_sha256: &str,
) -> Result<ExternalDoseBundle, ExternalDoseError> {
    if !crate::schema_matches(&document.schema_version, EXTERNAL_DOSE_SCHEMA) {
        return Err(ExternalDoseError::UnsupportedSchema(
            document.schema_version.clone(),
        ));
    }
    if document.case_id.trim().is_empty() {
        return Err(ExternalDoseError::EmptyField("case_id"));
    }
    if document.producer.system.trim().is_empty() {
        return Err(ExternalDoseError::EmptyField("producer.system"));
    }
    if document.producer.version.trim().is_empty() {
        return Err(ExternalDoseError::EmptyField("producer.version"));
    }
    if document.producer.normalization.trim().is_empty() {
        return Err(ExternalDoseError::EmptyField("producer.normalization"));
    }
    if let Some(uid) = &document.frame_of_reference_uid
        && uid.trim().is_empty()
    {
        return Err(ExternalDoseError::EmptyField("frame_of_reference_uid"));
    }
    let voxel_count = document.geometry.voxel_count()?;
    if document.values.len() != voxel_count
        || document.values.iter().any(|v| !v.is_finite() || *v < 0.0)
    {
        return Err(ExternalDoseError::InvalidDoseValues);
    }
    if let Some(sigma) = &document.absolute_standard_uncertainty
        && (sigma.len() != voxel_count || sigma.iter().any(|v| !v.is_finite() || *v < 0.0))
    {
        return Err(ExternalDoseError::InvalidUncertainty);
    }
    match &document.fractionation {
        ExternalFractionation::Uniform { count } if *count == 0 => {
            return Err(ExternalDoseError::ZeroFractions);
        }
        ExternalFractionation::Explicit { doses } => {
            if doses.is_empty() {
                return Err(ExternalDoseError::ZeroFractions);
            }
            for dose in doses {
                if dose.len() != voxel_count || dose.iter().any(|v| !v.is_finite() || *v < 0.0) {
                    return Err(ExternalDoseError::InvalidFractionDoses(doses.len()));
                }
            }
            // Explicit fractions must sum to the declared total — otherwise
            // the two dose statements silently disagree.
            for voxel in 0..voxel_count {
                let sum: f64 = doses.iter().map(|d| d[voxel]).sum();
                let total = document.values[voxel];
                if (sum - total).abs() > 1e-6 * total.abs().max(1e-12) {
                    return Err(ExternalDoseError::InvalidFractionDoses(doses.len()));
                }
            }
        }
        ExternalFractionation::Uniform { .. } => {}
    }

    Ok(ExternalDoseBundle {
        schema_version: document.schema_version.clone(),
        case_id: document.case_id.clone(),
        frame_of_reference_uid: document.frame_of_reference_uid.clone(),
        geometry: document.geometry.clone(),
        producer: document.producer.clone(),
        quantity: document.quantity,
        values: document.values.clone(),
        absolute_standard_uncertainty: document.absolute_standard_uncertainty.clone(),
        fractionation: document.fractionation.clone(),
        provenance_id: format!(
            "external-dose:{}:sha256:{document_sha256}",
            document.producer.system
        ),
    })
}

/// Dose-field resampling method. Trilinear interpolation at voxel centers is
/// the only supported scheme; the choice is recorded in whatever record the
/// resampled field lands in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResampleMethod {
    Trilinear,
}

#[derive(Debug, Error, PartialEq)]
pub enum ResampleError {
    #[error("resampling requires axis-aligned (identity-direction) grids")]
    NonAxisAligned,
    #[error("source field has {0} values; grid holds {1} voxels")]
    LengthMismatch(usize, usize),
    #[error("target grid is not covered by the source field on axis {axis}")]
    UncoveredTarget { axis: usize },
    #[error("invalid geometry: {0}")]
    InvalidGeometry(#[from] ValidationError),
}

/// Resample a voxel field onto a target grid by trilinear interpolation at
/// voxel centers.
///
/// Strict co-registration: both grids must be axis-aligned and every target
/// voxel center must lie inside the source field's outer bounds (source
/// centers at the edge extrapolate flatly to the half-spacing face). A
/// target point outside the source extent is a hard error — never a silent
/// zero.
pub fn resample_trilinear(
    values: &[f64],
    source: &GridGeometry,
    target: &GridGeometry,
) -> Result<Vec<f64>, ResampleError> {
    let identity = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    if source.direction != identity || target.direction != identity {
        return Err(ResampleError::NonAxisAligned);
    }
    let src_nx = source.shape[0] as usize;
    let src_ny = source.shape[1] as usize;
    let src_nz = source.shape[2] as usize;
    let src_count = src_nx * src_ny * src_nz;
    if values.len() != src_count {
        return Err(ResampleError::LengthMismatch(values.len(), src_count));
    }
    let target_count = target.voxel_count()?;
    // Reuse the shared equivalent-grid check: an identical grid resamples to
    // the same values with no interpolation cost.
    if grid_geometry_equivalent(source, target) {
        return Ok(values.to_vec());
    }

    let mut out = Vec::with_capacity(target_count);
    let [tnx, tny, tnz] = target.shape.map(|d| d as usize);
    for k in 0..tnz {
        for j in 0..tny {
            for i in 0..tnx {
                let mut value = 0.0;
                let mut weights = [(0_usize, 0.0_f64); 8];
                for axis in 0..3 {
                    let p = match axis {
                        0 => target.origin_mm[0] + i as f64 * target.spacing_mm[0],
                        1 => target.origin_mm[1] + j as f64 * target.spacing_mm[1],
                        _ => target.origin_mm[2] + k as f64 * target.spacing_mm[2],
                    };
                    // Fractional source index of the target center; a target
                    // center may sit at most half a spacing outside the outer
                    // centers (flat extrapolation to the face), never beyond.
                    let f = (p - source.origin_mm[axis]) / source.spacing_mm[axis];
                    let n = source.shape[axis] as f64;
                    if !(-0.5..=n - 0.5).contains(&f) {
                        return Err(ResampleError::UncoveredTarget { axis });
                    }
                    let f = f.clamp(0.0, n - 1.0);
                    let lo = f.floor() as usize;
                    let hi = (lo + 1).min(source.shape[axis] as usize - 1);
                    weights[axis * 2] = (lo, 1.0 - (f - lo as f64));
                    weights[axis * 2 + 1] = (hi, f - lo as f64);
                }
                for (bi, wi) in [weights[0], weights[1]] {
                    for (bj, wj) in [weights[2], weights[3]] {
                        for (bk, wk) in [weights[4], weights[5]] {
                            value += values[bi + src_nx * bj + src_nx * src_ny * bk] * wi * wj * wk;
                        }
                    }
                }
                out.push(value);
            }
        }
    }
    Ok(out)
}

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

    fn grid() -> GridGeometry {
        GridGeometry {
            shape: [2, 1, 1],
            spacing_mm: [5.0, 5.0, 5.0],
            origin_mm: [0.0, 0.0, 0.0],
            direction: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
        }
    }

    fn producer() -> ExternalProducer {
        ExternalProducer {
            system: "course-sim".into(),
            version: "1".into(),
            normalization: "absolute gray".into(),
        }
    }

    fn document() -> ExternalDoseDocument {
        ExternalDoseDocument {
            schema_version: EXTERNAL_DOSE_SCHEMA.into(),
            case_id: "case".into(),
            frame_of_reference_uid: None,
            geometry: grid(),
            producer: producer(),
            quantity: ExternalDoseQuantity::Physical,
            values: vec![60.0, 60.0],
            absolute_standard_uncertainty: Some(vec![0.6, 0.6]),
            fractionation: ExternalFractionation::Uniform { count: 30 },
        }
    }

    #[test]
    fn valid_document_binds_provenance() {
        let bundle = import_external_dose(&document(), "ab12").unwrap();
        assert_eq!(bundle.provenance_id, "external-dose:course-sim:sha256:ab12");
        assert_eq!(bundle.fraction_count(), 30);
        assert!((bundle.fraction_dose(0, 0) - 2.0).abs() < 1e-12);
    }

    #[test]
    fn rejects_wrong_schema_and_bad_values() {
        let mut bad = document();
        bad.schema_version = "openbnct.external-dose/0.0.0".into();
        assert!(import_external_dose(&bad, "x").is_err());
        let mut bad = document();
        bad.values.push(1.0); // 3 values on a 2-voxel grid
        assert!(import_external_dose(&bad, "x").is_err());
        let mut bad = document();
        bad.values[0] = f64::NAN;
        assert!(import_external_dose(&bad, "x").is_err());
        let mut bad = document();
        bad.fractionation = ExternalFractionation::Uniform { count: 0 };
        assert!(import_external_dose(&bad, "x").is_err());
    }

    #[test]
    fn explicit_fractions_must_sum_to_total() {
        let mut doc = document();
        doc.fractionation = ExternalFractionation::Explicit {
            doses: vec![vec![30.0, 30.0], vec![30.0, 30.0]],
        };
        assert!(import_external_dose(&doc, "x").is_ok());
        doc.fractionation = ExternalFractionation::Explicit {
            doses: vec![vec![30.0, 30.0], vec![29.0, 30.0]],
        };
        assert!(import_external_dose(&doc, "x").is_err());
    }

    #[test]
    fn resample_identity_is_exact() {
        let values = vec![1.0, 2.0];
        assert_eq!(
            resample_trilinear(&values, &grid(), &grid()).unwrap(),
            values
        );
    }

    #[test]
    fn resample_trilinear_interpolates_centers() {
        // Source: 2 voxels at x = 0, 5 mm with values 0 and 10.
        // Target: 3 voxels at x = 0, 2.5, 5 mm → 0, 5, 10.
        let target = GridGeometry {
            shape: [3, 1, 1],
            spacing_mm: [2.5, 5.0, 5.0],
            ..grid()
        };
        let out = resample_trilinear(&[0.0, 10.0], &grid(), &target).unwrap();
        assert!((out[0] - 0.0).abs() < 1e-12);
        assert!((out[1] - 5.0).abs() < 1e-12);
        assert!((out[2] - 10.0).abs() < 1e-12);
    }

    #[test]
    fn resample_rejects_uncovered_target() {
        // Target centered beyond the source extent (x = 10 mm past the edge).
        let target = GridGeometry {
            shape: [1, 1, 1],
            origin_mm: [20.0, 0.0, 0.0],
            ..grid()
        };
        assert_eq!(
            resample_trilinear(&[1.0, 2.0], &grid(), &target),
            Err(ResampleError::UncoveredTarget { axis: 0 })
        );
    }

    #[test]
    fn resample_rejects_rotated_grids() {
        let mut rotated = grid();
        rotated.direction = [0.0, 1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
        assert_eq!(
            resample_trilinear(&[1.0, 2.0], &rotated, &grid()),
            Err(ResampleError::NonAxisAligned)
        );
    }
}