neuroformats 0.5.0

Handling of structural neuroimaging file formats. The focus is on surface-based brain morphometry data and FreeSurfer file formats.
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
//! Utility functions used in all other neuroformats modules.

use std::io::BufRead;
use std::path::Path;

use crate::error::{NeuroformatsError, Result};

use byteordered::byteorder::ReadBytesExt;


use colorgrad::Gradient;

/// Convert a slice of f32 values to a vector of RGB colors using the Viridis colormap.
///
/// This function takes a slice of f32 values and maps them to RGB colors using the Viridis colormap.
/// The values are normalized to the range [0, 1] based on the provided minimum and maximum values.
/// The resulting colors are returned as a vector of u8 values, where each color is represented by three consecutive u8 values (R, G, B).
/// # Arguments
/// * `values` - A slice of f32 values to be converted to colors.
/// * `min_val` - The minimum value for normalization. If the values argument contains values less than this, they will be clamped to this value.
/// * `max_val` - The maximum value for normalization. If the values argument contains values greater than this, they will be clamped to this value.
/// # Returns
/// * A vector of u8 values representing the RGB colors.
/// # Example
/// ```
/// use neuroformats::util::values_to_colors;
/// let values = vec![0.0, 0.5, 1.1];
/// let min_val = 0.0;
/// let max_val = 1.0;
/// let colors = values_to_colors(&values, min_val, max_val);
/// assert_eq!(colors, vec![68, 1, 84, 38, 130, 142, 254, 232, 37]);
/// ```
/// # Note
/// The input values should be in the range [min_val, max_val]. Values outside this range will be clamped.
/// The resulting colors are in the RGB format, where each color is represented by three consecutive u8 values (R, G, B).
/// The colors are generated using the Viridis colormap, which is perceptually uniform and colorblind-friendly.
pub fn values_to_colors(values: &[f32], min_val: f32, max_val: f32) -> Vec<u8> {
    // Create Viridis colormap
    let grad = colorgrad::preset::viridis();

    // Normalize values to [0, 1] range and map to colors
    let mut colors = Vec::with_capacity(values.len() * 3);

    for &value in values {
        // Normalize to [0, 1] range
        let t = (value - min_val) / (max_val - min_val);
        let t = t.clamp(0.0, 1.0); // Ensure within bounds

        // Get color from gradient
        let color = grad.at(t as f32);

        // Convert to RGB u8 and add to output
        colors.push((color.r * 255.0) as u8);
        colors.push((color.g * 255.0) as u8);
        colors.push((color.b * 255.0) as u8);
    }

    colors
}

/// Check whether the file extension ends with ".gz".
/// This is a simple check and does not guarantee that the file is actually gzipped.
/// # Example
/// ```
/// use std::path::Path;
/// use neuroformats::util::is_gz_file;
/// assert_eq!(is_gz_file("example.gz"), true);
/// assert_eq!(is_gz_file("example.txt"), false);
/// ```
/// # Arguments
/// * `path` - A path to the file to check.
/// # Returns
/// * `true` if the file name ends with ".gz", `false` otherwise.
/// # Note
/// This function does not check the actual content of the file.
pub fn is_gz_file<P>(path: P) -> bool
where
    P: AsRef<Path>,
{
    path.as_ref()
        .file_name()
        .map(|a| a.to_string_lossy().ends_with(".gz"))
        .unwrap_or(false)
}

/// Read a variable length Freesurfer-style byte string from the input.
///
/// A FreeSurfer-style variable length string is a string terminated by two `\x0A`, or 'Unix line feed' ASCII characters.
///
/// # Arguments
/// * `input` - A buffered reader positioned at the start of the string.
/// * `max_len` - Maximum number of characters to read before returning an error.
///
/// # Warnings
///
/// * Terrible things will happen if the input does not contain a sequence of two consecutive `\x0A` chars within `max_len` bytes.
/// * Returns [`NeuroformatsError::StringTooLong`] if the string exceeds `max_len` before finding the terminator.
pub fn read_fs_variable_length_string<S>(input: &mut S, max_len: usize) -> Result<String>
where
    S: BufRead,
{
    let mut last_char;
    let mut cur_char: char = '0';
    let mut info_line = String::new();
    loop {
        last_char = cur_char;
        cur_char = input.read_u8()? as char;
        info_line.push(cur_char);
        if info_line.len() > max_len {
            return Err(NeuroformatsError::StringTooLong);
        }
        if last_char == '\x0A' && cur_char == '\x0A' {
            break;
        }
    }
    Ok(info_line)
}

