JuliaDataCubes / YAXArrays.jl

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

mapslices fails on a 2D cube #252

Closed Balinus closed 1 year ago

Balinus commented 1 year ago

Hello!

I stumbled upon a strange bug. I have a 2D Cube

YAXArray with the following dimensions
longitude           Axis with 321 Elements from -82.0 to -50.0
latitude            Axis with 201 Elements from 62.0 to 42.0
Total size: 504.07 KB

For plotting purpose, I have the following function that estimate min-max for specified dimensions:

function estimate_clims(ds; dims=["longitude", "latitude"])

    clims = (mapslices(minimum ∘ skipmissing, ds, dims=dims).data[1],
        mapslices(maximum ∘ skipmissing, ds, dims=dims).data[1])
end

Strangely, the function worked over the last days but now I get an error for this new Cube (above).

MethodError: defalg(::Vector{Union{}}) is ambiguous. Candidates:
  defalg(v::AbstractArray{<:Union{Missing, Number}}) in Base.Sort at sort.jl:658
  defalg(::AbstractArray{<:Union{Missing, InlineStrings.String1, InlineStrings.String15, InlineStrings.String3, InlineStrings.String7}}) in InlineStrings at /gpfs/home/dl2594/.julia/packages/InlineStrings/rlLZO/src/InlineStrings.jl:942
Possible fix, define
  defalg(::AbstractArray{<:Missing})

Stacktrace:
  [1] sort!(v::Vector{Union{}})
    @ Base.Sort ./sort.jl:711
  [2] sort(v::Vector{Union{}}; kws::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base.Sort ./sort.jl:770
  [3] sort(v::Vector{Union{}})
    @ Base.Sort ./sort.jl:770
  [4] YAXArrays.YAXTools.PickAxisArray(parent::Array{Union{Missing, Float64}, 0}, indmask::Tuple{}, perm::Nothing)
    @ YAXArrays.YAXTools ~/.julia/packages/YAXArrays/au5n4/src/YAXTools.jl:21
  [5] YAXArrays.YAXTools.PickAxisArray(parent::Array{Union{Missing, Float64}, 0}, indmask::Tuple{})
    @ YAXArrays.YAXTools ~/.julia/packages/YAXArrays/au5n4/src/YAXTools.jl:18
  [6] (::YAXArrays.DAT.var"#120#125")(oc::YAXArrays.DAT.OutputCube, cache::Array{Union{Missing, Float64}, 0})
    @ YAXArrays.DAT ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:763
  [7] map
    @ ./tuple.jl:246 [inlined]
  [8] getallargs(dc::YAXArrays.DAT.DATConfig{1, 1})
    @ YAXArrays.DAT ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:761
  [9] runLoop(dc::YAXArrays.DAT.DATConfig{1, 1}, showprog::Bool)
    @ YAXArrays.DAT ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:705
 [10] mapCube(::ComposedFunction{typeof(minimum), typeof(skipmissing)}, ::Tuple{YAXArray{Union{Missing, Float64}, 2, SubArray{Union{Missing, Float64}, 2, Array{Union{Missing, Float64}, 3}, Tuple{Int64, Base.OneTo{Int64}, Base.OneTo{Int64}}, false}, Vector{CubeAxis}}}; max_cache::Float64, indims::InDims, outdims::OutDims, inplace::Bool, ispar::Bool, debug::Bool, include_loopvars::Bool, showprog::Bool, irregular_loopranges::Bool, nthreads::Vector{Int64}, loopchunksize::Dict{Any, Any}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ YAXArrays.DAT ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:475
 [11] #mapCube#36
    @ ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:303 [inlined]
 [12] #mapslices#50
    @ ~/.julia/packages/YAXArrays/au5n4/src/DAT/DAT.jl:374 [inlined]
 [13] estimate_clims(ds::YAXArray{Union{Missing, Float64}, 2, SubArray{Union{Missing, Float64}, 2, Array{Union{Missing, Float64}, 3}, Tuple{Int64, Base.OneTo{Int64}, Base.OneTo{Int64}}, false}, Vector{CubeAxis}}; dims::Vector{String})
    @ Main /mnt/automountdir/gpfs/groups/gc086/DL2495/PrevSaisonPhil/Notebooks/src/figures.jl:317
 [14] single_map(ds::YAXArray{Union{Missing, Float64}, 3, SubArray{Union{Missing, Float64}, 3, Array{Union{Missing, Float64}, 3}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}, Base.OneTo{Int64}}, false}, Vector{CubeAxis}}; variable::String, time::Year, xres::Int64, yres::Int64, rel_height::Float64, colormap::Symbol, savefig::String)
    @ Main /mnt/automountdir/gpfs/groups/gc086/DL2495/PrevSaisonPhil/Notebooks/src/figures.jl:184
 [15] top-level scope
    @ In[71]:2
 [16] eval
    @ ./boot.jl:373 [inlined]
 [17] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

