-
Notifications
You must be signed in to change notification settings - Fork 4
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
Accessing "coordinates" or enumerating elements #33
Comments
The closest thing I have right now is this. julia> axes_keys(AxisArray(reshape(1:12, (3,4)), 0.1:0.1:0.3, 0.01:0.01:0.04))
(0.1:0.1:0.3, 0.01:0.01:0.04)
But you'd still need to use It almost seems like this is going for an |
I came across this again with a further case: For a |
Here's my solution. Even ignoring the lack of generality, I'm 100% sure there's a cleaner way to do this. I'm only posting this here as a hack for the next time I run into this. EDIT: Is ordering of nt_coord(naa::NamedAxisArray{X}, tup::Tuple) where X = NamedTuple{X}(tup)
macro ifsomething(ex)
quote
result = $(esc(ex))
result === nothing && return nothing
result
end
end
struct EnumerateNAA{NAA,II}
naa::NAA
inner_iter::II
EnumerateNAA(naa::NAA, ii::II) where {NAA,II} = new{NAA,II}(naa, ii)
end
EnumerateNAA(naa) = EnumerateNAA(naa, zip(Iterators.product(axes_keys(naa)...), naa))
Base.length(e::EnumerateNAA) = length(e.naa)
function enumerate_nt(naa::NamedAxisArray{X}) where X
EnumerateNAA(naa)
end
function Base.iterate(enaa::EnumerateNAA, state)
((tup, val), state) = @ifsomething iterate(enaa.inner_iter, state)
nt = nt_coord(enaa.naa, tup)
return ((nt, val), state)
end
function Base.iterate(enaa::EnumerateNAA)
((tup, val), state) = @ifsomething iterate(enaa.inner_iter)
nt = nt_coord(enaa.naa, tup)
return ((nt, val), state)
end |
dimension names will always be in the same order as the axes. |
I find myself frequently extracting the "coordinates" of points via
Is there a simpler way to do this? It'd also be nice to have a way to dispatch
pairs
to output these coordinates rather than the linear indices, perhaps via returning something different fromIndexStyle(A)
.(I'm happy to implement this if you think it'd be useful and can tell me the interface you'd like)
The text was updated successfully, but these errors were encountered: