From 52150be7ce6a602ecf3c5ae104a12c9fe4a8ae24 Mon Sep 17 00:00:00 2001 From: donghoon Date: Fri, 28 Sep 2018 16:39:02 -0500 Subject: [PATCH 1/2] Add test for `maximum` and `mean` of an AxisArray image across a specific dimension. --- test/core.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/core.jl b/test/core.jl index e9f026b..146c1b5 100644 --- a/test/core.jl +++ b/test/core.jl @@ -358,4 +358,13 @@ end @inferred cv[1,1,1] end +@testset "AxisArray_CartesianIndex" begin + M = reshape([1,2,3,4], 2,2) + M = AxisArray(M) + img = ImageMeta(M) + @test 1 == img[1, CartesianIndex(1)] #Int and cartesianindex + @test maximum(img,dims=2) == reshape([3,4],2,1) + @test mean(img,dims=1) == reshape([1.5,3.5], 1,2) +end + nothing From 1900a6f9067cc3ada0c0c4980b09122433afe036 Mon Sep 17 00:00:00 2001 From: donghoon Date: Fri, 28 Sep 2018 17:30:07 -0500 Subject: [PATCH 2/2] `maybe_wrap` function has been added. This function returns either a single value or an array with properties, depending on its input type. Thus, now `getindex` can read a single element from an AxisArray image. Consequently, some functions, such as `mean` and `maximum`, can be computed along a specific dimension. --- src/ImageMetadata.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ImageMetadata.jl b/src/ImageMetadata.jl index 3309c82..6b60cbe 100644 --- a/src/ImageMetadata.jl +++ b/src/ImageMetadata.jl @@ -90,11 +90,16 @@ for AType in (ImageMeta, ImageMetaAxis) end @inline function Base.getindex(img::ImageMetaAxis, ax::Axis, I...) - copyproperties(img, img.data[ax, I...]) + result = img.data[ax, I...] + maybe_wrap(img, result) end @inline function Base.getindex(img::ImageMetaAxis, i::Union{Integer,AbstractVector,Colon}, I...) - copyproperties(img, img.data[i, I...]) + result = img.data[i, I...] + maybe_wrap(img, result) end +maybe_wrap(img::ImageMeta{T}, result::T) where T = result +maybe_wrap(img::ImageMeta{T}, result::AbstractArray{T}) where T = copyproperties(img, result) + @inline function Base.setindex!(img::ImageMetaAxis, val, ax::Axis, I...) setindex!(img.data, val, ax, I...)