Skip to content

Commit

Permalink
start fixing eachslice
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Jul 27, 2023
1 parent 67dfb03 commit ac10c2c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/array/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ end
"""
function Base.eachslice(A::AbstractDimArray; dims)
dimtuple = _astuple(dims)
all(hasdim(A, dimtuple...)) || throw(DimensionMismatch("A doesn't have all dimensions $dims"))
all(hasdim(A, dimtuple)) || throw(DimensionMismatch("A doesn't have all dimensions $dims"))
_eachslice(A, dimtuple)
end
else
@inline function Base.eachslice(A::AbstractDimArray; dims, drop=true)
dimtuple = _astuple(dims)
all(hasdim(A, dimtuple...)) || throw(DimensionMismatch("A doesn't have all dimensions $dims"))
all(hasdim(A, dimtuple)) || throw(DimensionMismatch("A doesn't have all dimensions $dims"))
_eachslice(A, dimtuple, drop)
end
Base.@constprop :aggressive function _eachslice(A::AbstractDimArray{T,N}, dims, drop) where {T,N}
Expand Down
11 changes: 7 additions & 4 deletions src/dimindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Base.axes(di::AbstractDimIndices) = map(d -> axes(d, 1), dims(di))
for f in (:getindex, :view, :dotview)
@eval begin
@propagate_inbounds Base.$f(A::AbstractDimIndices, i1::Selector, I::Selector...) =
Base.$f(A, dims2indices(A, i1, I)...)
@propagate_inbounds function Base.$f(A::AbstractDimIndices, i1::Dimension, I::Dimension...; kw...)
Base.$f(A, dims2indices(A, i1, I..., kwdims(values(kw))...)...)
Base.$f(A, dims2indices(A, (i1, I))...)
@propagate_inbounds function Base.$f(A::AbstractDimIndices, I::Dimension...; kw...)
dims2indices(A, (i1..., kwdims(values(kw))...))
Base.$f(A, dims2indices(A, (I..., kwdims(values(kw))...))...)
end
end
end
Expand Down Expand Up @@ -65,7 +66,7 @@ function Base.getindex(di::DimIndices{<:Any,N}, i::Int) where N
rebuild(d, axes(d, 1)[i])
end
end

Base.getindex(di::DimIndices) = throw(BoundsError(di, ()))

"""
DimPoints <: AbstractArray
Expand Down Expand Up @@ -107,6 +108,7 @@ function Base.getindex(dp::DimPoints, i1::Int, i2::Int, I::Int...)
end
Base.getindex(di::DimPoints{<:Any,1}, i::Int) = (dims(di, 1)[i],)
Base.getindex(di::DimPoints, i::Int) = di[Tuple(CartesianIndices(di)[i])...]
Base.getindex(di::DimPoints) = throw(BoundsError(di, ()))

"""
DimKeys <: AbstractArray
Expand Down Expand Up @@ -166,3 +168,4 @@ function Base.getindex(di::DimKeys{<:Any,1}, i::Int)
(rebuild(d, rebuild(di.selectors[1]; val=d[i])),)
end
Base.getindex(di::DimKeys, i::Int) = di[Tuple(CartesianIndices(di)[i])...]
Base.getindex(di::DimIndices) = throw(BoundsError(di, ()))
2 changes: 1 addition & 1 deletion src/stack/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ and 2 layers:
"""
function Base.eachslice(s::AbstractDimStack; dims)
dimtuple = _astuple(dims)
all(hasdim(s, dimtuple...)) || throw(DimensionMismatch("s doesn't have all dimensions $dims"))
all(hasdim(s, dimtuple)) || throw(DimensionMismatch("s doesn't have all dimensions $dims"))
_eachslice(s, dimtuple)
end

Expand Down
10 changes: 9 additions & 1 deletion test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ end
@test_throws DimensionMismatch eachslice(da; dims=(y, ti, Z))
end

@testset "allow slicing with an empty tuple" begin
@test eachslice(da; dims=()) == eachslice(parent(da); dims=())
end

@testset "slice over last dimension" begin
@testset for dims in tis2
da2 = map(mean, eachslice(da; dims=dims)) == DimArray([3.0, 4.0, 5.0, 6.0], ti)
Expand Down Expand Up @@ -375,7 +379,11 @@ end
# Categorical is taken from refdims
dra = rebuild(da; refdims=(Z(Categorical([1], ForwardOrdered(), NoMetadata())),))
drb = rebuild(db; refdims=(Z(Categorical([1], ForwardOrdered(), NoMetadata())),))
@test dims(cat(dra, drb; dims=Z(1:2)), Z) == Z(Categorical(1:2, ForwardOrdered(), NoMetadata()))
@test
dims(
cat(dra, drb; dims=Z(1:2))
, Z)
== Z(Categorical(1:2, ForwardOrdered(), NoMetadata()))
end
end

Expand Down
12 changes: 10 additions & 2 deletions test/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ using DimensionalData: Sampled, Categorical, AutoLookup, NoLookup, Transformed,
Regular, Irregular, Points, Intervals, Start, Center, End,
Metadata, NoMetadata, ForwardOrdered, ReverseOrdered, Unordered, layers

A = [1.0 2.0 3.0;
4.0 5.0 6.0]
A = [1.0 2.0 3.0; 4.0 5.0 6.0]
x, y, z = X([:a, :b]), Y(10.0:10.0:30.0; metadata=Dict()), Z()
dimz = x, y
da1 = DimArray(A, (x, y); name=:one, metadata=Metadata())
da2 = DimArray(Float32.(2A), (x, y); name=:two)
da3 = DimArray(Int.(3A), (x, y); name=:three)
da4 = DimArray(cat(4A, 5A, 6A, 7A; dims=3), (x, y, z); name=:extradim)

cat(rand(X(1:3)), rand(X(5:7)))

s = DimStack((da1, da2, da3))
mixed = DimStack(da1, da2, da4)
mixed[]

@testset "constructors" begin
@test DimStack((one=A, two=2A, three=3A), dimz) == s
Expand Down Expand Up @@ -168,6 +170,12 @@ end
@test_throws DimensionMismatch eachslice(mixed; dims=Dim{:x})
end

@testset "allow slicing with an empty tuple" begin
@test
DimensionalData.dims2indices(mixed, ())
eachslice(mixed; dims=())
end

@testset "slice over X dimension" begin
@testset for dims in xs2
@test eachslice(mixed; dims=dims) isa Base.Generator
Expand Down

0 comments on commit ac10c2c

Please sign in to comment.