How to lookup and assign on label-aware AbstractVectors in various packages

sylvaticus
Thu, 19 May 2022 12:57:25 +0000
Data

Sometimes I do not remember the position of dimensions or of the individual indexes in a multi-dimensional array and when performances are not a critical factor I prefer to work on keys rather than position.
I am hence testing a few label-aware AbstractVector structures provided by some packages (namely NamedArrays, AxisArrays, DimensionalData and AxisKeys) but only with the first two “old” packages NamedArrays and AxisArrays I managed to obtain my position-agnostic look-up/assignment.
Is this possible also with the other packages ? Are the critics to the “old” pre-Julia 1.0 packages that are in some threads still actual or these are been redesigned to be effective in the post-Juliav1.0 environment ?

This is the example I am testing:

products = ["harwood roundwood", "softwood roundwood", "hardwood sawn wood", "softwood sawn wood", "pannels"]
regions  = ["Italy","France"]
(np, nr) = length(products), length(regions)

using NamedArrays # ok look-up and assignment
ϵ = NamedArray(
       zeros(np,nr,np,nr),
       (products,regions,products,regions),
       ("out_p", "out_r","in_p","in_r")
       )
ϵ["out_p" => "harwood roundwood", "out_r" => "Italy", "in_p" => "softwood roundwood", "in_r" => "Italy"] # 0.0
ϵ["out_p" => "harwood roundwood", "in_p" => "softwood roundwood", "out_r" => "Italy", "in_r" => "Italy"] = 30 # ok different dim order and auto casting Int to Float
ϵ[1,1,1,2] = 40
ϵ[1,1,2,1] # 30.0
ϵ["out_p" => "harwood roundwood", "in_r" => "France", "out_r" => "Italy", "in_p" => "harwood roundwood"] # 40.0
convert(Array{Float64,4},ϵ)

using AxisArrays # ok lookup and assignment
ϵ2 = AxisArray(zeros(np,nr,np,nr), out_p=products, out_r=regions, in_p = products, in_r=regions)
ϵ2[out_p = "harwood roundwood", out_r = "Italy", in_p = "softwood roundwood", in_r = "Italy"] # 0.0
ϵ2[out_p = "harwood roundwood", in_p = "softwood roundwood", out_r = "Italy", in_r = "Italy"] = 30
ϵ2[1,1,1,2] = 40
ϵ2[1,1,2,1] # 30.0
ϵ2[out_p = "harwood roundwood", in_r = "France", out_r = "Italy", in_p = "harwood roundwood"] # 40.0
convert(Array{Float64,4},ϵ2)

using DimensionalData # can't get even how to lookup
ϵ3 = DimArray(zeros(np,nr,np,nr), (out_p=products, out_r=regions, in_p = products, in_r=regions))
ϵ3[out_p("harwood roundwood"), out_r("Italy"), in_p("softwood roundwood"), in_r("Italy")] # UndefValError
ϵ3[out_p = "harwood roundwood", out_r = "Italy", in_p = "softwood roundwood", in_r = "Italy"] # ArgumentError
convert(Array{Float64,4},ϵ3)

using AxisKeys # ok lookup but not assignment
ϵ4 = KeyedArray(zeros(np,nr,np,nr), out_p=products, out_r=regions, in_p = products, in_r=regions)
ϵ4(out_p = "harwood roundwood", out_r = "Italy", in_p = "softwood roundwood", in_r = "Italy") # 0.0
ϵ4[out_p = "harwood roundwood", out_r = "Italy", in_p = "softwood roundwood", in_r = "Italy"] = 30.0 # MethodError
ϵ4(out_p = "harwood roundwood", out_r = "Italy", in_p = "softwood roundwood", in_r = "Italy") = 30.0 # Syntax error
ϵ4[1,1,1,2] = 40 # ok
convert(Array{Float64,4},ϵ4)

2 posts - 2 participants