-
Notifications
You must be signed in to change notification settings - Fork 37
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
Formalize interface for keyword arguments in indexing #83
Conversation
Are you waiting for a comment from oxinabox, or is this ready? |
I'd like to see what @oxinabox , @nickrobinson251, or @mcabbott think so that I don't step on any toes by using some of the ideas generated in NamedDims.jl. |
@timholy, you were part of the original discussion surrounding |
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.
From a quick skim it seems pretty reasonable to me, but I can't pretend to have given it a careful read. Just a couple of comments.
Users should only define these methods for new types like this so it should probably be represented in the documentation similarly.
@chriselrod did you want me to start incorporating traits from #61 into methods like |
Hi @Tokazama, I'd like to ask, if the interface also allows to interface |
It'd probably be better for a separate PR, focusing this one on keyword args. Good to merge this? Everyone's fine with the |
BTW. These were copied directly from @phlavenk, this is very much suited to work with something like AxisArrays.jl. You're comment motivated me to clean up some of my code in AxisIndices.jl (which has a lot of comments and mysteriously named inner functions right now). So here are some minimal examples of what could get this working with some of the stuff you mentioned. (Disclaimer: consider this pseudo code b/c I haven't actually tested it for AxisArrays specifically) ArrayInterface.dimnames(::Type{T}) where {T<:AxisArray} = AxisArrays.axisnames(T)
ArrayInterface.has_dimnames(::Type{T}) where {T<:AxisArray} = true
function ArrayInterface.to_index(::IndexStyle, axis::Axis, arg::Interval)
return findall(in(arg), axis.val)
end
Base.getindex(x::AxisArray, args...) = ArrayInterface.getindex(x, args...)
Base.getindex(x::AxisArray; kwargs...) = ArrayInterface.getindex(x; kwargs...)
function ArrayInterface.unsafe_get_element(x::AxisArray, inds)
return @inbounds(parent(x)[inds...])
end You can take advantage of this currently with |
What's the status on this? |
I made one more change to conform with changes to strides. There's a fallback for function contiguous_axis(::Type{T}) where {T}
if parent_type(T) <: T
return nothing
else
return contiguous_axis(parent_type(T))
end
end If that's fine then this should be ready. |
Yeah, I think that's fine. E.g., to add support for Alternatively, we could add an "inherits from parent" trait to make this optional. That'd be a bit safer, and only slightly less convenient. |
It would be nice if this were more formally addressed so there isn't any ambiguity about how traits function with arrays. I'd be happy to help out with that discussion/PR. |
Currently there is no standard meaning for keyword arguments when passed to
getindex
. Often keyword arguments are propagated optional arguments for some method along the call chain. However, the most popular use of keyword arguments for indexing (as far as I'm aware) is that implemented by NamedDims.jl where dimension names are the keywords. As discussed in this issue, this is problematic if the wrapper around a named dimension array alters the dimensions of an array (e.g.,SubArray
) because the indices associated with the keyword arguments passed on to subsequent calls don't necessary have the same meaning once the wrapper is removed (e.g.,getindex(x; kwargs...) = getindex(parent(x); kwargs...)
) .In this PR I added a minimal interface for incorporating named dimensions in the indexing pipeline. This provides generic support for what is arguably the most widely used application of keyword arguments for array indexing. This still propagates kwargs when the array doesn't have any dimension names. Since there currently isn't generic propagation of kwargs for indexing this shouldn't break anything.
Somewhat orthogonal to the issue of formalizing the keywords component of the indexing interface, this also standardizes named dimensions. It would be really nice if users didn't have to bother with figuring out if the model they used returns a
NamedDims.NamedDimsArray
,DimensionalData.AbstractDimArray
, orAxisArrays.AxisArray
when using NamedDims.jl, DimensionalData.jl, or AxisArrays.jl when indexing.Some of the code is from NamedDims.jl but a lot of it still needs to be benchmarked and tested. I figured I'd wait to do that until I knew if this was a desirable addition and if people agreed on the interface.
@oxinabox , you may be interested in this.