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

Reshape with an offset parent array #245

Merged
merged 10 commits into from
Jul 15, 2021
Merged

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 15, 2021

Fixes #219

Now

julia> r = OffsetArray(IdentityUnitRange(0:1), -1)
Base.IdentityUnitRange(0:1) with indices -1:0

julia> reshape(r, 2) # Int arguments strip offsets
0:1

julia> reshape(r, (2,))
0:1

julia> reshape(r, :) # Colon preserves offsets for an OffsetVector
Base.IdentityUnitRange(0:1) with indices -1:0

julia> reshape(r, (:,))
Base.IdentityUnitRange(0:1) with indices -1:0

The last one is a bugfix, as on master

julia> reshape(r, :)
Base.IdentityUnitRange(0:1) with indices -1:0

julia> reshape(r, (:,))
ERROR: ArgumentError: offset arrays are not supported but got an array with index other than 1
Stacktrace:

@codecov
Copy link

codecov bot commented Jun 15, 2021

Codecov Report

Merging #245 (4aef07c) into master (7a43b4c) will increase coverage by 0.15%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #245      +/-   ##
==========================================
+ Coverage   95.26%   95.41%   +0.15%     
==========================================
  Files           5        5              
  Lines         422      436      +14     
==========================================
+ Hits          402      416      +14     
  Misses         20       20              
Impacted Files Coverage Δ
src/OffsetArrays.jl 98.29% <100.00%> (+0.06%) ⬆️
src/utils.jl 97.95% <100.00%> (+0.23%) ⬆️
src/axes.jl 100.00% <0.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 7a43b4c...4aef07c. Read the comment docs.

@johnnychen94
Copy link
Member

johnnychen94 commented Jun 15, 2021

julia> reshape(r, :) # Colon preserves offsets for an OffsetVector
Base.IdentityUnitRange(0:1) with indices -1:0

On whether Colon should preserve the offset, looks like there's a back&forth here. #228 (comment)

I still believe we should preserve the offset because it provides more information. But that's definitely breaking, so need to properly deprecate it before v2.0.

@jishnub
Copy link
Member Author

jishnub commented Jun 16, 2021

This is actually the present behavior (a single colon preserves offsets for an OffsetVector), and this PR doesn't change that.

The present behavior (on master) is:

julia> r = ones(2:3);

julia> reshape(r, :) # A single colon preserves offsets for an OffsetVector
2-element OffsetArray(::Vector{Float64}, 2:3) with eltype Float64 with indices 2:3:
 1.0
 1.0

julia> reshape(ones(2:3, 2:3), :) # offsets are stripped for an OffsetArray other than an OffsetVector
4-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0

julia> reshape(r, (2, :)) # colon with any other argument strips offsets
2×1 Matrix{Float64}:
 1.0
 1.0

julia> reshape(ones(2:3, 2:3), (3:4, :))
2×2 OffsetArray(::Matrix{Float64}, 3:4, 1:2) with eltype Float64 with indices 3:4×1:2:
 1.0  1.0
 1.0  1.0

In that regard I think #228 is the right step, where a Colon preserves offsets if possible. This PR preserves the present behavior and is not breaking.

src/OffsetArrays.jl Outdated Show resolved Hide resolved
src/OffsetArrays.jl Outdated Show resolved Hide resolved
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.

The last round of review with only some style suggestions. Feel free to merge when you're ready.

src/OffsetArrays.jl Outdated Show resolved Hide resolved
src/OffsetArrays.jl Outdated Show resolved Hide resolved
src/OffsetArrays.jl Outdated Show resolved Hide resolved
Comment on lines 350 to 352
# try to pass on the indices as received to the parent.
# If the parent doesn't define an appropriate method, this will fall back to using the lengths
# Short-circuit the case with matching indices to circumvent the Base restriction to 1-based indices
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment still useful? I guess no?

@jishnub
Copy link
Member Author

jishnub commented Jul 15, 2021

@johnnychen94 I've added some bugfixes, although the fix seems a bit hacky. Could you take a look? The main changes are in 9f91760, where I add _reshape2 to deal with cases where no_offset_view applied to an OffsetArray returns another OffsetArray, which leads to stackoverflows. Now sure what's the best thing to do here, currently I just push the reshape to the parent and hope for the best.

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.

I can basically get your points here. And yes it is also tricky for me.

There might be some deeply hidden bugs but nothing suspicious to me so far.

@jishnub jishnub merged commit 1263c2a into JuliaArrays:master Jul 15, 2021
@jishnub jishnub deleted the fixreshape branch July 15, 2021 14:52
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.

Reshape is broken if the parent array does not use 1-based indexing
2 participants