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

Examples of mortar() for nd arrays? #414

Open
kescobo opened this issue Aug 7, 2024 · 3 comments
Open

Examples of mortar() for nd arrays? #414

kescobo opened this issue Aug 7, 2024 · 3 comments

Comments

@kescobo
Copy link
Contributor

kescobo commented Aug 7, 2024

The docstring for mortar() shows

mortar(blocks::AbstractArray{R, N}, sizes_1, sizes_2, ..., sizes_N)

But I can't figure out how to use this. Here's what I expected to work:

julia> arr = [[rand(2,3,4)] [rand(2,3,4)]
              [rand(2,3,4)] [rand(2,3,4)]];

julia> typeof(arr)
Matrix{Array{Float64, 3}} (alias for Array{Array{Float64, 3}, 2})

julia> mortar(arr, [2,2], [3,3], ones(Int, 4))
ERROR: MethodError: no method matching mortar(::Matrix{Array{Float64, 3}}, ::Vector{Int64}, ::Vector{Int64}, ::Vector{Int64})

I was expecting this to produce the equivalent of

julia> BlockedArray(rand(4,6,4), [2,2], [3,3], ones(Int, 4))
2×2×4-blocked 4×6×4 BlockedArray{Float64, 3}:
# ...
@dlfivefifty
Copy link
Member

I suspect the docstring ia out of date or just wrong…

since you have an alternative that works is there any reason to want to use mortar? We should probably just delete that docstring.

@kescobo
Copy link
Contributor Author

kescobo commented Aug 12, 2024

you have an alternative that works

The alternative does not work for my use-case, or at least, I don't think so. This is related to #87, where the idea is that I have some images that need to be tiled together with some overlaps, and I want to mortar() together views of the original images, without needing to allocate a new array.

I'm currently getting around this by making a vector of 2D arrays, one for each layer, but it would be nice to be able to have a 3D blocked array instead. If there's a different way to use the BlockedArray() constructor for a group of existing 3D arrays, I'm happy to learn of it - all of my attempts have seemed to concatenate / copy the original arrays

@kescobo
Copy link
Contributor Author

kescobo commented Oct 25, 2024

I just figured this out! It was a problem of not creating the 3D array in the first step - I didn't realize that part was required (nor, actually, how to do that). Here's an example of what I was trying to do before:

julia> m0 = zeros(10, 10, 2); m1 = ones(10, 10, 2);

julia> m0v = @view m0[1:5,1:5,:]; m1v = @view m1[6:10, 6:10,:];

julia> ba = [[[m0v] [m1v]];;;]
1×2×1 Array{SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}, 3}:
[:, :, 1] =
 [0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0;;; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0]  …  [1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0; … ; 1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0;;; 1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0; … ; 1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0]

julia> ma = mortar(ba)
1×2×1-blocked 5×10×2 BlockArray{Float64, 3, Array{SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}, 3}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}}:
[:, :, 1] =
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0

[:, :, 2] =
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0

julia> m1[:,:,2] .= 2; ma
1×2×1-blocked 5×10×2 BlockArray{Float64, 3, Array{SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}, 3}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}}:
[:, :, 1] =
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  1.0

[:, :, 2] =
 0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0  2.0

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

No branches or pull requests

2 participants