-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- More examples for lead/lag docstrings - can choose what dimension to apply lead/lag - Automatically chooses last dimension if no timedim
- Loading branch information
Showing
6 changed files
with
325 additions
and
160 deletions.
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
|
||
""" | ||
lag(A::AbstractArray, n::Integer) | ||
Shift the elements of `A` along the the axis of the dimension `dim` by `nshift` | ||
elements later. If `dim` is not specified then the dimension returned by | ||
`timedim` is used. If `A` does not have a time dimension then the last dimension | ||
is assumed to be the time dimension. | ||
## Examples | ||
```jldoctest | ||
julia> using TimeAxes | ||
julia> using Unitful: s | ||
julia> A = NamedAxisArray{(:time,)}(collect(1:5), (1:5)s) | ||
5-element NamedAxisArray{Int64,1} | ||
• time - 1 s:1 s:5 s | ||
1 s 1 | ||
2 s 2 | ||
3 s 3 | ||
4 s 4 | ||
5 s 5 | ||
julia> lag(A, 1) | ||
4-element NamedAxisArray{Int64,1} | ||
• time - 2 s:1 s:5 s | ||
2 s 1 | ||
3 s 2 | ||
4 s 3 | ||
5 s 4 | ||
julia> lag([1 2 3; 4 5 6; 7 8 9], 1, 1) | ||
2×3 Array{Int64,2}: | ||
1 2 3 | ||
4 5 6 | ||
julia> lag([1 2 3; 4 5 6; 7 8 9], 1, 2) | ||
3×2 Array{Int64,2}: | ||
1 2 | ||
4 5 | ||
7 8 | ||
``` | ||
""" | ||
|
||
@inline function lag(A::AbstractArray{T,N}, nshift::Integer, dim::Integer) where {T,N} | ||
return A[_lag_indices(Val(N), axes(A), nshift, dim)...] | ||
end | ||
|
||
@inline function lag(A::AxisArray{T,N}, nshift::Integer, dim::Int) where {T,N} | ||
indexing_indices = _lag_indices(Val(N), axes(A), nshift, dim) | ||
p = parent(A)[indexing_indices...] | ||
axs = _shift_axes(axes(A), indexing_indices, axes(p), dim) | ||
return unsafe_reconstruct(A, p, axs) | ||
end | ||
|
||
function lag(A::NamedAxisArray, dim::Int, n::Int) | ||
return NamedDimsArray{dimnames(A)}(lag(parent(A), dim, n)) | ||
end | ||
|
||
@inline function lag(A::AbstractArray{T,N}, nshift::Int) where {T,N} | ||
if has_timedim(A) | ||
return lag(A, nshift, timedim(A)) | ||
else | ||
return lag(A, nshift, N) | ||
end | ||
end | ||
|
||
@inline function _lag_indices(::Val{N}, inds::Tuple, nshift::Integer, dim::Integer) where {N} | ||
ntuple(Val(N)) do i | ||
if i === dim | ||
index = getfield(inds, i) | ||
firstindex(index):(lastindex(index) - nshift) | ||
else | ||
Colon() | ||
end | ||
end | ||
end | ||
|
||
#= | ||
lag(A::AbstractArray, n::Int) = _lag(A, timedim(A), n) | ||
function _lag(A::NamedAxisArray, dim::Int, n::Int) | ||
return NamedDimsArray{dimnames(A)}(_lag(parent(A), dim, n)) | ||
end | ||
function _lag(A::AxisArray{T,N}, dim::Int, n::Int) where {T,N} | ||
axs = axes(A) | ||
p = parent(A)[_lag_indices(axs, dim, n)...] | ||
newaxs = _lag_axes(axs, axes(p), dim, n) | ||
return AxisArray{eltype(p),N,typeof(p),typeof(newaxs)}(p, newaxs) | ||
end | ||
# _lag_indices | ||
@inline function _lag_indices(axs::NTuple{N,Any}, dim, n) where {N} | ||
if dim == 1 | ||
axis = first(axs) | ||
return (firstindex(axis):lastindex(axis) - n, ntuple(_-> :, Val(N-1))...) | ||
else | ||
return (:, _lag_indices(tail(axs), dim, n)...) | ||
end | ||
end | ||
_lag_indices(::Tuple{}, dim, n) = () | ||
# _lag_axes | ||
# TODO should probably add support for non AbstractAxis | ||
_lag_axes(::Tuple{}, ::Tuple{}, dim, n) = () | ||
@inline function _lag_axes(axs::Tuple, newinds::Tuple, dim, n) | ||
if dim == 1 | ||
return (_lag_axis(first(axs), first(newinds), n), map(assign_indices, tail(axs), tail(newinds))...) | ||
else | ||
return (assign_indices(first(axs), first(newinds)), _lag_axes(tail(axs), tail(newinds), dim, n)...) | ||
end | ||
end | ||
@inline function _lag_axis(axis::AbstractAxis, newinds, n) | ||
if is_indices_axis(axis) | ||
return assign_indices(axis, newinds) | ||
else | ||
return to_axis(axis, keys(axis)[(firstindex(axis) + n):lastindex(axis)], newinds) | ||
end | ||
end | ||
=# |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
""" | ||
lead(A::AbstractArray, nshift::Integer[, dim::Integer]) | ||
Shift the elements of `A` along the the axis of the dimension `dim` by `nshift` | ||
elements earlier. If `dim` is not specified then the dimension returned by | ||
`timedim` is used. If `A` does not have a time dimension then the last dimension | ||
is assumed to be the time dimension. | ||
## Examples | ||
```jldoctest | ||
julia> using TimeAxes | ||
julia> using Unitful: s | ||
julia> A = NamedAxisArray{(:time,)}(collect(1:5), (1:5)s) | ||
5-element NamedAxisArray{Int64,1} | ||
• time - 1 s:1 s:5 s | ||
1 s 1 | ||
2 s 2 | ||
3 s 3 | ||
4 s 4 | ||
5 s 5 | ||
julia> lead(A, 1) | ||
4-element NamedAxisArray{Int64,1} | ||
• time - 1 s:1 s:4 s | ||
1 s 2 | ||
2 s 3 | ||
3 s 4 | ||
4 s 5 | ||
julia> lead([1 2 3; 4 5 6; 7 8 9], 1, 1) | ||
2×3 Array{Int64,2}: | ||
4 5 6 | ||
7 8 9 | ||
julia> lead([1 2 3; 4 5 6; 7 8 9], 1, 2) | ||
3×2 Array{Int64,2}: | ||
2 3 | ||
5 6 | ||
8 9 | ||
``` | ||
""" | ||
@inline function lead(A::AbstractArray{T,N}, nshift::Integer, dim::Integer) where {T,N} | ||
return A[_lead_indices(Val(N), axes(A), nshift, dim)...] | ||
end | ||
|
||
@inline function lead(A::AxisArray{T,N}, nshift::Integer, dim::Int) where {T,N} | ||
indexing_indices = _lead_indices(Val(N), axes(A), nshift, dim) | ||
p = parent(A)[indexing_indices...] | ||
axs = _shift_axes(axes(A), indexing_indices, axes(p), dim) | ||
return unsafe_reconstruct(A, p, axs) | ||
end | ||
|
||
function lead(A::NamedAxisArray, dim::Int, n::Int) | ||
return NamedDimsArray{dimnames(A)}(lead(parent(A), dim, n)) | ||
end | ||
|
||
@inline function lead(A::AbstractArray{T,N}, nshift::Int) where {T,N} | ||
if has_timedim(A) | ||
return lead(A, nshift, timedim(A)) | ||
else | ||
return lead(A, nshift, N) | ||
end | ||
end | ||
|
||
@inline function _lead_indices(::Val{N}, inds::Tuple, nshift::Integer, dim::Integer) where {N} | ||
ntuple(Val(N)) do i | ||
index = getfield(inds, i) | ||
if i === dim | ||
index = getfield(inds, i) | ||
(firstindex(index) + nshift):lastindex(index) | ||
else | ||
Colon() | ||
end | ||
end | ||
end |
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
Oops, something went wrong.
78211b4
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.
@JuliaRegistrator register
Release notes:
78211b4
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.
Registration pull request created: JuliaRegistries/General/17656
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: