rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
282 stars 42 forks source link

Better error message when you do `At(X(...))` instead of `X(At(...))` #848

Open asinghvi17 opened 1 week ago

asinghvi17 commented 1 week ago

This is pretty inscrutable, but I'm not sure where the best place to intercept this is.


julia> dem[At(X(1)), At(Y(1))]
ERROR: MethodError: no method matching isless(::X{Int64}, ::Float64)
The function `isless` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  isless(::Missing, ::Any)
   @ Base missing.jl:87
  isless(::Any, ::Missing)
   @ Base missing.jl:88
  isless(::Rasters.Edge, ::Real)
   @ Rasters ~/.julia/packages/Rasters/0qvMe/src/methods/burning/edges.jl:31
  ...

Stacktrace:
  [1] <(x::X{Int64}, y::Float64)
    @ Base ./operators.jl:353
  [2] lt(o::Base.Order.Lt{typeof(<)}, a::X{Int64}, b::Float64)
    @ Base.Order ./ordering.jl:121
  [3] searchsortedlast
    @ ./sort.jl:204 [inlined]
  [4] searchsortedlast
    @ ./sort.jl:301 [inlined]
  [5] searchsortedlast
    @ ./sort.jl:303 [inlined]
  [6] #searchsortedlast#4
    @ ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/lookup_arrays.jl:59 [inlined]
  [7] searchsortedlast
    @ ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/lookup_arrays.jl:58 [inlined]
  [8] at(::DimensionalData.Dimensions.Lookups.ForwardOrdered, ::DimensionalData.Dimensions.Lookups.Regular{…}, lookup::Projected{…}, selval::X{…}, atol::Nothing, rtol::Nothing; err::DimensionalData.Dimensions.Lookups._True)
    @ DimensionalData.Dimensions.Lookups ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:215
  [9] at(::DimensionalData.Dimensions.Lookups.ForwardOrdered, ::DimensionalData.Dimensions.Lookups.Regular{…}, lookup::Projected{…}, selval::X{…}, atol::Nothing, rtol::Nothing)
    @ DimensionalData.Dimensions.Lookups ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:210
 [10] at(lookup::Projected{…}, sel::At{…}; kw::@Kwargs{})
    @ DimensionalData.Dimensions.Lookups ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:182
 [11] at(lookup::Projected{…}, sel::At{…})
    @ DimensionalData.Dimensions.Lookups ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:181
 [12] selectindices(l::Projected{…}, sel::At{…})
    @ Rasters ~/.julia/packages/Rasters/0qvMe/src/lookup.jl:98
 [13] (::DimensionalData.Dimensions.Lookups.var"#110#111")(l::Projected{…}, s::At{…})
    @ DimensionalData.Dimensions.Lookups ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:1099
 [14] map
    @ ./tuple.jl:383 [inlined]
 [15] selectindices
    @ ~/.julia/packages/DimensionalData/hhCBb/src/Lookups/selector.jl:1098 [inlined]
 [16] dims2indices
    @ ~/.julia/packages/DimensionalData/hhCBb/src/Dimensions/indexing.jl:43 [inlined]
 [17] dims2indices
    @ ~/.julia/packages/DimensionalData/hhCBb/src/Dimensions/indexing.jl:32 [inlined]
 [18] getindex(::Raster{…}, ::At{…}, ::At{…})
    @ DimensionalData ~/.julia/packages/DimensionalData/hhCBb/src/array/indexing.jl:69
 [19] top-level scope
    @ REPL[30]:1
Some type information was truncated. Use `show(err)` to see complete types.
rafaqz commented 1 week ago

Ok yes At totally lets you use a dimension as val

We could make that error as it will always be a mistake?

asinghvi17 commented 1 week ago

Yeah that would be good, maybe in the constructor then?

felixcremer commented 1 week ago

Yes I think that would make sense. I played around a bit and am wondering why this is not throwing an error, but returning the full array:

julia> dd = DimArray(1:3, X([X,Y,Z ]))
╭─────────────────────────────╮
│ 3-element DimArray{Int64,1} │
├─────────────────────────────┴────────────────────────────────────────── dims ┐
  ↓ X Sampled{UnionAll} [X, Y, Z] Unordered Irregular Points
└──────────────────────────────────────────────────────────────────────────────┘
 X  1
 Y  2
 Z  3

julia> dd[X=Y]
╭─────────────────────────────╮
│ 3-element DimArray{Int64,1} │
├─────────────────────────────┴────────────────────────────────────────── dims ┐
  ↓ X Sampled{UnionAll} [X, Y, Z] Unordered Irregular Points
└──────────────────────────────────────────────────────────────────────────────┘
 X  1
 Y  2
 Z  3