-
Notifications
You must be signed in to change notification settings - Fork 28
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
Map function to slices with index labels #158
Comments
That's a useful piece of functionality indeed, aside from the fact that |
I don't think I'm smart enough to build it myself. Hence the request. I previously requested similar functionality though based on the DataFramesMeta @rtransform macro. See my last post in #154 In both requests the idea is to select a dimension or dimensions to slice across, then have access to both the underlying data and index labels both within the slice and orthogonal to the slice. |
That's
Each of
This part requires an extra step, but is still reasonably possible: julia> using AxisKeysExtra, RectiGrids
julia> map(with_axiskeys(eachslice(K, dims=(:I,:X)))) do (keys, slice)
(;total=sum(slice), keys)
end
2×2 StructArray(::Matrix{Int64}, ::Matrix{@NamedTuple{I::Symbol, X::Symbol}}) with eltype @NamedTuple{total::Int64, keys::@NamedTuple{I::Symbol, X::Symbol}}:
(total = 3, keys = (I = :i, X = :x)) (total = 11, keys = (I = :i, X = :y))
(total = 7, keys = (I = :j, X = :x)) (total = 15, keys = (I = :j, X = :y)) |
Genius thank you :)
|
I got your another question in this thread as an email from github, and even though the message is no longer here – see the solution below:
Julia has julia> using DataPipes
# ... all definitions from above...
julia> function my_func2( ( (;I,X), slice)::Pair{<:NamedTuple,<:KeyedArray} )
KeyedArray( I .* [slice(:a),slice(:b)] , New=[:n1,:n2] )
end
julia> @p K |> eachSliceKeys(dims=(:I,:X)) |> my_func2.() |> stack
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓ New ∈ 3-element Vector{Symbol}
→ I ∈ 2-element Vector{Symbol}
◪ X ∈ 2-element Vector{Symbol}
And data, 3×2×2 Array{Int64, 3}:
[:, :, 1] ~ (:, :, :x):
(:i) (:j)
(:n1) 1 3
(:n2) 2 4
(:n3) 4 8
[:, :, 2] ~ (:, :, :y):
(:i) (:j)
(:n1) 5 7
(:n2) 6 8
(:n3) 12 16 |
Thanks:) |
Is there any shorter way to do this ?
Something like |
Well, IMO it's not that long, and is easily built out of composable blocks. Note that you don't even need to define the @p let
X
eachslice(dims=:a)
filter(!all(ismissing, _))
stack
end A potential alternative I've been thinking about is using Accessors
@modify(X |> eachslice(_, dims=:a)) do slices
filter(!allMissing, slices)
end This isn't implemented for now, but is possible. The idea of |
Ability to map a function to slices of a KeyedArray including the dimension index names of each slice.
Proposed syntax below passes the slice and index names as a named tuple.
The text was updated successfully, but these errors were encountered: