-
Notifications
You must be signed in to change notification settings - Fork 40
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
add center/centered helpers #242
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -626,6 +626,75 @@ _no_offset_view(::Tuple{<:Base.OneTo,Vararg{<:Base.OneTo}}, A::AbstractUnitRange | |
_no_offset_view(::Any, A::AbstractArray) = OffsetArray(A, Origin(1)) | ||
_no_offset_view(::Any, A::AbstractUnitRange) = UnitRange(A) | ||
|
||
##### | ||
# center/centered | ||
# These two helpers are deliberately not exported; their meaning can be very different in | ||
# other scenarios and will be very likely to cause name conflicts if exported. | ||
##### | ||
""" | ||
center(A, [r::RoundingMode=RoundDown])::Dims | ||
|
||
Return the center coordinate of given array `A`. If `size(A, k)` is even, | ||
a rounding procedure will be applied with mode `r`. | ||
|
||
!!! compat "OffsetArrays 1.9" | ||
This method requires at least OffsetArrays 1.9. | ||
|
||
# Examples | ||
|
||
```jldoctest; setup=:(using OffsetArrays) | ||
A = reshape(collect(1:9), 3, 3) | ||
c = OffsetArrays.center(A) # (2, 2) | ||
A[c...] == 5 # true | ||
|
||
Ao = OffsetArray(A, -2, -2) | ||
c = OffsetArrays.center(Ao) # (0, 0) | ||
Ao[c...] == 5 # true | ||
|
||
# output | ||
true | ||
``` | ||
|
||
To shift the center coordinate of the given array to `(0, 0, ...)`, you | ||
can use [`centered`](@ref OffsetArrays.centered). | ||
""" | ||
function center(A::AbstractArray, r::RoundingMode=RoundDown) | ||
map(axes(A)) do inds | ||
round(Int, (length(inds)-1)/2, r) + first(inds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default julia> A = reshape(collect(1:6), 2, 3)
2×3 Matrix{Int64}:
1 3 5
2 4 6
julia> OffsetArrays.centered(A) == ImageFiltering.centered(A)
true |
||
end | ||
end | ||
|
||
""" | ||
centered(A, r::RoundingMode=RoundDown) -> Ao | ||
|
||
Shift the center coordinate of array `A` to `(0, 0, ...)`. If `size(A, k)` | ||
is even, a rounding procedure will be applied with mode `r`. | ||
|
||
!!! compat "OffsetArrays 1.9" | ||
This method requires at least OffsetArrays 1.9. | ||
|
||
# Examples | ||
|
||
```jldoctest; setup=:(using OffsetArrays) | ||
A = reshape(collect(1:9), 3, 3) | ||
Ao = OffsetArrays.centered(A) | ||
Ao[0, 0] == 5 # true | ||
|
||
A = reshape(collect(1:9), 3, 3) | ||
Ao = OffsetArray(A, OffsetArrays.Origin(0)) | ||
Aoo = OffsetArrays.centered(Ao) | ||
Aoo[0, 0] == 5 # true | ||
|
||
# output | ||
true | ||
``` | ||
|
||
To query the center coordinate of the given array, you can | ||
instead use [`center`](@ref OffsetArrays.center). | ||
""" | ||
centered(A::AbstractArray, r::RoundingMode=RoundDown) = OffsetArray(A, .-center(A, r)) | ||
|
||
|
||
#### | ||
# work around for segfault in searchsorted* | ||
# https://github.com/JuliaLang/julia/issues/33977 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should these be doctests instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doctests are already in the
src/
andtest/
so I think it's okay to just follow the previous doc format here.