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

Empty arrays as components #116

Open
william-macready opened this issue Jan 18, 2022 · 3 comments
Open

Empty arrays as components #116

william-macready opened this issue Jan 18, 2022 · 3 comments

Comments

@william-macready
Copy link

I'd like to be able to use ComponentArrays with some fields containing empty data arrays. For example,
ComponentVector(a=Int[], b=1) and ComponentVector(a=[Int[]], b=1) both work fine. However,

ComponentVector(a=[(a1=Int[],)], b=1)

fails due to a zero step-size in a range used in the partition function in utils.jl

Is there a simple fix?

@william-macready
Copy link
Author

Perhaps the issue is more subtle than I naively thought: ca = ComponentVector(a=[Int[],Int[]], b=1) instantiates an object but ca.a and ca.b both error.

@jonniedie
Copy link
Owner

jonniedie commented Jan 19, 2022

Thanks for opening the issue, @william-macready. It looks like there are actually a few problems here, some more easily fixable than others:

  1. Components that are arrays of ComponentArrays that have only empty inner elements (e.g. a in ComponentVector(a=[(a1=Int[],)], b=1)) do not handle the zero-element case very gracefully. The method of partitioning the data for the output doesn't work when the chunk size is zero. I don't anticipate this being very hard to deal with, it will probably just have to special case for that situation.
  2. Components that are arrays of arrays (e.g. a in ComponentVector(a=[[3], [4, 5]], b=1)) are are collecting the elements of the arrays into the underlying array correctly, but messing up on the indices. For example, this silently gives the wrong answer:
julia> ca = ComponentVector(a=[[3], [4, 5]], b=1)
4-element ComponentVector{Int64} with axis Axis(a = 1:2, b = 3):
 3
 4
 5
 1

julia> ca.b
5

julia> ca.a
2-element view(::Vector{Int64}, 1:2) with eltype Int64:
 3
 4

The issue here is that I never really designed array-valued components to hold plain arrays. They can hold values or they can hold ComponentArrays (all of the same type, of course), but not plain arrays. The empty arrays in ComponentVector(a=[Int[],Int[]], b=1) are a bit of a red herring here because the issue is more with the incorrect indexing than the fact that the arrays are empty.

Fixing this may need a new AbstractAxis type. I'll have to think about how that all might work out. In the very least, though, it should error for now instead of silently giving the wrong answer.

@william-macready
Copy link
Author

Thank you for the explanations Jonnie.

  1. If you do fix this chunking issue I'm happy to test things for you on my use case. I hope the needed changes are straightforward and are largely localized to partition.
  2. Thanks for the warning, I'll structure things in my code to avoid nested arrays.

Thanks for a very useful package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants