-
Notifications
You must be signed in to change notification settings - Fork 48
Extending Base.stack for DimArrays #645
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
base: main
Are you sure you want to change the base?
Changes from all commits
4ea5e40
ec3b2cf
0b762ce
6affd9b
ac3affc
83ace60
e6cfb1b
ce16068
bdf151f
7a4cf26
3314852
0ac2eb0
884ab68
c369d06
2c7b8b9
cf57f86
0a278a6
f41f424
d9ee5e7
9b3b3b7
25dc8a2
e2aeb0e
39c78e8
16c205a
15cc9fd
52ce34a
f868ddf
911acda
9517d16
b329519
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,7 @@ Base methods | |
|
||
```@docs | ||
Base.cat | ||
Base.stack | ||
Base.copy! | ||
Base.eachslice | ||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -802,6 +802,47 @@ end | |
end | ||
end | ||
|
||
@testset "Base.stack" begin | ||
a = [1 2 3; 4 5 6] | ||
da = DimArray(a, (X(4.0:5.0), Y(6.0:8.0))) | ||
b = [7 8 9; 10 11 12] | ||
ca = DimArray(b, (X(4.0:5.0), Y(6.0:8.0))) | ||
db = DimArray(b, (X(6.0:7.0), Y(6.0:8.0))) | ||
|
||
x = DimArray([da, db], (Z(4.0:5.0))) | ||
@test_warn "Lookup values" stack(x) | ||
|
||
x = DimArray(fill(da, 2, 2), (Dim{:a}(4.0:5.0), Dim{:b}(6.0:7.0))) | ||
sx = stack(x) | ||
@test sx isa AbstractDimArray | ||
@test dims(sx) == (dims(first(x))..., dims(x)...) | ||
|
||
@test_throws "ArgumentError" stack(x; dims=4) | ||
|
||
x = DimArray([da, ca], (Dim{:a}(1:2),)) | ||
sx = stack(x; dims=1) | ||
sy = @test_nowarn stack(x; dims=:) | ||
sz = @test_nowarn stack(x; dims=X) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, _maybe_dimnum(x, dim) = hasdim(x, dim) ? dimnum(x, dim) : ndims(x) + 1 Should we throw an error instead, like when an out-of-range Integer dim is give? I.e.: function _maybe_dimnum(x, dim::Int)
if dim < ndims(x) + 2
return dim
else
throw(ArgumentError(LazyString("cannot stack slices ndims(x) = ", ndims(x) + 1, " along dims = ", dim)))
end
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking we should add a dimension like it is currently, but we can set it to be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the current minimal approach, stacking with a new dimension a = [1 2 3; 4 5 6]
da = DimArray(a, (X(4.0:5.0), Y(6.0:8.0)))
b = [7 8 9; 10 11 12]
db = DimArray(b, (X(4.0:5.0), Y(6.0:8.0)))
x = DimArray([da, db], (Dim{:a}(1:2),))
stack(set(x, :a=>Z)) # Or
set(stack(x), :a=>Z}) We could have this behavior be automatic (without overloading function Base.stack(iter::AbstractArray{<:AbstractDimArray}; dims=:)
x = Base._stack(_maybe_dimnum(first(iter), dims), check_stack_dims(iter))
if !hasdim(x, dims) && Z isa Union{Dimension,Type{<:Dimension}}
x = set(x, DimensionalData.dims(x)[end] => dims)
end
return x
end But I wonder if it could be confusing to have this different behavior for when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure we are understanding each other... My only ask here was that this new dimension is a |
||
@test sx == sz | ||
@test sx == stack([parent(da), parent(ca)], dims=1) | ||
@test sx isa AbstractDimArray | ||
@test dims(sx) == (dims(x)..., dims(first(x))...) | ||
|
||
for d = 1:3 | ||
x = DimArray([da, ca], (AnonDim(),)) | ||
dc = stack(x, dims=d) | ||
@test dims(dc, d) isa AnonDim | ||
@test parent(dc) == stack([da, db], dims=d) | ||
|
||
dc = stack(x -> x .^ 2, x; dims=d) | ||
@test dims(dc, d) isa AnonDim | ||
@test dc == stack(parent(x); dims=d) .^ 2 | ||
dc = stack(+, x, x, x; dims=d) | ||
@test dims(dc, d) isa AnonDim | ||
@test dc == stack(x + x + x; dims=d) | ||
end | ||
end | ||
|
||
@testset "unique" begin | ||
a = [1 1 6; 1 1 6] | ||
xs = (X, X(), :X) | ||
|
Uh oh!
There was an error while loading. Please reload this page.