Skip to content

Commit

Permalink
extend OffsetArrays.centered for AxisArray/Axis (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 authored Aug 20, 2021
1 parent 4cfa8c4 commit 38b4a0b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ImageAxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export AxisArray, Axis, axisnames, axisvalues, axisdim, atindex, atvalue, collap

@reexport using ImageCore
using ImageCore.MappedArrays
using ImageCore.OffsetArrays


export # types
Expand Down Expand Up @@ -440,4 +441,7 @@ function AxisArrays._summary(io, A::AxisArray{T,N}) where {T<:Union{Fractional,C
println(io, ",$N,...} with axes:")
end

# glue codes
include("offsetarrays.jl")

end # module
7 changes: 7 additions & 0 deletions src/offsetarrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if isdefined(OffsetArrays, :centered)
# Requires OffsetArrays v1.9
# https://github.com/JuliaArrays/OffsetArrays.jl/pull/242
# Originally from ImageFiltering https://github.com/JuliaImages/ImageFiltering.jl/pull/214
OffsetArrays.centered(ax::Axis{name}) where name = Axis{name}(OffsetArrays.centered(ax.val))
OffsetArrays.centered(a::AxisArray) = AxisArray(OffsetArrays.centered(a.data), OffsetArrays.centered.(AxisArrays.axes(a)))
end
21 changes: 21 additions & 0 deletions test/offsetarrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@testset "centered" begin
if isdefined(OffsetArrays, :centered) # OffsetArrays v1.9
check_range(r, f, l) = (@test first(r) == f; @test last(r) == l)
check_range_axes(r, f, l) = check_range(Base.axes(r)[1], f, l)

check_range(Base.axes(OffsetArrays.centered(1:3))[1], -1, 1)
a = AxisArray(rand(3, 3), Axis{:y}(0.1:0.1:0.3), Axis{:x}(1:3))

ca = OffsetArrays.centered(a)
axs = Base.axes(ca)
check_range(axs[1], -1, 1)
check_range(axs[2], -1, 1)
@test ca[OffsetArrays.center(ca)...] == a[OffsetArrays.center(a)...]

axs = AxisArrays.axes(ca)
check_range(axs[1].val, 0.1, 0.3)
check_range(axs[2].val, 1, 3)
check_range_axes(axs[1].val, -1, 1)
check_range_axes(axs[1].val, -1, 1)
end
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test, ImageCore
using ImageCore.MappedArrays
using ImageCore.OffsetArrays
import AxisArrays

ambs = detect_ambiguities(ImageCore,AxisArrays,Base,Core)
Expand Down Expand Up @@ -232,4 +233,8 @@ end
@test ImageAxes.filter_streamed((1,2), S) == (2,)
end

@testset "OffsetArrays" begin
include("offsetarrays.jl")
end

nothing

0 comments on commit 38b4a0b

Please # to comment.