Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow printing of arbitrary Particles #145

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

gdalle
Copy link
Contributor

@gdalle gdalle commented Aug 8, 2024

At the moment, printing errors for particles that don't have a number- or array-like type (which allows pstd):

julia> using MonteCarloMeasurements

julia> Particles(["a", "b"])
Error showing value of type Particles{String, 2}:
ERROR: MethodError: no method matching /(::String, ::Int64)

Closest candidates are:
  /(::BigFloat, ::Union{Int16, Int32, Int64, Int8})
   @ Base mpfr.jl:494
  /(::Missing, ::Number)
   @ Base missing.jl:123
  /(::BigInt, ::Union{Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8})
   @ Base gmp.jl:565
  ...

Stacktrace:
  [1] _mean(f::typeof(identity), A::Vector{String}, dims::Colon)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:186
  [2] mean(A::Vector{String}; dims::Function)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:174
  [3] _var(A::Vector{String}, corrected::Bool, mean::Nothing, ::Colon)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:389
  [4] var(A::Vector{String}; corrected::Bool, mean::Nothing, dims::Function)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:378
  [5] _std(A::Vector{String}, corrected::Bool, mean::Nothing, ::Colon)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:464
  [6] std(A::Vector{String}; corrected::Bool, mean::Nothing, dims::Function)
    @ Statistics ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Statistics/src/Statistics.jl:459
  [7] pstd(::Particles{String, 2}; kwargs::@Kwargs{})
    @ MonteCarloMeasurements ~/.julia/packages/MonteCarloMeasurements/dQJTj/src/particles.jl:310
  [8] pstd(::Particles{String, 2})
    @ MonteCarloMeasurements ~/.julia/packages/MonteCarloMeasurements/dQJTj/src/particles.jl:308
  [9] to_num_str(p::Particles{String, 2}, d::Int64, ds::Int64)
    @ MonteCarloMeasurements ~/.julia/packages/MonteCarloMeasurements/dQJTj/src/particles.jl:105
 [10] show(io::IOContext{Base.TTY}, ::MIME{Symbol("text/plain")}, p::Particles{String, 2})
    @ MonteCarloMeasurements ~/.julia/packages/MonteCarloMeasurements/dQJTj/src/particles.jl:0
 [11] (::OhMyREPL.var"#15#16"{REPL.REPLDisplay{}, MIME{}, Base.RefValue{}})(io::IOContext{Base.TTY})
    @ OhMyREPL ~/.julia/packages/OhMyREPL/gPPKm/src/output_prompt_overwrite.jl:23
 [12] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [13] display
    @ ~/.julia/packages/OhMyREPL/gPPKm/src/output_prompt_overwrite.jl:6 [inlined]
 [14] display
    @ ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:278 [inlined]
 [15] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [16] #invokelatest#2
    @ ./essentials.jl:892 [inlined]
 [17] invokelatest
    @ ./essentials.jl:889 [inlined]
 [18] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{…})
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:315
 [19] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:284
 [20] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [21] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:282
 [22] (::REPL.var"#do_respond#80"{})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:911
 [23] #invokelatest#2
    @ ./essentials.jl:892 [inlined]
 [24] invokelatest
    @ ./essentials.jl:889 [inlined]
 [25] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2656
 [26] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1312
 [27] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ~/.julia/juliaup/julia-1.10.4+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 PR allows a nicer behavior:

julia> using MonteCarloMeasurements

julia> Particles(["a", "b"])
 Particles{String, 2}

@baggepinnen baggepinnen merged commit d0ea922 into baggepinnen:master Aug 8, 2024
3 checks passed
@baggepinnen
Copy link
Owner

Thanks for the PR! Out of curiosity, what kind of use case do you have that calls for Particles{String}? :)

@gdalle
Copy link
Contributor Author

gdalle commented Aug 8, 2024

I'm not sure yet but I'm working on a code where I have multiple samples of things that are not numbers (e.g. functions), and a Particle seemed like the correct struct to use semantically

@baggepinnen
Copy link
Owner

Note that Particles <: Real, and method overloads are only really provided for functions that expect some form of Number, so you might have uphill battle making this usecase work out :/

@gdalle
Copy link
Contributor Author

gdalle commented Aug 8, 2024

Oh right I had missed that! In that case I think my PR is not great cause it also assumes Particles can contain arrays. Feel free to revert it.
Basically I'm not using your package much at all, I just thought it was the right abstraction

@gdalle gdalle deleted the gd/printing branch August 12, 2024 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants