Skip to content

Commit

Permalink
fix cat where dims holds a lookup array (#501)
Browse files Browse the repository at this point in the history
* fix cat where dims holds a lookup array

* bugfix cat
  • Loading branch information
rafaqz authored Jul 26, 2023
1 parent baa72b2 commit dae3b8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/array/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,21 @@ function _cat(catdims::Tuple, A1::AbstractDimArray, As::AbstractDimArray...)
if all(x -> hasdim(x, catdim), Xin)
# We concatenate an existing dimension
newcatdim = if lookup(A1, catdim) isa NoLookup
# Colon will be converted to array axis in `format`
rebuild(catdim; val=:)
rebuild(catdim, NoLookup())
else
# vcat the index for the catdim in each of Xin
reduce(vcat, map(x -> dims(x, catdim), Xin))
end
else
# Concatenate new dims
if all(map(x -> hasdim(refdims(x), catdim), Xin))
# vcat the refdims
reduce(vcat, map(x -> refdims(x, catdim), Xin))
if catdim isa Dimension && val(catdim) isa AbstractArray && !(lookup(catdim) isa NoLookup{AutoIndex})
# Combine the refdims properties with the passed in catdim
set(refdims(first(Xin), catdim), catdim)
else
# vcat the refdims
reduce(vcat, map(x -> refdims(x, catdim), Xin))
end
else
# Use the catdim as the new dimension
catdim
Expand Down
8 changes: 8 additions & 0 deletions test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ end
@test all(map(==, index(dx), index(DimensionalData.format((X([4.0, 5.0, 6.0, 7.0]), Y(6:8), Ti(1:2)), dx))))
@test_throws ErrorException vcat(da, reverse(db; dims=X))
@test_throws ErrorException vcat(db, da)
@testset "lookup array in dims" begin
@test dims(cat(da, db; dims=Ti(1:2)), Ti) == Ti(Sampled(1:2, ForwardOrdered(), Regular(1), Points(), NoMetadata()))
@test dims(cat(da, db; dims=Ti(Categorical(1:2))), Ti) == Ti(Categorical(1:2, ForwardOrdered(), NoMetadata()))
# 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()))
end
end

# https://github.com/rafaqz/DimensionalData.jl/issues/451
Expand Down

0 comments on commit dae3b8f

Please sign in to comment.