medrs 0.1.2

Ultra-high-performance medical imaging I/O for deep learning
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
//! Image orientation and reorientation.
//!
//! Medical images can be stored in various orientations. This module provides
//! tools to detect orientation and reorient to standard coordinate systems.

use std::str::FromStr;

use crate::error::Error;
use crate::nifti::image::ArrayData;
use crate::nifti::{DataType, NiftiImage};
use ndarray::{ArrayD, IxDyn, ShapeBuilder};

/// Standard anatomical orientation codes.
///
/// Each axis is labeled by which direction increases along that axis:
/// - R/L: Right/Left
/// - A/P: Anterior/Posterior
/// - S/I: Superior/Inferior
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AxisCode {
    /// Increasing axis moves to the subject's right.
    R,
    /// Increasing axis moves to the subject's left.
    L, // Right, Left
    /// Increasing axis moves toward the anterior direction.
    A,
    /// Increasing axis moves toward the posterior direction.
    P, // Anterior, Posterior
    /// Increasing axis moves toward the superior direction.
    S,
    /// Increasing axis moves toward the inferior direction.
    I, // Superior, Inferior
}

impl AxisCode {
    fn axis_index(self) -> usize {
        match self {
            Self::R | Self::L => 0,
            Self::A | Self::P => 1,
            Self::S | Self::I => 2,
        }
    }
}

/// Orientation as a tuple of three axis codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Orientation(pub AxisCode, pub AxisCode, pub AxisCode);

impl Orientation {
    /// RAS (Right-Anterior-Superior) - standard neuroimaging orientation.
    pub const RAS: Self = Self(AxisCode::R, AxisCode::A, AxisCode::S);
    /// LAS (Left-Anterior-Superior) - radiological convention.
    pub const LAS: Self = Self(AxisCode::L, AxisCode::A, AxisCode::S);
    /// LPS (Left-Posterior-Superior) - DICOM convention.
    pub const LPS: Self = Self(AxisCode::L, AxisCode::P, AxisCode::S);

    /// Parse from 3-character string like "RAS", "LPS", etc.
    ///
    /// Returns `None` if the string is not a valid orientation code.
    /// Prefer using `FromStr::from_str` which returns a descriptive error.
    fn parse_orientation(s: &str) -> Option<Self> {
        let chars: Vec<char> = s.chars().collect();
        if chars.len() != 3 {
            return None;
        }

        let parse_code = |c: char| -> Option<AxisCode> {
            match c.to_ascii_uppercase() {
                'R' => Some(AxisCode::R),
                'L' => Some(AxisCode::L),
                'A' => Some(AxisCode::A),
                'P' => Some(AxisCode::P),
                'S' => Some(AxisCode::S),
                'I' => Some(AxisCode::I),
                _ => None,
            }
        };

        Some(Self(
            parse_code(chars[0])?,
            parse_code(chars[1])?,
            parse_code(chars[2])?,
        ))
    }
}

impl FromStr for Orientation {
    type Err = Error;

    /// Parse from 3-character string like "RAS", "LPS", etc.
    ///
    /// # Errors
    /// Returns `InvalidOrientation` if the string is not exactly 3 characters
    /// or contains invalid axis codes.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse_orientation(s).ok_or_else(|| {
            Error::InvalidOrientation(format!(
                "Invalid orientation code '{}'. Expected 3-letter code like 'RAS' or 'LPS' \
                 using R/L (right/left), A/P (anterior/posterior), S/I (superior/inferior).",
                s
            ))
        })
    }
}

impl Orientation {
    /// Get codes as array.
    pub fn codes(&self) -> [AxisCode; 3] {
        [self.0, self.1, self.2]
    }
}

impl std::fmt::Display for Orientation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let code_char = |c: AxisCode| -> char {
            match c {
                AxisCode::R => 'R',
                AxisCode::L => 'L',
                AxisCode::A => 'A',
                AxisCode::P => 'P',
                AxisCode::S => 'S',
                AxisCode::I => 'I',
            }
        };
        write!(
            f,
            "{}{}{}",
            code_char(self.0),
            code_char(self.1),
            code_char(self.2)
        )
    }
}

