Skip to content

Commit

Permalink
Merge pull request #52 from JuliaNeuroscience/5.4
Browse files Browse the repository at this point in the history
Patch bump for #51 and changed `nidim` to `to_dim_i16` b/c it is slightly faster and preps for
NIfTI-v2 headers
  • Loading branch information
Tokazama authored Apr 2, 2021
2 parents 9eaad22 + ca36e73 commit 8172491
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "NIfTI"
uuid = "a3a9e032-41b5-5fc4-967a-a6b7a19844d3"
version = "0.5.3"
version = "0.5.4"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
35 changes: 2 additions & 33 deletions src/NIfTI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ function byteswap(hdr::NIfTI1Header)
hdr
end

function string_tuple(x::String, n::Int)
a = codeunits(x)
padding = zeros(UInt8, n-length(a))
(a..., padding...)
end
string_tuple(x::AbstractString) = string_tuple(bytestring(x))

struct NIVolume{T<:Number,N,R} <: AbstractArray{T,N}
header::NIfTI1Header
extensions::Vector{NIfTIExtension}
Expand All @@ -131,22 +124,6 @@ NIVolume(header::NIfTI1Header, extensions::Vector{NIfTIExtension}, raw::Abstract
NIVolume(header::NIfTI1Header, raw::AbstractArray{Bool,N}) where {N} =
NIVolume{Bool,N,typeof(raw)}(header, NIfTIExtension[], raw)

# Conversion factors to mm/ms
# http://nifti.nimh.nih.gov/nifti-1/documentation/nifti1fields/nifti1fields_pages/xyzt_units.html
const SPATIAL_UNIT_MULTIPLIERS = [
1000, # 1 => NIfTI_UNITS_METER
1, # 2 => NIfTI_UNITS_MM
0.001 # 3 => NIfTI_UNITS_MICRON
]
const TIME_UNIT_MULTIPLIERS = [
1000, # NIfTI_UNITS_SEC
1, # NIfTI_UNITS_MSEC
0.001, # NIfTI_UNITS_USEC
1, # NIfTI_UNITS_HZ
1, # NIfTI_UNITS_PPM
1 # NIfTI_UNITS_RADS
]

# Always in mm
voxel_size(header::NIfTI1Header) =
[header.pixdim[i] * SPATIAL_UNIT_MULTIPLIERS[header.xyzt_units & Int8(3)] for i = 2:min(header.dim[1], 3)+1]
Expand Down Expand Up @@ -179,14 +156,6 @@ function dim_info(header::NIfTI1Header, dim_info::Tuple{T, T, T}) where {T<:Inte
header.dim_info = to_dim_info(dim_info)
end

# Gets dim to be used in header
function nidim(x::AbstractArray)
dim = ones(Int16, 8)
dim[1] = ndims(x)
dim[2:dim[1]+1] = [size(x)...]
return (dim...,)
end

# Gets the size of a type in bits
nibitpix(t::Type) = Int16(sizeof(t)*8)
nibitpix(::Type{Bool}) = Int16(1)
Expand Down Expand Up @@ -334,7 +303,7 @@ function NIVolume(
end

NIVolume(NIfTI1Header(SIZEOF_HDR1, string_tuple(data_type, 10), string_tuple(db_name, 18), extents, session_error,
regular, to_dim_info(dim_info), nidim(raw), intent_p1, intent_p2,
regular, to_dim_info(dim_info), to_dim_i16(size(raw)), intent_p1, intent_p2,
intent_p3, intent_code, eltype_to_int16(t), nibitpix(t),
slice_start, (qfac, voxel_size..., time_step, 0, 0, 0), 352,
scl_slope, scl_inter, slice_end, slice_code,
Expand All @@ -348,7 +317,7 @@ end
# 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
vol.header.dim = nidim(vol.raw)
vol.header.dim = to_dim_i16(size(vol.raw))
vol.header.datatype = eltype_to_int16(T)
vol.header.bitpix = nibitpix(T)
vol.header.vox_offset = isempty(vol.extensions) ? Int32(352) :
Expand Down
34 changes: 34 additions & 0 deletions src/parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,41 @@ function hdr_to_img(file::AbstractString)
end
end

function string_tuple(x::String, n::Int)
a = codeunits(x)
padding = zeros(UInt8, n-length(a))
(a..., padding...)
end
string_tuple(x::AbstractString) = string_tuple(bytestring(x))

# Conversion factors to mm/ms
# http://nifti.nimh.nih.gov/nifti-1/documentation/nifti1fields/nifti1fields_pages/xyzt_units.html
const SPATIAL_UNIT_MULTIPLIERS = [
1000, # 1 => NIfTI_UNITS_METER
1, # 2 => NIfTI_UNITS_MM
0.001 # 3 => NIfTI_UNITS_MICRON
]
const TIME_UNIT_MULTIPLIERS = [
1000, # NIfTI_UNITS_SEC
1, # NIfTI_UNITS_MSEC
0.001, # NIfTI_UNITS_USEC
1, # NIfTI_UNITS_HZ
1, # NIfTI_UNITS_PPM
1 # NIfTI_UNITS_RADS
]

# Gets dim to be used in header
function to_dim_i16(x::NTuple{N}) where {N}
return ntuple(Val(8)) do i
if i === 1
Int16(N)
elseif i > (N + 1)
zero(Int16)
else
@inbounds(getfield(x, i - 1))
end
end
end

#=
to_spatial_units(x::Int8) = _to_spatial_units(x & 0x07)
Expand Down

2 comments on commit 8172491

@Tokazama
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/33386

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.4 -m "<description of version>" 8172491ac67f89334a86a358c799480ce2313200
git push origin v0.5.4

Please sign in to comment.