Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Fix *cat inconsistencies #169

wants to merge 7 commits into from

Conversation

nrontsis
Copy link
Contributor

@nrontsis nrontsis commented Oct 23, 2022

This PR addresses #113 and #168 by making cats support arbitrarily many arguments, and making *cat(args...) equivalent to reduce(*cat, args...).

Tests were extended accordingly.

idxmap_x, idxmap_y = indexmap.((ax_x, ax_y))
return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...)))
end
function Base._typed_hcat(::Type{T}, inputs::Base.AbstractVecOrTuple{ComponentArray}) where {T}
Copy link
Contributor Author

@nrontsis nrontsis Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hcat and vcat call _typed_h/vcat under the hood.

Unfortunately, implementing the (seemingly private) _typed_hcat for ComponentArrays is apparently necessary for e.g.

reduce(hcat, [cv1, cv2])

to be identical to hcat(cv1, cv2), as the above calls directly _typed_hcat.

Since unit tests are in place for the above behaviour, I thought the above is okay-ish?

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 || isempty(intersect(keys.(axes_to_merge)...)))
if no_duplicate_keys && length(Set(rest_axes)) == 1
Copy link
Contributor Author

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.

@nrontsis nrontsis marked this pull request as ready for review October 23, 2022 17:10
This PR aims to avoid issues like:
```
using ComponentArrays
A = ComponentMatrix(ones(2, 2), Axis(:a, :b), FlatAxis())
A[:b, :] # works
A'[:, :b] # fails!
```
by wrapping adjoint operations in the underlying data of the ComponentArray structure.

By not having to care about adjoints of ComponentArray, we arguably also reduce overall cognitive load.
@nrontsis
Copy link
Contributor Author

@jonniedie I fixed the tests here as well, after incorporating changes from #170. So perhaps first review #170.

@@ -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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 cat calls reindex to all axis, as compared to only the first or the second one that the old hcats and vcats were calling before.

@test vtempca isa ComponentVector
@test vtempca.r == temp.r
@test vtempca.c == ca.c
@test length(vtempca) == length(temp) + length(ca)
@test [ca; ca; ca] isa Vector
@test vcat(ca, 100) isa Vector
@test [ca' ca']' isa Vector
@test [ca' ca']' isa Adjoint{T, Matrix{T}} where T
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was incorrect in my opinion, as in it was inconsistent to what happens with normal arrays.

@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2022

Codecov Report

Merging #169 (5f92105) into master (cbb24ef) will increase coverage by 2.30%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #169      +/-   ##
==========================================
+ Coverage   73.49%   75.79%   +2.30%     
==========================================
  Files          20       20              
  Lines         679      599      -80     
==========================================
- Hits          499      454      -45     
+ Misses        180      145      -35     
Impacted Files Coverage Δ
src/broadcasting.jl 100.00% <ø> (ø)
src/componentarray.jl 81.30% <ø> (-0.59%) ⬇️
src/show.jl 3.84% <ø> (ø)
src/array_interface.jl 86.07% <100.00%> (+2.91%) ⬆️
src/axis.jl 84.41% <100.00%> (-1.30%) ⬇️
src/compat/gpuarrays.jl 72.91% <100.00%> (+20.03%) ⬆️
src/linear_algebra.jl 95.83% <100.00%> (-0.33%) ⬇️
src/similar_convert_copy.jl 75.00% <0.00%> (-10.00%) ⬇️
src/utils.jl 93.93% <0.00%> (-3.04%) ⬇️
... and 2 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment