meggart / DiskArrays.jl

Other
72 stars 13 forks source link

Indexing with vectors can give the wrong result #138

Closed rafaqz closed 6 months ago

rafaqz commented 7 months ago

MWE:

using DiskArrays
# Define a data structure that can be used for testing
struct _DiskArray{T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N}
    parent::A
    chunksize::NTuple{N,Int}
end
_DiskArray(a; chunksize=size(a)) = _DiskArray(a, chunksize)
DiskArrays.@implement_diskarray _DiskArray
Base.size(a::_DiskArray) = size(a.parent)
DiskArrays.haschunks(::_DiskArray) = DiskArrays.Chunked()
DiskArrays.eachchunk(a::_DiskArray) = DiskArrays.GridChunks(a, a.chunksize)
DiskArrays.readblock!(a::_DiskArray, aout, i::AbstractUnitRange...) = aout .= a.parent[i...]
DiskArrays.writeblock!(a::_DiskArray, v, i::AbstractUnitRange...) = view(a.parent, i...) .= v

data = rand(1:99,10,10,10)
a1 = _DiskArray(data);

a1[[1, 2], [2, 3], :] == data[[1, 2], [2, 3], :]

With results

julia> a1[[1, 2], [2, 3], :] == data[[1, 2], [2, 3], :]
false

julia> a1[[1, 2], [2, 3], :]
2×10×2 Array{Int64, 3}:
[:, :, 1] =
 53   5  52  50  21  68  98  44  40  56
 49  64  66  98  42  57  50  12  15  84

[:, :, 2] =
 21  39  75  92  83  64  90  90  40  79
 45  23  54  91  52  33  44  23  47  78

julia> data[[1, 2], [2, 3], :]
2×2×10 Array{Int64, 3}:
[:, :, 1] =
 53  21
 49  45

[:, :, 2] =
  5  39
 64  23

[:, :, 3] =
 52  75
 66  54

[:, :, 4] =
 50  92
 98  91

[:, :, 5] =
 21  83
 42  52

[:, :, 6] =
 68  64
 57  33

[:, :, 7] =
 98  90
 50  44

[:, :, 8] =
 44  90
 12  23

[:, :, 9] =
 40  40
 15  47

[:, :, 10] =
 56  79
 84  78

See #131 for previous discussion

meggart commented 6 months ago

I just tested the MWE, it is fixed by #146