/// Read a fixed length NUL-terminated string.
///
/// Read a fixed length zero-terminated byte string of the given length from the input. The `len` value must include the trailing NUL byte position, if any. Embedded '\0' chars are allowed, and the trailing one (if any) is read but not added to the returned String (all others are).
pub fn read_fixed_length_string<S>(input: &mut S, len: usize) -> Result<String>
where
    S: BufRead,
{
    let mut info_line = String::with_capacity(len);
    for char_idx in 0..len {
        let cur_char = input.read_u8()? as char;
        if char_idx == (len - 1) {
            if cur_char != '\0' {
                info_line.push(cur_char);
            }
        } else {
            info_line.push(cur_char);
        }
    }
    Ok(info_line)
}

/// Determine the minimum and maximum value of an `f32` sequence.
///
/// # Panics
///
/// If the `data` input vector is empty or contains nan values.
///
/// # Return value
///
/// A tuple of length 2, the first value is the minimum, the second the maximum.
///
/// Example:
/// ```
/// use neuroformats::util::vec32minmax;
/// let v: Vec<f32> = vec![0.4, 0.5, 0.9, 0.01];
/// let (min, max) = vec32minmax(v.into_iter(), true);
/// assert_eq!(min, 0.01);
/// assert_eq!(max, 0.9);
/// ```
/// # Arguments
/// * `data` - An iterator over `f32` values.
/// * `remove_nan` - If set to true, NaN values will be filtered out. If set to false, the function will panic if NaN values are found.
/// # Note
/// The function will panic if the input iterator is empty or contains NaN values and `remove_nan` is set to false.
/// The function will also panic if the input iterator is empty.
/// The function will filter out NaN values if `remove_nan` is set to true.
/// The function will return a tuple containing the minimum and maximum values found in the input iterator.
pub fn vec32minmax<I>(data: I, remove_nan: bool) -> (f32, f32)
where
    I: Iterator<Item = f32>,
{
    // NOTE: the data variable is a iterator, it will be consumed by the for loop bellow
    let mut data = data.filter(|v| match (remove_nan, v.is_nan()) {
        // if is just a regular f32, just let is pass
        (_, false) => true,
        // remove_nan is set, if is a NaN, filter it out
        (true, true) => false,
        // remove_nan is not set, panic if is NaN
        (false, true) => panic!("NaN values not allowed in input."),
    });

    let first = data.next().expect("Input data must not be empty.");
    let mut min = first;
    let mut max = first;
    for value in data {
        if value < min {
            min = value;
        } else if value > max {
            max = value;
        }
    }
    (min, max)
}

/// Safely compute the product of i32 dimensions, checking for overflow and negative values.
///
/// Returns the total number of elements as `usize`, or an error if any dimension is negative
/// or if the multiplication overflows.
///
/// # Arguments
/// * `dims` - Slice of i32 dimension values from a file header.
///
/// # Errors
/// * `InvalidHeaderValue` if any dimension is negative.
/// * `IntegerOverflow` if the product exceeds `usize::MAX`.
///
/// # Examples
///
/// ```
/// use neuroformats::util::checked_mul_dims;
/// assert_eq!(checked_mul_dims(&[256, 256, 256, 1]).unwrap(), 16_777_216);
/// ```
pub fn checked_mul_dims(dims: &[i32]) -> Result<usize> {
    let mut total: usize = 1;
    for &dim in dims {
        if dim < 0 {
            return Err(NeuroformatsError::InvalidHeaderValue(format!(
                "Negative dimension value: {}",
                dim
            )));
        }
        total = total
            .checked_mul(dim as usize)
            .ok_or(NeuroformatsError::IntegerOverflow)?;
    }
    Ok(total)
}