/// Detect the orientation of an image from its affine matrix.
///
/// Handles degenerate affines (zero columns) gracefully by using default axis codes.
pub fn orientation_from_affine(affine: &[[f32; 4]; 4]) -> Orientation {
    // Extract rotation/scaling part
    let mut codes = [AxisCode::R, AxisCode::A, AxisCode::S];

    for i in 0..3 {
        // Find which anatomical axis this image axis corresponds to
        let col = [affine[0][i], affine[1][i], affine[2][i]];
        let abs_col = [col[0].abs(), col[1].abs(), col[2].abs()];

        // Check for degenerate affine (zero or near-zero column)
        let col_magnitude = abs_col[0] + abs_col[1] + abs_col[2];
        if col_magnitude < f32::EPSILON {
            // Degenerate column - keep default axis code
            continue;
        }

        // Find dominant axis (use strict > to handle ties deterministically)
        let max_idx = if abs_col[0] > abs_col[1] && abs_col[0] > abs_col[2] {
            0
        } else if abs_col[1] > abs_col[2] {
            1
        } else {
            2
        };

        let positive = col[max_idx] > 0.0;

        codes[i] = match max_idx {
            0 => {
                if positive {
                    AxisCode::R
                } else {
                    AxisCode::L
                }
            }
            1 => {
                if positive {
                    AxisCode::A
                } else {
                    AxisCode::P
                }
            }
            2 => {
                if positive {
                    AxisCode::S
                } else {
                    AxisCode::I
                }
            }
            _ => unreachable!(),
        };
    }

    Orientation(codes[0], codes[1], codes[2])
}

/// Reorient an image to target orientation.
///
/// # Errors
/// Returns `Error::InvalidDimensions` if the image has fewer than 3 spatial dimensions.
/// Returns `Error::ShapeMismatch` if axis mapping fails (should not happen with valid NIfTI).
///
/// # Example
/// ```ignore
/// use medrs::transforms::{Orientation, reorient};
///
/// // Reorient to standard RAS orientation
/// let ras_img = reorient(&img, Orientation::RAS)?;
/// ```
#[allow(clippy::needless_range_loop)]
pub fn reorient(image: &NiftiImage, target: Orientation) -> Result<NiftiImage, Error> {
    let current = orientation_from_affine(&image.affine());

    if current == target {
        return Ok(image.clone());
    }

    let data = image.to_f32()?;
    let affine = image.affine();
    let shape = image.shape();

    // Validate shape has at least 3 dimensions
    if shape.len() < 3 {
        return Err(Error::InvalidDimensions(format!(
            "Image must have at least 3 dimensions, got {}",
            shape.len()
        )));
    }

    // Compute axis permutation and flips
    let current_codes = current.codes();
    let target_codes = target.codes();

    let mut perm = [0usize; 3];
    let mut flip = [false; 3];

    for (i, &target_code) in target_codes.iter().enumerate() {
        // Find which current axis maps to this target axis
        let mut found = false;
        for (j, &current_code) in current_codes.iter().enumerate() {
            if current_code.axis_index() == target_code.axis_index() {
                perm[i] = j;
                flip[i] = current_code != target_code;
                found = true;
                break;
            }
        }
        if !found {
            return Err(Error::ShapeMismatch(format!(
                "Could not map axis {} in orientation transformation",
                i
            )));
        }
    }

    // Apply permutation and flips
    let new_shape: Vec<usize> = perm.iter().map(|&p| shape[p]).collect();
    let mut new_data = permute_axes(&data, &perm, &new_shape)?;

    // Apply flips
    for (i, &should_flip) in flip.iter().enumerate() {
        if should_flip {
            new_data = flip_axis(&new_data, i)?;
        }
    }

    // Update affine matrix
    let mut new_affine = [[0.0f32; 4]; 4];
    new_affine[3][3] = 1.0;

    for i in 0..3 {
        let src_axis = perm[i];
        let flip_sign = if flip[i] { -1.0 } else { 1.0 };

        for j in 0..3 {
            new_affine[j][i] = affine[j][src_axis] * flip_sign;
        }

        // Adjust origin if flipping
        if flip[i] {
            let extent = (new_shape[i] - 1) as f32;
            for j in 0..3 {
                new_affine[j][3] += new_affine[j][i] * extent;
            }
        }
    }

    // Copy origin
    for j in 0..3 {
        new_affine[j][3] += affine[j][3];
    }

    // Build output header while preserving metadata
    let mut header = image.header().clone();
    header.ndim = new_shape.len() as u8;
    header.datatype = DataType::Float32;
    header.scl_slope = 1.0;
    header.scl_inter = 0.0;
    header.dim = [1u16; 7];
    for (i, &d) in new_shape.iter().enumerate() {
        header.dim[i] = d as u16;
    }
    header.pixdim = [1.0f32; 8];
    for i in 0..3 {
        header.pixdim[i + 1] = image.spacing()[perm[i]];
    }
    header.set_affine(new_affine);

    Ok(NiftiImage::from_parts(header, ArrayData::F32(new_data)))
}

