<commit_before>//! Importers for RAW volume files. A raw volume file is the raw bytes of
//! volume data in little endian, with X increasing first, then Y, then Z.

use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::iter;
use std::mem;
use std::path::Path;

use num::{self, NumCast};

use crate::vec3::Vec3i;
use crate::vol::Volume;

/// Import a RAW volume file with the set dimensions. The volume data is
/// expected to be of type T which should be a primitive scalar type
pub fn import<T: NumCast>(path: &Path, dims: Vec3i) -> Volume {
    let mut f = match File::open(&path) {
        Ok(f) => BufReader::new(f),
        Err(e) => panic!("Error opening volume `{:?}`: {}", path, e),
    };
    let mut data: Vec<_> = iter::repeat(0u8)
        .take((dims.x * dims.y * dims.z) as usize * mem::size_of::<T>())
        .collect();
    f.read_exact(&mut data[..])
        .expect("Failed to read entire RAW volume");
    let data: Vec<f32> = data
        .chunks(mem::size_of::<T>())
        .map(|x| unsafe { mem::transmute_copy::<u8, T>(&x[0]) })
        .map(|x| num::cast(x).unwrap())
        .collect();
    let mut volume = Volume::new(dims);
    volume.set_region(&data[..], Vec3i::broadcast(0), dims);
    volume
}
<commit_msg>fix clippy failure found by weekly job<commit_after>//! Importers for RAW volume files. A raw volume file is the raw bytes of
//! volume data in little endian, with X increasing first, then Y, then Z.

use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::iter;
use std::mem;
use std::path::Path;

use num::{self, NumCast};

use crate::vec3::Vec3i;
use crate::vol::Volume;

/// Import a RAW volume file with the set dimensions. The volume data is
/// expected to be of type T which should be a primitive scalar type
pub fn import<T: NumCast>(path: &Path, dims: Vec3i) -> Volume {
    let mut f = match File::open(path) {
        Ok(f) => BufReader::new(f),
        Err(e) => panic!("Error opening volume `{:?}`: {}", path, e),
    };
    let mut data: Vec<_> = iter::repeat(0u8)
        .take((dims.x * dims.y * dims.z) as usize * mem::size_of::<T>())
        .collect();
    f.read_exact(&mut data[..])
        .expect("Failed to read entire RAW volume");
    let data: Vec<f32> = data
        .chunks(mem::size_of::<T>())
        .map(|x| unsafe { mem::transmute_copy::<u8, T>(&x[0]) })
        .map(|x| num::cast(x).unwrap())
        .collect();
    let mut volume = Volume::new(dims);
    volume.set_region(&data[..], Vec3i::broadcast(0), dims);
    volume
}

{'fraction_non_alphanumeric': 0.18328100470957615, 'fraction_numerical': 0.008634222919937205, 'mean_word_length': 3.284033613445378, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '781553', 'n_tokens_mistral': 888, 'n_tokens_neox': 889, 'n_words': 274}