If I only specify 1 dimension, it works:

estimate_clims(Clim[time=Year(1981), Mois=12], dims=["longitude"])
(95.53845871194117, 159.04409864128752)

MWE

using Dates
using YAXArrays

axlist = [
    RangeAxis("time", Date("2022-01-01"):Day(1):Date("2022-01-30")),
    RangeAxis("longitude", range(1, 10, length=10)),
    RangeAxis("latitude", range(1, 5, length=15))    
    ]

data = rand(30, 10, 15)
ds = YAXArray(axlist, data)

dims=["longitude", "latitude"]

mapslices(minimum ∘ skipmissing, ds[time=Date(2022,1,1)], dims=dims)
felixcremer commented 1 year ago

On what Julia version are you? This ambiguity should be fixed by Julia PR 43426 which should be in 1.7.

Balinus commented 1 year ago

Thanks, I am using 1.7.2. I can post tomorrow the content of the Project.toml file when I get back at work.

Balinus commented 1 year ago

Here's my environment info

(Notebooks) pkg> st
      Status `.../Project.toml`
  [13f3f980] CairoMakie v0.10.2
  [864edb3b] DataStructures v0.18.13
  [0703355e] DimensionalData v0.24.6
  [fcd2136c] DiskArrayTools v0.1.6
  [db073c08] GeoMakie v0.5.0
  [c27321d9] Glob v1.3.0
  [cd3eb016] HTTP v1.7.4
  [ee78f7c6] Makie v0.19.2
  [e1d29d7a] Missings v1.1.0
  [85f8d34a] NCDatasets v0.12.13
  [30363a11] NetCDF v0.11.7
  [e4faabce] PProf v2.2.2
  [91a5bcdd] Plots v1.38.8
  [438e738f] PyCall v1.95.1
  [a3a2b9e3] Rasters v0.5.2
  [8e980c4a] Shapefile v0.9.1
  [f3b207a7] StatsPlots v0.15.4
  [a759f4b9] TimerOutputs v0.5.22
  [90b8fcef] YAXArrayBase v0.6.1
  [c21b50f5] YAXArrays v0.4.5
  [0a941bbe] Zarr v0.8.0

From those package, the ones that got installed recently are Shapefile.jl, StatsPlots.jl

versioninfo()
Julia Version 1.7.2
Commit bf53498635* (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) Gold 6150 CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake-avx512)
Environment:
  JULIA_NUM_THREADS = 16

The thing I don't understand is that the function was working before and I haven't changed my environment.

Balinus commented 1 year ago

Well, new day, new behaviour I guess 😀 : I do not have the bug today, function is working and my MWE that failed yesterday is working today. I use JupyerLab and perhaps I selected Julia 1.6.2 (although I would be surprised since the packages are not installed on 1.6.2 and I would have had a message telling me to instantiate ?).

Anyway, "Move along, nothing to see here" !