Skip to content

Commit

Permalink
better reorder (#558)
Browse files Browse the repository at this point in the history
* better reorder

* allow any dims object in reorder

* f

* docs

* bugfix
  • Loading branch information
rafaqz authored Oct 25, 2023
1 parent ceecf8b commit c403613
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Documenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
DATADEPS_ALWAYS_ACCEPT: true
run: |
julia --code-coverage=user --project=docs/ --color=yes docs/genfiles.jl
julia --code-coverage=user --project=docs/ --color=yes docs/make.jl
julia --code-coverage=user --project=docs/ --color=yes docs/make.jl
26 changes: 22 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@

"""
reorder(A::AbstractDimArray, order::Pair) => AbstractDimArray
reorder(A::Dimension, order::Order) => AbstractDimArray
reorder(A::Union{AbstractDimArray,AbstractDimStack}, order::Pair...)
reorder(A::Union{AbstractDimArray,AbstractDimStack}, order)
reorder(A::Dimension, order::Order)
Reorder every dims index/array to `order`, or reorder index for
the the given dimension(s) to the `Order` they wrap.
the the given dimension(s) in `order`.
`order` can be an [`Order`](@ref), or `Dimeension => Order` pairs.
`order` can be an [`Order`](@ref), `Dimension => Order` pairs.
A Tuple of Dimensions or any object that defines `dims` can be used
in which case dimensions are
If no axis reversal is required the same objects will be returned, without allocation.
## Example
```jldoctest
# Create a DimArray
da = DimArray([1 2 3; 4 5 6], (X(10:10:20), Y(300:-100:100)))
# Reverse it
rev = reverse(da, dims=Y)
# using `da` in reorder will return it to the original order
reorder(rev, da) == da
# output
true
"""
function reorder end

reorder(x, A::Union{AbstractDimArray,AbstractDimStack,AbstractDimIndices}) = reorder(x, dims(A))
reorder(x, ::Nothing) = throw(ArgumentError("object has no dimensions"))
reorder(x, p::Pair, ps::Vararg{Pair}) = reorder(x, (p, ps...))
reorder(x, ps::Tuple{Vararg{Pair}}) = reorder(x, Dimensions.pairdims(ps...))
# Reorder specific dims.
Expand All @@ -27,6 +44,7 @@ _reorder(x, orderdims::DimTuple) = _reorder(reorder(x, orderdims[1]), tail(order
_reorder(x, orderdims::Tuple{}) = x

reorder(x, orderdim::Dimension) = _reorder(val(orderdim), x, dims(x, orderdim))
reorder(x, orderdim::Dimension{<:LookupArray}) = _reorder(order(orderdim), x, dims(x, orderdim))

_reorder(neworder::Order, x, dim::Dimension) = _reorder(basetypeof(neworder), x, dim)
# Reverse the dimension index
Expand Down
13 changes: 13 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ end
d = reorder(dims(da, Y), ForwardOrdered())
@test order(d) isa ForwardOrdered
@test index(d) == 100:100:300

# reorder with dimension lookups
rev = reverse(da, dims=Y)
reo = reorder(rev, da)
@test rev != da
@test reo == da
@test dims(reo) == dims(da)
@test_throws MethodError reorder(rev, :test)
rev_s = reverse(s, dims=Y)
reo_s = reorder(rev_s, da)
@test rev_s != s
@test reo_s == s
@test dims(reo_s) == dims(s)
end

@testset "modify" begin
Expand Down

0 comments on commit c403613

Please sign in to comment.