-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
Fix *cat inconsistencies #169
base: main
Are you sure you want to change the base?
Changes from all commits
c1af7db
0b7a72d
8d8bbb6
de8d8ba
419105f
7bc3561
5f92105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,53 +16,29 @@ ArrayInterfaceCore.indices_do_not_alias(::Type{ComponentArray{T,N,A,Axes}}) wher | |
ArrayInterfaceCore.instances_do_not_alias(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = ArrayInterfaceCore.instances_do_not_alias(A) | ||
|
||
# Cats | ||
# TODO: Make this a little less copy-pastey | ||
function Base.hcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat) | ||
ax_x, ax_y = second_axis.((x,y)) | ||
if reduce((accum, key) -> accum || (key in keys(ax_x)), keys(ax_y); init=false) || getaxes(x)[1] != getaxes(y)[1] | ||
return hcat(getdata(x), getdata(y)) | ||
function Base.cat(inputs::ComponentArray...; dims::Int) | ||
combined_data = cat(getdata.(inputs)...; dims=dims) | ||
axes_to_merge = [(getaxes(i)..., FlatAxis())[dims] for i in inputs] | ||
rest_axes = [getaxes(i)[1:end .!= dims] for i in inputs] | ||
no_duplicate_keys = (length(inputs) == 1 || allunique(vcat(collect.(keys.(axes_to_merge))...))) | ||
if no_duplicate_keys && length(Set(rest_axes)) == 1 | ||
offsets = (0, cumsum(size.(inputs, dims))[1:(end - 1)]...) | ||
merged_axis = Axis(merge(indexmap.(reindex.(axes_to_merge, offsets))...)) | ||
result_axes = (first(rest_axes)[1:(dims - 1)]..., merged_axis, first(rest_axes)[dims:end]...) | ||
return ComponentArray(combined_data, result_axes...) | ||
else | ||
data_x, data_y = getdata.((x, y)) | ||
ax_y = reindex(ax_y, size(x,2)) | ||
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y)) | ||
axs = getaxes(x) | ||
return ComponentArray(hcat(data_x, data_y), axs[1], Axis((;idxmap_x..., idxmap_y...)), axs[3:end]...) | ||
return combined_data | ||
end | ||
end | ||
|
||
second_axis(ca::AbstractComponentVecOrMat) = getaxes(ca)[2] | ||
second_axis(::ComponentVector) = FlatAxis() | ||
|
||
# Are all these methods necessary? | ||
# TODO: See what we can reduce down to without getting ambiguity errors | ||
Base.vcat(x::ComponentVector, y::AbstractVector) = vcat(getdata(x), y) | ||
Base.vcat(x::AbstractVector, y::ComponentVector) = vcat(x, getdata(y)) | ||
function Base.vcat(x::ComponentVector, y::ComponentVector) | ||
if reduce((accum, key) -> accum || (key in keys(x)), keys(y); init=false) | ||
return vcat(getdata(x), getdata(y)) | ||
else | ||
data_x, data_y = getdata.((x, y)) | ||
ax_x, ax_y = getindex.(getaxes.((x, y)), 1) | ||
ax_y = reindex(ax_y, length(x)) | ||
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y)) | ||
return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...))) | ||
end | ||
Base.hcat(inputs::ComponentArray...) = Base.cat(inputs...; dims=2) | ||
Base.vcat(inputs::ComponentArray...) = Base.cat(inputs...; dims=1) | ||
function Base._typed_hcat(::Type{T}, inputs::Base.AbstractVecOrTuple{ComponentArray}) where {T} | ||
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.
Unfortunately, implementing the (seemingly private)
to be identical to Since unit tests are in place for the above behaviour, I thought the above is okay-ish? |
||
return Base.cat(map(i -> T.(i), inputs)...; dims=2) | ||
end | ||
function Base.vcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat) | ||
ax_x, ax_y = getindex.(getaxes.((x, y)), 1) | ||
if reduce((accum, key) -> accum || (key in keys(ax_x)), keys(ax_y); init=false) || getaxes(x)[2:end] != getaxes(y)[2:end] | ||
return vcat(getdata(x), getdata(y)) | ||
else | ||
data_x, data_y = getdata.((x, y)) | ||
ax_y = reindex(ax_y, size(x,1)) | ||
idxmap_x, idxmap_y = indexmap.((ax_x, ax_y)) | ||
return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...)), getaxes(x)[2:end]...) | ||
end | ||
function Base._typed_vcat(::Type{T}, inputs::Base.AbstractVecOrTuple{ComponentArray}) where {T} | ||
return Base.cat(map(i -> T.(i), inputs)...; dims=1) | ||
end | ||
Base.vcat(x::CV...) where {CV<:AdjOrTransComponentArray} = ComponentArray(reduce(vcat, map(y->getdata(y.parent)', x)), getaxes(x[1])) | ||
Base.vcat(x::ComponentVector, args...) = vcat(getdata(x), getdata.(args)...) | ||
Base.vcat(x::ComponentVector, args::Union{Number, UniformScaling, AbstractVecOrMat}...) = vcat(getdata(x), getdata.(args)...) | ||
Base.vcat(x::ComponentVector, args::Vararg{AbstractVector{T}, N}) where {T,N} = vcat(getdata(x), getdata.(args)...) | ||
|
||
function Base.hvcat(row_lengths::NTuple{N,Int}, xs::AbstractComponentVecOrMat...) where {N} | ||
i = 1 | ||
|
@@ -145,4 +121,4 @@ end | |
Base.stride(x::ComponentArray, k) = stride(getdata(x), k) | ||
Base.stride(x::ComponentArray, k::Int64) = stride(getdata(x), k) | ||
|
||
ArrayInterfaceCore.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A | ||
ArrayInterfaceCore.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,7 +148,7 @@ Base.keys(ax::AbstractAxis) = keys(indexmap(ax)) | |
reindex(i, offset) = i .+ offset | ||
reindex(ax::FlatAxis, _) = ax | ||
reindex(ax::Axis, offset) = Axis(map(x->reindex(x, offset), indexmap(ax))) | ||
reindex(ax::ViewAxis, offset) = ViewAxis(viewindex(ax) .+ offset, indexmap(ax)) | ||
reindex(ax::ViewAxis{Inds,IdxMap,Ax}, offset) where {Inds, IdxMap, Ax} = ViewAxis(viewindex(ax) .+ offset, Ax()) | ||
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. This is a bugfix that was only now discovered by the tests, because now |
||
|
||
# Get AbstractAxis index | ||
@inline Base.getindex(::AbstractAxis, idx) = ComponentIndex(idx) | ||
|
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.
Note the extra check
length(Set(rest_axes)) == 1
that was not in place in the previous implementation.