Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend OffsetArrays.centered for AxisArray/Axis #57

Merged
merged 2 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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