jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
291 stars 35 forks source link

support extracting sub-axis #132

Closed bgctw closed 2 years ago

bgctw commented 2 years ago

general approach to extract several subcompoennts.

For #129 one can do: cv[Axis(:a,:b)]

jonniedie commented 2 years ago

Hey, thanks for the PR! I actually already have multi-symbol indexing in the newest update of ComponentArrays, though. I decided to go in a different direction than this PR for the following reasons:

  1. Indexing into an array with another array means something specific already. So having something like cv1[cv2] should mean "Index into cv1 at indices defined by the values of cv2" For example:
    
    julia> cv = ComponentVector(a=(a1=100,a2=(a21=210, a22=220)), b=2, c = (c1=reshape(1:4,(2,2)),));

julia> cv[[1, 2, 4]] 3-element Vector{Int64}: 100 210 2

julia> cv[ComponentArray(x=2:3, y=6)] ComponentVector{Int64}(x = [210, 220], y = 2)

Changing this behavior would break from the expected interface of `AbstractArray`s and may break some people's code that depends on this behavior.

2. `Axis(:a, :b)` is a shorthand constructor for `Axis(a=1, b=2)`, which means "An `Axis` where the named `a` component gets index `1` and `b` gets index `2`". So this would be a valid axis for a `ComponentVector` like this:
```julia
ComponentVector(a=200, b=42)

but not this:

ComponentVector(a=200, b=[42, 13])

The correct axis type for the last example would look like

Axis(a=1, b=2:3)
bgctw commented 2 years ago

Thanks for the explanation. I agree that indexing with another ComponentArray should be avoided.

However, the indexing by an Axis could be nice and quite general.

Can the current indexing support my use case #133?