Skip to content

Commit

Permalink
Merge pull request #28 from JuliaIO/teh/meta
Browse files Browse the repository at this point in the history
Apply more exclusions with regards to header fields when writing
  • Loading branch information
timholy authored Dec 7, 2018
2 parents e0fa983 + f3ad657 commit 56bbd5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/NRRD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ function load(io::Stream{format"NRRD"}, Tuser::Type=Any; mode="r", mmap=:auto)
isa(axs, Dims) ? A : AxisArray(A, axs)
end

function save(f::File{format"NRRD"}, img::AbstractArray; props::Dict = Dict{String,Any}(), keyvals=nothing, comments=nothing)
function save(f::File{format"NRRD"}, img::AbstractArray; kwargs...)
open(f, "w") do io
write(io, magic(format"NRRD"))
save(io, img; props=props, keyvals=keyvals, comments=comments)
save(io, img; kwargs...)
end
end

function save(io::Stream{format"NRRD"}, img::AbstractArray{T}; props::Dict = Dict{String,Any}(), keyvals=nothing, comments=nothing) where T
function save(io::Stream{format"NRRD"}, img::AbstractArray{T}; props::Dict = Dict{String,Any}(), keyvals=nothing, comments=nothing, kwargs...) where T
axs = axisinfo(img)
header = headerinfo(T, axs)
header = headerinfo(T, axs; kwargs...)
header_eltype!(header, T)
# copy fields from props to override those in header
for (k, v) in props
Expand Down Expand Up @@ -378,6 +378,7 @@ function headerinfo(T, axs)
if isa(axs, Base.Indices)
axs = map(length, axs)
end
specifyorientation = false
if isa(axs, Dims)
header["sizes"] = [axs...]
else
Expand All @@ -387,8 +388,7 @@ function headerinfo(T, axs)
isspace = map(s->!startswith(string(s), "time"), axnames)
if haskey(axes2space, axnames)
header["space"] = axes2space[axnames]
else
header["space dimension"] = sum(isspace)
specifyorientation = true
end
header["kinds"] = [isspc ? "domain" : "time" for isspc in isspace]
if !all(isdefaultname, axnames)
Expand All @@ -402,10 +402,14 @@ function headerinfo(T, axs)
header["units"] = [unitstr...]
end
if !all(x->x==1, spacing)
header["spacings"] = [spacing...]
if specifyorientation
header["space directions"] = [ntuple(d->d==i ? spacing[i] : 0, length(spacing)) for i = 1:length(spacing)]
else
header["spacings"] = [Float64.(spacing)...]
end
end
origin = map(x->isa(x, Quantity) ? ustrip(x) : x, map(first, rng))
if any(x->x!=0, origin)
if specifyorientation && any(x->x!=0, origin)
header["space origin"] = [origin[[isspace...]]...]
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SimpleTraits
ImageMetadata
ImageMetadata 0.5.2
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ include("unu-make.jl")
save(outname, img)
imgc = load(outname)
@test img == imgc

# With units
img = AxisArray(img, (:y, :x), (1.0u"mm", 1.2u"mm"))
save(outname, img)
imgc = load(outname)
@test img == imgc
@test pixelspacing(imgc) == (1.0u"mm", 1.2u"mm")
img = ImageMeta(img, info=false)
save(outname, img)
imgc = load(outname)
@test img == imgc
@test AxisArrays.HasAxes(img) isa AxisArrays.HasAxes{true}
@test pixelspacing(imgc) == (1.0u"mm", 1.2u"mm")
end

GC.gc() # to close any mmapped files
Expand Down

0 comments on commit 56bbd5b

Please sign in to comment.