From fc587d44ebb595ac2edb98e970346fcc6ae037d7 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 6 Dec 2018 16:56:10 -0600 Subject: [PATCH 1/2] Add test to ensure pixelspacing gets saved --- test/REQUIRE | 2 +- test/runtests.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/REQUIRE b/test/REQUIRE index 4007dfe..194d701 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1,2 +1,2 @@ SimpleTraits -ImageMetadata +ImageMetadata 0.5.2 diff --git a/test/runtests.jl b/test/runtests.jl index ad7b3d2..22fcf10 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -144,6 +144,20 @@ include("unu-make.jl") save(outname, img) imgc = load(outname) @test img == imgc + + # With units + img = AxisArray(rand(N0f16, 5, 7), (:y, :x), (1.0u"mm", 1.2u"mm")) + outname = joinpath(writedir, "imagemeta.nhdr") + 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 From f3ad657638e05322b50e25dec364418aaec24f41 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 7 Dec 2018 07:41:06 -0600 Subject: [PATCH 2/2] Be more selective about writing orientation information ITK applies more exclusivity among fields, for compatibility we should try to impose the same exclusions. --- src/NRRD.jl | 20 ++++++++++++-------- test/runtests.jl | 3 +-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/NRRD.jl b/src/NRRD.jl index 014380d..ccd350a 100644 --- a/src/NRRD.jl +++ b/src/NRRD.jl @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 22fcf10..65dec67 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -146,8 +146,7 @@ include("unu-make.jl") @test img == imgc # With units - img = AxisArray(rand(N0f16, 5, 7), (:y, :x), (1.0u"mm", 1.2u"mm")) - outname = joinpath(writedir, "imagemeta.nhdr") + img = AxisArray(img, (:y, :x), (1.0u"mm", 1.2u"mm")) save(outname, img) imgc = load(outname) @test img == imgc