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

Preserve axes in permutedims for AbstractVectors #243

Merged
merged 4 commits into from
May 30, 2021

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented May 29, 2021

The Base implementation of permutedims does not preserve axes for AbstractVector arguments. This is a temporary solution to the problem for OffsetArrays, as the problem is likely to arise for these.

Not sure if a general solution may be added to Base, as reshape for custom axis types does not exist as a concept currently. Perhaps something like this may be added in the longer term to Base and this method may be version-limited.

On master:

julia> permutedims(OffsetArray(2:3, 4:5))
1×2 reshape(::UnitRange{Int64}, 1, 2) with eltype Int64:
 2  3

After this PR:

julia> permutedims(OffsetArray(2:3, 4:5))
1×2 OffsetArray(reshape(::UnitRange{Int64}, 1, 2), 1:1, 4:5) with eltype Int64 with indices 1:1×4:5:
 2  3

@codecov
Copy link

codecov bot commented May 29, 2021

Codecov Report

Merging #243 (986fc0a) into master (41823a0) will increase coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #243      +/-   ##
==========================================
+ Coverage   95.39%   95.47%   +0.07%     
==========================================
  Files           5        5              
  Lines         413      420       +7     
==========================================
+ Hits          394      401       +7     
  Misses         19       19              
Impacted Files Coverage Δ
src/OffsetArrays.jl 98.23% <100.00%> (+0.01%) ⬆️
src/utils.jl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 41823a0...986fc0a. Read the comment docs.

test/runtests.jl Outdated
Comment on lines 1772 to 1779
@testset "permutedims" begin
a = OffsetArray(1:2, 2:3)
@test permutedims(a) == reshape(1:2, 1, 2:3)
a = OffsetArray([10,11], Base.OneTo(2))
@test permutedims(a) == reshape(10:11, 1, 1:2)
a = OffsetArray(SVector{2}(1,2), 3:4)
@test permutedims(a) == reshape(1:2, 1, 3:4)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add 2d array tests to make sure the behavior is consistent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but those should be unaffected as the method is only added for OffsetVectors

@@ -364,6 +364,10 @@ Base.reshape(A::OffsetVector, ::Colon) = A
Base.reshape(A::OffsetArray, inds::Union{Int,Colon}...) = reshape(parent(A), inds)
Base.reshape(A::OffsetArray, inds::Tuple{Vararg{Union{Int,Colon}}}) = reshape(parent(A), inds)

# permutedims in Base does not preserve axes, and can not be fixed in a non-breaking way
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you explain it a bit why this can't be fixed in a non-breaking way? The following version looks good to me.

julia> using OffsetArrays

julia> function Base.permutedims(v::AbstractVector)
           out = similar(v, (1, axes(v, 1)))
           copyto!(out, v)
       end

julia> permutedims(rand(4))
1×4 Matrix{Float64}:
 0.062106  0.864693  0.0235159  0.929236

julia> x = OffsetArray(rand(4,), -1);

julia> permutedims(x)
1×4 OffsetArray(::Matrix{Float64}, 1:1, 0:3) with eltype Float64 with indices 1:1×0:3:
 0.449456  0.0518755  0.689773  0.00949295

Copy link
Member Author

@jishnub jishnub May 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring for permutedims states that it must be a reshape for AbstractVectors (that is the underlying data is shared), however reshape does not currently accept custom axis types as arguments.

Although I might be misinterpreting what the docstring tries to say. Perhaps it uses reshape loosely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JuliaLang/julia#41003 is a good but controversial call; I don't think there will be a generic fix until #87 (comment) is solved; there will be many similar issues as identified by @mcabbott.

This specialization itself makes sense without being involved in type piracy so there's no objection in adding this to OffsetArrays.

@jishnub
Copy link
Member Author

jishnub commented May 30, 2021

c312eac fixes #235

@jishnub jishnub linked an issue May 30, 2021 that may be closed by this pull request
@jishnub
Copy link
Member Author

jishnub commented May 30, 2021

8409f01 pops the parent array in a reshape operation if no ranges are supplied as arguments, and the parent has Base.OneTo axes.

Previously:

julia> reshape(zeros(6), Int32(2), :)
2×3 OffsetArray(::Matrix{Float64}, 1:2, 1:3) with eltype Float64 with indices 1:2×1:3:
 0.0  0.0  0.0
 0.0  0.0  0.0

After this PR:

julia> reshape(zeros(6), Int32(2), :)
2×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0

@jishnub
Copy link
Member Author

jishnub commented May 30, 2021

@johnnychen94 good to merge?

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Efforts to minimize the effect due to type piracy is appreciated.

@jishnub jishnub merged commit f35032b into JuliaArrays:master May 30, 2021
@jishnub jishnub deleted the permutedims branch May 30, 2021 07:54
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

Successfully merging this pull request may close these issues.

Possible bug: StackOverflowError with inds::Tuple{Int32,Colon}
2 participants