gcalderone / Gnuplot.jl

Julia interface to gnuplot
Other
119 stars 16 forks source link

when pass session name as a variable, it breaks (as of v1.6.something) #63

Closed ahbarnett closed 9 months ago

ahbarnett commented 9 months ago

Not exactly sure when this broke, but I used to rely on it in v1.5 something:

@gp :fig1 rand(10) "w l"                   # works fine
s = :fig1
@gp s rand(10) "w l"                   # breaks
@gp session_names()[1] rand(10) "w l"     # also breaks.

The error is:

ERROR: Unexpected argument with type Symbol
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] parseSpecs(::Symbol, ::Vararg{Any}; default_mid::Int64, is3d::Bool, kws::@Kwargs{})
   @ Gnuplot ~/.julia/packages/Gnuplot/PdysS/src/plotspecs.jl:200
 [3] top-level scope
   @ REPL[17]:1

Thanks for a useful package! Best, Alex

gcalderone commented 9 months ago

It broke in v1.6. The problem is that the session name needs to be a literal Symbol, while variables of type Symbol are no longer accepted. This is why @gp :fig1 ... works while @gp s ... raises an error. The advantage is that a @gp invocation now involves exactly one session.
Sorry for this breaking change, but I had to rationalize a bit the syntax...

If you still wish to use a Symbol in a variable you can copy/paste the code generated by the @gp macro, e.g.:

@macroexpand @gp :fig1 rand(10) "w l"
quote
    local gp = Gnuplot.getsession(:fig1)
    Gnuplot.reset(gp)
    Gnuplot.append!(gp, Gnuplot.parseSpecs(rand(10), "w l", default_mid = Gnuplot.last_added_mid(gp), is3d = false))
    Gnuplot.options.gpviewer && gpexec.(Ref(gp), Gnuplot.collect_commands(gp))
    gp
end

and replace :fig1 with a variable containing a session name such as s from your example, or session_names()[1]

ahbarnett commented 9 months ago

I don't know metaprogramming, but I don't see why you couldn't allow a Symbol as well as a QuoteNode in the logic here:

https://github.com/gcalderone/Gnuplot.jl/blob/461322708eccce5ec955be5486a06faf9dcdbb15/src/Gnuplot.jl#L437

That would recover the rather useful behavior from v1.5. Otherwise unpacking the macro is awkward.

Here's me checking the symbol or quotenode

julia> macro t(args...)
       println(args[1]," ",isa(args[1],Symbol)," ",isa(args[1],QuoteNode))
       end
@t (macro with 1 method)

julia> @t :fig1
:fig1 false true

julia> s= :fig1; @t s
s true false

Since the command Gnuplot.getsession(s) works on symbols as well as quotenodes, this should be a 1-line change (the line above). Thanks, Alex

gcalderone commented 9 months ago

Because isa(args[1],Symbol) is a check on the name of the variable, not its content. If you set s to an int or a string you would obtain exactly the same results:

s= :fig1; @t s
s true false

julia> s= 1; @t s
s true false

julia> s= "ddd"; @t s
s true false
gcalderone commented 9 months ago

An alternative approach is to change the name of the default session being addressed when no one is specified, e.g.:

s = :fig1; Gnuplot.options.default = s; @gp rand(10) "w l"
ahbarnett commented 9 months ago

Ok, great - this last option is very easy and natural. Thanks for a great package. It would be great if you could add it to your Advanced examples webpage under Sessions.

On Fri, Jan 12, 2024 at 4:05 AM gcalderone @.***> wrote:

An alternative approach is to change the name of the default session being addressed when no one is specified, e.g.:

s = :fig1; Gnuplot.options.default = s; @gp rand(10) "w l"

— Reply to this email directly, view it on GitHub https://github.com/gcalderone/Gnuplot.jl/issues/63#issuecomment-1888698739, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACNZRSSUFGJ677OAFSVHJ3DYOD4EZAVCNFSM6AAAAABBW5SJB6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBYGY4TQNZTHE . You are receiving this because you authored the thread.Message ID: @.***>

-- *-------------------------------------------------------------------~^`^~._.~' |\ Alex Barnett Center for Computational Mathematics, Flatiron Institute | \ http://users.flatironinstitute.org/~ahb 646-876-5942

gcalderone commented 9 months ago

I'm working on new examples, and I will also add this. Thank you for pointing it out!