/// Validate that all values in a float slice are finite (not NaN, not Inf).
///
/// # Arguments
/// * `values` - Slice of f32 values to validate.
/// * `field_name` - Human-readable field name for error messages.
///
/// # Errors
/// * `InvalidHeaderValue` if any value is NaN or infinite.
pub fn validate_finite_f32_slice(values: &[f32], field_name: &str) -> Result<()> {
    for (i, &v) in values.iter().enumerate() {
        if !v.is_finite() {
            return Err(NeuroformatsError::InvalidHeaderValue(format!(
                "Field '{}' at index {} is not finite (value: {})",
                field_name, i, v
            )));
        }
    }
    Ok(())
}

/// Validate that a slice of f32 vertex values contains only finite values.
///
/// # Arguments
/// * `values` - Slice of f32 values to validate.
/// * `label` - Human-readable label for error messages.
///
/// # Errors
/// * `InvalidVertexValue` if any value is NaN or infinite.
pub fn validate_finite_vertex_values(values: &[f32], label: &str) -> Result<()> {
    for (i, &v) in values.iter().enumerate() {
        if !v.is_finite() {
            return Err(NeuroformatsError::InvalidVertexValue(format!(
                "{} at index {} is not finite (value: {})",
                label, i, v
            )));
        }
    }
    Ok(())
}

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

    #[test]
    fn the_min_and_max_of_an_f32_vector_without_nan_values_can_be_computed() {
        let v: Vec<f32> = vec![0.4, 0.5, 0.9, 0.01];
        let (min, max) = vec32minmax(v.into_iter(), true);
        assert_abs_diff_eq!(min, 0.01, epsilon = 1e-8);
        assert_abs_diff_eq!(max, 0.9, epsilon = 1e-8);
    }

    #[test]
    fn the_min_and_max_of_an_f32_vector_with_nan_values_can_be_computed() {
        let v: Vec<f32> = vec![0.4, 0.5, 0.9, f32::NAN, 0.01];
        let (min, max) = vec32minmax(v.into_iter(), true);
        assert_abs_diff_eq!(min, 0.01, epsilon = 1e-8);
        assert_abs_diff_eq!(max, 0.9, epsilon = 1e-8);
    }

    #[test]
    fn a_variable_length_fs_string_can_be_read() {
        use std::io::{Cursor, Read, Seek, SeekFrom, Write};

        // Create our "file".
        let mut c = Cursor::new(Vec::<u8>::new());
        c.write(b"test\x0A\x0A").unwrap();
        c.write(&[166 as u8]).unwrap();

        // Seek to start
        c.seek(SeekFrom::Start(0)).unwrap();

        // Re-read the data.
        let s = read_fs_variable_length_string(&mut c, 1024).unwrap();
        let mut out = Vec::new();
        c.read_to_end(&mut out).unwrap();

        assert_eq!(s, "test\n\n");
        assert_eq!(out, &[166]);
        assert_eq!(7, c.position());
    }

    #[test]
    fn a_fixed_length_nul_terminated_string_can_be_read() {
        use std::io::{Cursor, Read, Seek, SeekFrom, Write};

        // Create our "file".
        let mut c = Cursor::new(Vec::<u8>::new());
        c.write(b"test\x0A\x0Atest\x00").unwrap();

        // Seek to start
        c.seek(SeekFrom::Start(0)).unwrap();

        // Re-read the data.
        let s = read_fixed_length_string(&mut c, 11 as usize).unwrap();
        let mut out: Vec<u8> = Vec::new();
        c.read_to_end(&mut out).unwrap();

        let empty: Vec<u8> = [].to_vec();
        assert_eq!(s, "test\n\ntest");
        assert_eq!(out, empty);
        assert_eq!(11, c.position());
    }

    #[test]
    fn a_fixed_length_without_termination_char_can_be_read() {
        use std::io::{Cursor, Read, Seek, SeekFrom, Write};

        // Create our "file".
        let mut c = Cursor::new(Vec::<u8>::new());
        c.write(b"test\x0A\x0Atestdonotreadthis").unwrap();

        // Seek to start
        c.seek(SeekFrom::Start(0)).unwrap();

        // Re-read the data.
        let s = read_fixed_length_string(&mut c, 10 as usize).unwrap();

        assert_eq!(s, "test\n\ntest");
        assert_eq!(10, c.position());

        let mut out: Vec<u8> = Vec::new();
        c.read_to_end(&mut out).unwrap();
        assert_eq!(23, c.position());
    }

    #[test]
    fn float_per_vertex_data_can_be_converted_to_rgb_uint8_colors() {
        let values: Vec<f32> = vec![0.0, 0.5, 1.0];
        let min_val: f32 = 0.0;
        let max_val: f32 = 1.0;
        let colors: Vec<u8> = values_to_colors(&values, min_val, max_val);
        assert_eq!(colors, vec![68, 1, 84, 38, 130, 142, 254, 232, 37]);
    }

    #[test]
    fn checked_mul_dims_rejects_negative_values() {
        let result = checked_mul_dims(&[10, -1, 10]);
        assert!(result.is_err());
        match result {
            Err(NeuroformatsError::InvalidHeaderValue(msg)) => {
                assert!(msg.contains("Negative"));
            }
            _ => panic!("Expected InvalidHeaderValue error"),
        }
    }

    #[test]
    fn checked_mul_dims_detects_overflow() {
        // Product of multiple i32::MAX values overflows usize.
        let result = checked_mul_dims(&[i32::MAX, i32::MAX, i32::MAX]);
        assert!(result.is_err());
    }

    #[test]
    fn checked_mul_dims_works_for_valid_input() {
        assert_eq!(checked_mul_dims(&[256, 256, 256, 1]).unwrap(), 16_777_216);
        assert_eq!(checked_mul_dims(&[1]).unwrap(), 1);
        assert_eq!(checked_mul_dims(&[0, 100]).unwrap(), 0);
    }

    #[test]
    fn validate_finite_f32_slice_rejects_nan() {
        let values = [1.0_f32, f32::NAN, 3.0];
        let result = validate_finite_f32_slice(&values, "test_field");
        assert!(result.is_err());
    }

    #[test]
    fn validate_finite_f32_slice_rejects_inf() {
        let values = [1.0_f32, f32::INFINITY, 3.0];
        let result = validate_finite_f32_slice(&values, "test_field");
        assert!(result.is_err());
    }

    #[test]
    fn validate_finite_f32_slice_rejects_neg_inf() {
        let values = [f32::NEG_INFINITY, 0.0];
        let result = validate_finite_f32_slice(&values, "test_field");
        assert!(result.is_err());
    }

    #[test]
    fn validate_finite_f32_slice_accepts_valid_values() {
        let values = [1.0_f32, -2.5, 0.0, 3.14];
        assert!(validate_finite_f32_slice(&values, "test_field").is_ok());
    }

    #[test]
    fn validate_finite_vertex_values_rejects_nan() {
        let values = [0.0_f32, f32::NAN, 1.0];
        let result = validate_finite_vertex_values(&values, "vertex");
        assert!(result.is_err());
    }

    #[test]
    fn variable_length_string_exceeding_max_is_rejected() {
        use std::io::{Cursor, Seek, Write};

        // Create a string that is too long (no terminator within max_len)
        let mut c = Cursor::new(Vec::<u8>::new());
        let long_string = vec![b'A'; 200];
        c.write(&long_string).unwrap();
        c.seek(std::io::SeekFrom::Start(0)).unwrap();

        let result = read_fs_variable_length_string(&mut c, 100);
        assert!(result.is_err());
        match result {
            Err(NeuroformatsError::StringTooLong) => {} // expected
            other => panic!("Expected StringTooLong, got {:?}", other),
        }
    }
}