JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 42 forks source link

Code in README no longer works #127

Closed tlnagy closed 5 years ago

tlnagy commented 7 years ago

The last example in the readme:

B = AxisArray(randn(100,100,100), :x, :y, :z)
Itotal = sumz = 0.0
for iter in eachindex(B)  # traverses in storage order for cache efficiency
    I = B[iter]  # intensity in a single voxel
    Itotal += I
    sumz += I * iter[axisdim(B, Axis{:z})]  # axisdim "looks up" the z dimension
end
meanz = sumz/Itotal

doesn't work under Julia 0.6. It gives a bounds error:

ERROR: BoundsError
Stacktrace:
 [1] getindex(::Int64, ::Int64) at ./number.jl:38
 [2] macro expansion at ./REPL[4]:4 [inlined]
 [3] anonymous at ./<missing>:?

I believe this is due to eachindex returning a single value instead of a tuple. This is likely because it is using fast linear indexing instead of Cartesian indexing. What would the updated version of this code look like? @timholy

tlnagy commented 7 years ago

Thoughts @mbauman?

timholy commented 7 years ago

Good catch!

That example should be changed to for iter in CartesianRange(indices(B)). eachindex is deliberately not guaranteed to return a CartesianIndex.

tlnagy commented 6 years ago

The original eachindex code had the following comment: # traverses in storage order for cache efficiency is that true for the CartesianRange code as well?

timholy commented 6 years ago

Yes, at least for anything that's not a PermutedDimsArray.