JuliaDataCubes / YAXArrays.jl

Yet Another XArray-like Julia package
https://juliadatacubes.github.io/YAXArrays.jl/
Other
103 stars 18 forks source link

yaxconvert alters the dim names #362

Open bjarthur opened 10 months ago

bjarthur commented 10 months ago

why the change from D1, D2 here to Dim{:D1}, Dim{:D2} when converting a dimarray to a yaxarray?

julia> using DimensionalData, YAXArrays, YAXArrayBase

julia> DimensionalData.@dim D1

julia> DimensionalData.@dim D2

julia> dd = rand(D1(["a","b"]), D2(["x","y"]), metadata = Dict{String, Any}())
2×2 DimArray{Float64,2} with dimensions: 
  D1 Categorical{String} String["a", "b"] ForwardOrdered,
  D2 Categorical{String} String["x", "y"] ForwardOrdered
        "x"       "y"
  "a"  0.790863  0.345696
  "b"  0.490964  0.814311

julia> ddyax = yaxconvert(YAXArray, dd)
2×2 YAXArray{Float64,2} with dimensions: 
  Dim{:D1} Categorical{String} String["a", "b"] ForwardOrdered,
  Dim{:D2} Categorical{String} String["x", "y"] ForwardOrdered
Total size: 32.0 bytes

same thing happens in the other direction, yax to dd:

julia> yax = YAXArray((D1(["a","b"]), D2(["x","y"])), rand(2,2))
2×2 YAXArray{Float64,2} with dimensions: 
  D1 Categorical{String} String["a", "b"] ForwardOrdered,
  D2 Categorical{String} String["x", "y"] ForwardOrdered
Total size: 32.0 bytes

julia> yaxdd = yaxconvert(DimArray, yax)
2×2 DimArray{Float64,2} with dimensions: 
  Dim{:D1} Categorical{String} String["a", "b"] ForwardOrdered,
  Dim{:D2} Categorical{String} String["x", "y"] ForwardOrdered
        "x"       "y"
  "a"  0.27363   0.455353
  "b"  0.446381  0.709265

there is a workaround, but why doesn't yaxconvert operate like this?

julia> hack = DimArray(yax.data, dims(yax))
2×2 DimArray{Float64,2} with dimensions: 
  D1 Categorical{String} String["a", "b"] ForwardOrdered,
  D2 Categorical{String} String["x", "y"] ForwardOrdered
        "x"       "y"
  "a"  0.27363   0.455353
  "b"  0.446381  0.709265

i ask because indexing with D1 is more succinct and easier to read and type than Dim{:D1}.

lazarusA commented 10 months ago

most likely is because currently there is not a @dim definition as in DimensionalData.@dim D1 . I agree that is annoying, suggestions [PRs] are welcome.

felixcremer commented 10 months ago

The construction of the DimArray or Yaxarray in yaxcreate in YAXArrayBase is using the Dim Constructor explicitely. There might be a smarter way to get the Dimension from a dimension name.