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

map over PaddedAxis fails #56

Closed
fonsp opened this issue Nov 10, 2020 · 6 comments
Closed

map over PaddedAxis fails #56

fonsp opened this issue Nov 10, 2020 · 6 comments

Comments

@fonsp
Copy link

fonsp commented Nov 10, 2020

It seems like Base.map(::Function, ::PaddedAxis) is implemented, but it fails for this basic use case:

julia> using AxisIndices

julia> a = AxisArray(3:4, one_pad(sym_pad=2));

julia> i = eachindex(a)
one_pad(SimpleAxis(1:2); sym_pad=2)

julia> map(identity, i)
ERROR: ArgumentError: invalid index: AxisIndices.FillPad{typeof(oneunit)}(oneunit) of type AxisIndices.FillPad{typeof(oneunit)}
Stacktrace:
 [1] to_index(::Function) at ./indices.jl:297
 [2] to_index(::Array{Int64,1}, ::Function) at ./indices.jl:274
 [3] to_indices at ./indices.jl:325 [inlined]
 [4] to_indices at ./indices.jl:322 [inlined]
 [5] setindex! at ./abstractarray.jl:1149 [inlined]
 [6] unsafe_set_element! at /home/fons/.julia/packages/AxisIndices/eTKfQ/src/axis_array.jl:433 [inlined]
 [7] unsafe_setindex! at /home/fons/.julia/packages/ArrayInterface/rw2kK/src/indexing.jl:501 [inlined]
 [8] unsafe_setindex! at /home/fons/.julia/packages/ArrayInterface/rw2kK/src/indexing.jl:500 [inlined]
 [9] setindex! at /home/fons/.julia/packages/ArrayInterface/rw2kK/src/indexing.jl:487 [inlined]
 [10] setindex! at /home/fons/.julia/packages/AxisIndices/eTKfQ/src/arrays.jl:427 [inlined]
 [11] collect_to_with_first!(::AxisArray{Int64,1,Array{Int64,1},Tuple{AxisIndices.PaddedAxis{AxisIndices.FillPad{typeof(oneunit)},Int64,Int64,Int64,SimpleAxis{Int64,Base.OneTo{Int64}}}}}, ::Int64, ::Base.Generator{AxisIndices.PaddedAxis{AxisIndices.FillPad{typeof(oneunit)},Int64,Int64,Int64,SimpleAxis{Int64,Base.OneTo{Int64}}},typeof(identity)}, ::Int64) at ./array.jl:711
 [12] _collect at ./array.jl:706 [inlined]
 [13] collect_similar(::AxisIndices.PaddedAxis{AxisIndices.FillPad{typeof(oneunit)},Int64,Int64,Int64,SimpleAxis{Int64,Base.OneTo{Int64}}}, ::Base.Generator{AxisIndices.PaddedAxis{AxisIndices.FillPad{typeof(oneunit)},Int64,Int64,Int64,SimpleAxis{Int64,Base.OneTo{Int64}}},typeof(identity)}) at ./array.jl:630
 [14] map(::Function, ::AxisIndices.PaddedAxis{AxisIndices.FillPad{typeof(oneunit)},Int64,Int64,Int64,SimpleAxis{Int64,Base.OneTo{Int64}}}) at ./abstractarray.jl:2158
 [15] top-level scope at REPL[6]:1
@Tokazama
Copy link
Owner

Before I claim to have a fix for this, what is the return you want from map(identity, i)?

@fonsp
Copy link
Author

fonsp commented Nov 10, 2020

I'm not familiar with AxisIndicies, just found the bug and wrote a quick issue 😊

Maybe this is useful: in OffsetArrays, the result is also an offset array:

julia> using OffsetArrays

julia> a = OffsetArray(zeros(3), 20:22)
3-element OffsetArray(::Array{Float64,1}, 20:22) with eltype Float64 with indices 20:22:
 0.0
 0.0
 0.0

julia> i = eachindex(a)
OffsetArrays.IdOffsetRange(20:22)

julia> result = map(identity, i)
3-element OffsetArray(::Array{Int64,1}, 20:22) with eltype Int64 with indices 20:22:
 20
 21
 22

julia> result[1]
ERROR: BoundsError: attempt to access 3-element OffsetArray(::Array{Int64,1}, 20:22) with eltype Int64 with indices 20:22 at index [1]
Stacktrace:
 [1] throw_boundserror(::OffsetArray{Int64,1,Array{Int64,1}}, ::Tuple{Int64}) at ./abstractarray.jl:537
 [2] checkbounds at ./abstractarray.jl:502 [inlined]
 [3] getindex(::OffsetArray{Int64,1,Array{Int64,1}}, ::Int64) at /home/fons/.julia/packages/OffsetArrays/7j7P7/src/OffsetArrays.jl:275
 [4] top-level scope at REPL[7]:1

julia> result[20]
20

@fonsp
Copy link
Author

fonsp commented Nov 10, 2020

I think the general trend is that the result of map should have a structure similar to its input.

@Tokazama
Copy link
Owner

I'm not familiar with AxisIndicies, just found the bug and wrote a quick issue 😊

Thank you. It is greatly appreciated!

Maybe this is useful: in OffsetArrays, the result is also an offset array:

I borrowed liberally from concepts there so if you have AxisArray(3:4, offset(2)) it already does this but I missed a minor detail with padded axes.

How's this?

julia> a = AxisArray(3:4, one_pad(sym_pad=2));

julia> i = eachindex(a)
one_pad(SimpleAxis(1:2); sym_pad=2)

julia> map(identity, i)
6-element AxisArray(::Array{Int64,1}
  • axes:
     1 = -1:4
)
      1 
  -1  -1
  0    0
  1    1
  2    2
  3    3
  4    4  

@fonsp
Copy link
Author

fonsp commented Nov 10, 2020

perfect!

@Tokazama
Copy link
Owner

Fixed in v0.7.1

grahamas pushed a commit to grahamas/AxisIndices.jl that referenced this issue Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants