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

Add new_vol_like convenience function #60

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/NIfTI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module NIfTI
using CodecZlib, Mmap, MappedArrays, TranscodingStreams

import Base.getindex, Base.size, Base.ndims, Base.length, Base.write, Base64
export NIVolume, niread, niwrite, voxel_size, time_step, vox, getaffine, setaffine
export NIVolume, niread, niwrite, voxel_size, time_step, vox, getaffine, setaffine, new_vol_like

include("parsers.jl")
include("extensions.jl")
Expand Down Expand Up @@ -176,6 +176,15 @@ function NIVolume(
(orientation[2, :]...,), (orientation[3, :]...,), string_tuple(intent_name, 16), NP1_MAGIC), extensions, raw)
end

"""
new_vol_like(vol::NIVolume{T}, raw::R) where {R,T}

Convenience function to create a new NIfTI volume with data `raw`, using
the header and extensions of `vol`.
"""
new_vol_like(vol::NIVolume{T}, raw::R) where {R,T} = NIVolume(vol.header,vol.extensions,raw)


# Validates the header of a volume and updates it to match the volume's contents
function niupdate(vol::NIVolume{T}) where {T}
vol.header.sizeof_hdr = SIZEOF_HDR1
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ const TEMP_GZIPPED_FILE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii.gz")
niwrite(TEMP_GZIPPED_FILE, vol)
niread(TEMP_GZIPPED_FILE)

# Test new_img_like
img = niread(GZIPPED_NII)
test_raw = ones(size(img))
new_img = new_vol_like(img, test_raw)
@test new_img.header == img.header
@test new_img.raw == test_raw
@test new_img.extensions == img.extensions

# Write and read DT_BINARY
const BOOL_WRITE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii")
const BIT_WRITE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii")
Expand Down