Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Simple chains throw errors when displayed #447

Closed
kaandocal opened this issue Jan 31, 2024 · 4 comments
Closed

Simple chains throw errors when displayed #447

kaandocal opened this issue Jan 31, 2024 · 4 comments

Comments

@kaandocal
Copy link

I'm not sure if this has been noted before, but creating a very simple chain or attempting to slice a chain in interactive mode (REPL, notebook) etc. fails:

using MCMCChains

Chains(zeros(1, 3, 1), [:a, :b, :c])  

results in:

Chains MCMC chain (1×3×1 Array{Float64, 3}):                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                     
Iterations        = 1:1:1                                                                                                                                                                                                                                                            
Number of chains  = 1                                                                                                                                                                                                                                                                
Samples per chain = 1                                                                                                                                                                                                                                                                
parameters        = a, b, c                                                                                                                                                                                                                                                          
┌ Warning: MCSE calculation failed: ArgumentError("number of draws after splitting must >4 but is 0.")                                                                                                                                                                               
└ @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:310                                                                                                                                                                                                                   
┌ Warning: Tail ESS calculation failed: ArgumentError("number of draws after splitting must >4 but is 0.")                                                                                                                                                                           
└ @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:319                                                                        
┌ Warning: Bulk ESS/R-hat calculation failed: ArgumentError("number of draws after splitting must >4 but is 0.")                          
└ @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:333 
Error showing value of type Chains{Float64, AxisArrays.AxisArray{Float64, 3, Array{Float64, 3}, Tuple{AxisArrays.Axis{:iter, StepRange{Int64, Int64}}, AxisArrays.Axis{:var, Vector{Symbol}}, AxisArrays.Axis{:chain, UnitRange{Int64}}}}, Missing, @NamedTuple{parameters::Vector{Symbol}}, @NamedTuple{}}:
ERROR: ArgumentError: collection must be non-empty
Stacktrace:
  [1] first(itr::@NamedTuple{})
    @ Base ./abstractarray.jl:470
  [2] ChainDataFrame(name::String, nt::@NamedTuple{})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/summarize.jl:8
  [3] summarystats(chains::Chains{…}; sections::Symbol, append_chains::Bool, autocov_method::AutocovMethod, maxlag::Int64, name::String, kwargs::@Kwargs{…})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:338
  [4] summarystats
    @ ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:286 [inlined]
  [5] describe(io::Base.TTY, chains::Chains{…}; q::Vector{…}, etype::Symbol, kwargs::@Kwargs{})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:191
  [6] describe(io::Base.TTY, chains::Chains{Float64, AxisArrays.AxisArray{…}, Missing, @NamedTuple{…}, @NamedTuple{}})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:184
  [7] describe(c::Chains{Float64, AxisArrays.AxisArray{…}, Missing, @NamedTuple{…}, @NamedTuple{}}; args::@Kwargs{})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:174
  [8] describe
    @ ~/.julia/packages/MCMCChains/AQENn/src/stats.jl:174 [inlined]
  [9] show(io::IOContext{…}, mime::MIME{…}, chains::Chains{…})
    @ MCMCChains ~/.julia/packages/MCMCChains/AQENn/src/chains.jl:352
 [10] (::REPL.var"#55#56"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:273
 [11] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [12] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:259
 [13] display
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:278 [inlined]
 [14] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [15] #invokelatest#2
    @ Base ./essentials.jl:887 [inlined]
 [16] invokelatest
    @ Base ./essentials.jl:884 [inlined]
 [17] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{…})
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:315
 [18] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:284
 [19] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [20] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:282
 [21] (::REPL.var"#do_respond#80"{…})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:911
 [22] #invokelatest#2
    @ Base ./essentials.jl:887 [inlined]
 [23] invokelatest
    @ Base ./essentials.jl:884 [inlined]
 [24] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2656
 [25] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1312
 [26] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:386
Some type information was truncated. Use `show(err)` to see complete types.

This seems to be caused by the display routine since the following works:

using MCMCChains

Chains(zeros(1, 3, 1), [:a, :b, :c]).value.data
@sethaxen
Copy link
Member

sethaxen commented Feb 1, 2024

The issue here seems to be that if ess, rhat, and mcse all fail, then nt_additional is empty, and this line tries to create an empty ChainDataFrame, which is not allowed.

additional_df = ChainDataFrame("Additional", (; ((k, nt_additional[k]) for k in additional_keys if k keys(nt_additional))...))

A simple fix would be to allow an empty ChainDataFrame. However, that will be replaced by #431.

@devmotion
Copy link
Member

There are a few other (slightly different and already fixed) issues regarding problems with describe (and hence display) if chains are too short. But I haven't been aware of this particular problem.

@sethaxen
Copy link
Member

MCMCDiagnosticTools v0.3.10 contains a fix for this: TuringLang/MCMCDiagnosticTools.jl#119

@yebai
Copy link
Member

yebai commented May 23, 2024

It seems fixed on the most recent release.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants