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

Preserve metadata on getindex operation when scalarizing #1010

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

contradict
Copy link

No description provided.

if isnothing(metadata(arr))
scalarized
else
metadata(scalarized, metadata(arr))
Copy link
Member

Choose a reason for hiding this comment

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

@shashi @YingboMa should the metadata be the same, or should it know that it's derived from indexing this object somehow?

Copy link
Member

Choose a reason for hiding this comment

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

It should not be the same... And I think this should not be added... I would say this is a usecase where tagged-data like API becomes useful. You should be able to define something like:

@metadata getindex(::MetadataType, idx) = compute_output_metadata_here

And it should be handled for every key which registers in this way.

I'd be interested in adding this general feature, but I'm looking for more substantial examples where this is super useful.

Until then, I think you should just try to propagate the metadata outside of the package's behavior (by walking the tree once constructed). The method in this PR seems not correct to me.

@contradict
Copy link
Author

The present behavior seems surprising to me. Is the correct resolution to document that scalarize does not preserve metadata or to create an issue to record this and come back to it when a more complete solution can be designed?

@ChrisRackauckas
Copy link
Member

I don't think the approach is generally wrong, it's just that A shouldn't preserve the same exact metadata as A[i]. Something should happen when indexing since it's not the same variable.

@contradict
Copy link
Author

The new thing that happens here is not the indexing, this was an indexing operation coming in and is still one on return (possibly even exactly the same one if there were no array operations in the variable or indexing arguments). The new thing is that scalarize has been applied to the variable and to the indexing arguments. In the other branches, metadata is preserved with no modifications to indicate scalarization, what is special about scalarization of indexing?

@ChrisRackauckas
Copy link
Member

What if you just add to the metadata the index?

@contradict
Copy link
Author

In the test case I have the index is just an integer. What is the right thing to wrap that in so I can attach metadata?

@ChrisRackauckas
Copy link
Member

@shashi have a preference?

@shashi
Copy link
Member

shashi commented Nov 21, 2023

We already do something like this for @variables x(t)[1:20] I believe, the hook

@wrapped function getindex_posthook(f, x::AbstractArray)

So if you just do getindex_posthook(f, x) then f will be called on every indexing operation on x and the arguments will be, the result of the getindex operation, and the indices passed in. At this point you can choose to attach some sub-metadata.

And this function

function recurse_and_apply(f, x)
will make it sticky: it will make it work on the subarrays as well. But I recommend writing your own because that's untested and dead code. 🗡️

@shashi
Copy link
Member

shashi commented Nov 21, 2023

In general, it would be nice to have something that makes sense for all purposes and is extensible. I'm also ok with attaching the index to the metadata, you could just create a new type called MetadataView(metadata_item, index), but metadata is required to be a ImmutableDict, so you'd have to create a view for each key that's already in the array's metadata. Maybe it makes sense to define a generic metadata_view(::MetadataKeyType, array, idx...) which providers of MetadataKeyType can overload to get the immediate behavior they want. In all other cases, it could just fallback to constructing MetadataView

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

Successfully merging this pull request may close these issues.

None yet

3 participants