#[allow(clippy::expect_used)]
fn permute_axes(
    data: &ArrayD<f32>,
    perm: &[usize; 3],
    new_shape: &[usize],
) -> Result<ArrayD<f32>, Error> {
    let old_shape = data.shape();

    // Convert F-order to C-order for processing if needed
    let data_c: std::borrow::Cow<'_, ArrayD<f32>> = if data.is_standard_layout() {
        std::borrow::Cow::Borrowed(data)
    } else {
        let mut c_order = ArrayD::zeros(IxDyn(old_shape));
        c_order.assign(data);
        std::borrow::Cow::Owned(c_order)
    };

    let src = data_c
        .as_slice()
        .expect("C-order array should have contiguous slice");
    let mut output = vec![0.0f32; new_shape.iter().product()];
    let (nd, nh, nw) = (new_shape[0], new_shape[1], new_shape[2]);
    let old_strides = [old_shape[1] * old_shape[2], old_shape[2], 1];

    for d in 0..nd {
        for h in 0..nh {
            for w in 0..nw {
                let new_coords = [d, h, w];
                // perm is a permutation of [0,1,2], so position() always succeeds
                let old_coords = [
                    new_coords[perm.iter().position(|&p| p == 0).expect("perm contains 0")],
                    new_coords[perm.iter().position(|&p| p == 1).expect("perm contains 1")],
                    new_coords[perm.iter().position(|&p| p == 2).expect("perm contains 2")],
                ];

                let old_idx = old_coords[0] * old_strides[0]
                    + old_coords[1] * old_strides[1]
                    + old_coords[2] * old_strides[2];
                let new_idx = d * nh * nw + h * nw + w;

                output[new_idx] = src[old_idx];
            }
        }
    }

    // Output is in C-order. Convert to F-order to match NIfTI convention.
    let c_order = ArrayD::from_shape_vec(IxDyn(new_shape), output)
        .map_err(|e| Error::MemoryAllocation(format!("Failed to create permuted array: {}", e)))?;
    let mut f_order = ArrayD::zeros(IxDyn(new_shape).f());
    f_order.assign(&c_order);
    Ok(f_order)
}

#[allow(clippy::expect_used)]
fn flip_axis(data: &ArrayD<f32>, axis: usize) -> Result<ArrayD<f32>, Error> {
    let shape = data.shape();

    // Convert F-order to C-order for processing if needed
    let data_c: std::borrow::Cow<'_, ArrayD<f32>> = if data.is_standard_layout() {
        std::borrow::Cow::Borrowed(data)
    } else {
        let mut c_order = ArrayD::zeros(IxDyn(shape));
        c_order.assign(data);
        std::borrow::Cow::Owned(c_order)
    };

    let src = data_c
        .as_slice()
        .expect("C-order array should have contiguous slice");
    let mut output = vec![0.0f32; src.len()];
    let (d, h, w) = (shape[0], shape[1], shape[2]);

    for di in 0..d {
        for hi in 0..h {
            for wi in 0..w {
                let (new_d, new_h, new_w) = match axis {
                    0 => (d - 1 - di, hi, wi),
                    1 => (di, h - 1 - hi, wi),
                    2 => (di, hi, w - 1 - wi),
                    _ => unreachable!(),
                };

                let old_idx = di * h * w + hi * w + wi;
                let new_idx = new_d * h * w + new_h * w + new_w;
                output[new_idx] = src[old_idx];
            }
        }
    }

    // Output is in C-order. Convert to F-order to match NIfTI convention.
    let c_order = ArrayD::from_shape_vec(IxDyn(shape), output)
        .map_err(|e| Error::MemoryAllocation(format!("Failed to create flipped array: {}", e)))?;
    let mut f_order = ArrayD::zeros(IxDyn(shape).f());
    f_order.assign(&c_order);
    Ok(f_order)
}

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

    #[test]
    fn test_orientation_parse() {
        // Test valid orientations via FromStr trait
        assert_eq!("RAS".parse::<Orientation>().unwrap(), Orientation::RAS);
        assert_eq!("LPS".parse::<Orientation>().unwrap(), Orientation::LPS);
        assert_eq!("ras".parse::<Orientation>().unwrap(), Orientation::RAS); // case insensitive

        // Test invalid orientations return descriptive errors
        let err = "XYZ".parse::<Orientation>().unwrap_err();
        assert!(format!("{}", err).contains("Invalid orientation"));

        let err = "RA".parse::<Orientation>().unwrap_err(); // too short
        assert!(format!("{}", err).contains("Invalid orientation"));
    }

    #[test]
    fn test_orientation_display() {
        assert_eq!(format!("{}", Orientation::RAS), "RAS");
        assert_eq!(format!("{}", Orientation::LPS), "LPS");
    }

    #[test]
    fn test_orientation_from_identity_affine() {
        // Identity affine should give RAS orientation
        let affine = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 1.0],
        ];
        assert_eq!(orientation_from_affine(&affine), Orientation::RAS);
    }

    #[test]
    fn test_orientation_from_lps_affine() {
        // LPS affine: negative x, negative y, positive z
        let affine = [
            [-1.0, 0.0, 0.0, 0.0],
            [0.0, -1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 1.0],
        ];
        assert_eq!(orientation_from_affine(&affine), Orientation::LPS);
    }

    #[test]
    fn test_orientation_from_degenerate_affine() {
        // Degenerate affine with zero columns should use default (RAS)
        let affine = [
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 1.0],
        ];
        // Should not panic, returns default RAS
        let orientation = orientation_from_affine(&affine);
        assert_eq!(orientation, Orientation::RAS);
    }

    #[test]
    fn test_orientation_from_partial_degenerate_affine() {
        // Affine with one zero column
        let affine = [
            [1.0, 0.0, 0.0, 0.0], // X axis valid (R)
            [0.0, 0.0, 0.0, 0.0], // Y axis zero - use default (A)
            [0.0, 0.0, 1.0, 0.0], // Z axis valid (S)
            [0.0, 0.0, 0.0, 1.0],
        ];
        let orientation = orientation_from_affine(&affine);
        // Should handle gracefully
        assert_eq!(orientation.0, AxisCode::R);
        assert_eq!(orientation.2, AxisCode::S);
    }

    #[test]
    fn test_reorient_result_type() {
        // Test that reorient returns Result type
        let affine = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 1.0],
        ];
        let img =
            NiftiImage::from_array::<f32>(ndarray::ArrayD::zeros(IxDyn(&[2, 2, 2]).f()), affine);

        // This should work without panicking
        let result = reorient(&img, Orientation::RAS);
        assert!(result.is_ok());

        // Test that identical orientation returns Ok with cloned image
        let same_result = reorient(&result.unwrap(), Orientation::RAS);
        assert!(same_result.is_ok());
    }
}