AxisArrays.jl icon indicating copy to clipboard operation
AxisArrays.jl copied to clipboard

Document indexing semantics

Open milktrader opened this issue 9 years ago • 2 comments

I'm curious why the API has this indexing syntax and why it's different from standard Julia array indexing.

julia> A = AxisArray(reshape(1:60, 12, 5), (.1:.1:1.2, .1:.1:.5));

julia>  A[Axis{:col}(2)] == A[:,2]
true

julia>  A[Axis{:col}(2)] === A[:,2]
false

milktrader avatar Feb 20 '15 16:02 milktrader

I'm not sure if it was intentional. There's a difference in how the indexes into the subarray become stored. With Axis, it's Colon, and with direct indexing, it's a UnitRange.

julia> a = AxisArray(reshape([1:24], 12,2), (.1:.1:1.2, [:a,:b]))

julia> dump(a[Axis{:col}(2)])
AxisArrays.AxisArray{Int64,1,SubArray{Int64,1,Array{Int64,2},(Colon,Int64),2},(:row,),(FloatRange{Float64},)} 
  data: SubArray{Int64,1,Array{Int64,2},(Colon,Int64),2} 
    parent: Array(Int64,(12,2)) 12x2 Array{Int64,2}:
  1  13
  2  14
  3  15
  4  16
  5  17
  6  18
  7  19
  8  20
  9  21
 10  22
 11  23
 12  24
    indexes: (Colon,Int64) (Colon(),2)
    dims: (Int64,) (12,)
    first_index: Int64 13
    stride1: Int64 1
  axes: (FloatRange{Float64},) (0.1:0.1:1.2,)

julia> dump(a[:,2])
AxisArrays.AxisArray{Int64,1,SubArray{Int64,1,Array{Int64,2},(UnitRange{Int64},Int64),2},(:row,),(FloatRange{Float64},)} 
  data: SubArray{Int64,1,Array{Int64,2},(UnitRange{Int64},Int64),2} 
    parent: Array(Int64,(12,2)) 12x2 Array{Int64,2}:
  1  13
  2  14
  3  15
  4  16
  5  17
  6  18
  7  19
  8  20
  9  21
 10  22
 11  23
 12  24
    indexes: (UnitRange{Int64},Int64) (1:12,2)
    dims: (Int64,) (12,)
    first_index: Int64 13
    stride1: Int64 1
  axes: (FloatRange{Float64},) (0.1:0.1:1.2,)

tshort avatar Feb 20 '15 17:02 tshort

Oh interesting. I didn't think about that. This should become consistent once the Julia parser lowers colons as Colon() instead of calculating the range (which I believe is the plan).

We definitely need to document the view/copy behaviors, though.

mbauman avatar Feb 20 '15 20:02 mbauman