Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write ndarray to nifti #21

Merged
merged 9 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ version = ">=0.10.12,<0.13.0"
[dev-dependencies]
approx = "0.3.0"
pretty_assertions = "0.5.0"
tempfile = "3.0"

[[example]]
name = "niftidump"
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! ```no_run
//! use nifti::{NiftiObject, InMemNiftiObject, NiftiVolume};
//! # use nifti::error::Result;
//!
//!
//! # fn run() -> Result<()> {
//! let obj = InMemNiftiObject::from_file("myvolume.nii.gz")?;
//! // use obj
Expand Down Expand Up @@ -44,7 +44,7 @@
//! # Ok(())
//! # }
//! ```
//!
//!
#![deny(missing_debug_implementations)]
#![warn(missing_docs, unused_extern_crates, trivial_casts, unused_results)]

Expand All @@ -64,6 +64,7 @@ pub mod object;
pub mod volume;
pub mod error;
pub mod typedef;
#[cfg(feature = "ndarray_volumes")] pub mod writer;
mod util;

pub use error::{NiftiError, Result};
Expand Down
14 changes: 14 additions & 0 deletions src/volume/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use safe_transmute::guarded_transmute_pod_vec_permissive;
use error::Result;
use num_traits::cast::AsPrimitive;
use util::{Endianness, convert_bytes_to};
use NiftiType;

/// Interface for linear (affine) transformations to values. Multiple
/// implementations are needed because the original type `T` may not have
Expand Down Expand Up @@ -96,6 +97,9 @@ where
/// values.
pub trait DataElement: 'static + Sized + Copy + AsPrimitive<u8> + AsPrimitive<f32> + AsPrimitive<f64>
{
/// The `datatype` mapped to the type T
const DATA_TYPE: NiftiType;

/// For defining how this element is linearly transformed to another.
type Transform: LinearTransform<Self>;

Expand All @@ -111,6 +115,7 @@ pub trait DataElement: 'static + Sized + Copy + AsPrimitive<u8> + AsPrimitive<f3
}

impl DataElement for u8 {
const DATA_TYPE: NiftiType = NiftiType::Uint8;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, _: Endianness) -> Result<Vec<Self>> {
Ok(vec)
Expand All @@ -120,6 +125,7 @@ impl DataElement for u8 {
}
}
impl DataElement for i8 {
const DATA_TYPE: NiftiType = NiftiType::Int8;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, _: Endianness) -> Result<Vec<Self>> {
Ok(guarded_transmute_pod_vec_permissive(vec))
Expand All @@ -129,6 +135,7 @@ impl DataElement for i8 {
}
}
impl DataElement for u16 {
const DATA_TYPE: NiftiType = NiftiType::Uint16;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -138,6 +145,7 @@ impl DataElement for u16 {
}
}
impl DataElement for i16 {
const DATA_TYPE: NiftiType = NiftiType::Int16;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -147,6 +155,7 @@ impl DataElement for i16 {
}
}
impl DataElement for u32 {
const DATA_TYPE: NiftiType = NiftiType::Uint32;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -156,6 +165,7 @@ impl DataElement for u32 {
}
}
impl DataElement for i32 {
const DATA_TYPE: NiftiType = NiftiType::Int32;
type Transform = LinearTransformViaF32;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -165,6 +175,7 @@ impl DataElement for i32 {
}
}
impl DataElement for u64 {
const DATA_TYPE: NiftiType = NiftiType::Uint64;
type Transform = LinearTransformViaF64;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -174,6 +185,7 @@ impl DataElement for u64 {
}
}
impl DataElement for i64 {
const DATA_TYPE: NiftiType = NiftiType::Int64;
type Transform = LinearTransformViaF64;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -183,6 +195,7 @@ impl DataElement for i64 {
}
}
impl DataElement for f32 {
const DATA_TYPE: NiftiType = NiftiType::Float32;
type Transform = LinearTransformViaOriginal;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand All @@ -192,6 +205,7 @@ impl DataElement for f32 {
}
}
impl DataElement for f64 {
const DATA_TYPE: NiftiType = NiftiType::Float64;
type Transform = LinearTransformViaOriginal;
fn from_raw_vec(vec: Vec<u8>, e: Endianness) -> Result<Vec<Self>> {
Ok(convert_bytes_to(vec, e))
Expand Down
Loading