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

[Nuisance issue] Using Gnuplot prints correct graph, also throws error #321

Closed
Xasin opened this issue Jun 26, 2022 · 8 comments
Closed

Comments

@Xasin
Copy link

Xasin commented Jun 26, 2022

Dear IRuby maintainer team,

I ran into a minor issue (annoyance, does not seem to actually impact functionality) with using Gnuplot.

The issue:
Gnuplot will correctly render the graph and insert output below the cell.
However, IRuby also throws an error below it. The kernel continues execution as normal and the graph remains, the only issue is the fact that the error message clutters output.

Reproduction:
Using the minimal gnuplot example as given in the example notebook:

require 'gnuplot'

Gnuplot::Plot.new() do |plot|
  
    plot.title  "Array Plot Example"
    plot.xlabel "x"
    plot.ylabel "x^2"
    
    x = (0..50).collect { |v| v.to_f }
    y = x.collect { |v| v ** 2 }

    plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
      ds.with = "linespoints"
      ds.notitle
    end
  end

Output:
image

Execution continues as normal after this.

Versions:

  • ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]
  • Jupyter:
IPython          : 8.4.0
ipykernel        : 6.15.0
ipywidgets       : not installed
jupyter_client   : 7.3.4
jupyter_core     : 4.10.0
jupyter_server   : 1.18.0
jupyterlab       : 3.4.3
nbclient         : 0.6.4
nbconvert        : 6.5.0
nbformat         : 5.4.0
notebook         : 6.4.12
qtconsole        : not installed
traitlets        : 5.3.0
  • gnuplot gem (2.6.2)
  • gnuplot apt package 5.4.1+dfsg1-1+deb11u1
  • iruby (0.7.4)

No further output in the console running Jupyter was seen.
As such, this is mostly a nuisance issue, but can be confusing, as it is expected that the given minimal examples work without throwing errors.

Note that the error message IS correct.
Calling to_hash returns a key-value pair array.
This might be a subtle version incompatibility between IRuby and the gnuplot gem that I am using. Perhaps a small check to convert the returned array ((which really should be a hash, why to_hash returns a key-value pair array is a mystery to me)) to a hash would already fix this.

The returned array looks like this:
[[:set, "title", "\"Array Plot Example\""], [:set, "xlabel", "\"x\""], [:set, "ylabel", "\"x^2\""], [:set, "to_hash", ""]]

@kojix2
Copy link
Member

kojix2 commented Jun 26, 2022

I tried with Ruby 3.1 but could not reproduce the relevant error message.

Lab
image

Notebook
image

  • ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
Selected Jupyter core packages...
IPython          : 8.3.0
ipykernel        : 6.9.1
ipywidgets       : not installed
jupyter_client   : 7.2.2
jupyter_core     : 4.10.0
jupyter_server   : 1.17.1
jupyterlab       : 3.3.2
nbclient         : 0.5.13
nbconvert        : 6.4.4
nbformat         : 5.3.0
notebook         : 6.4.11
qtconsole        : not installed
traitlets        : 5.1.1

@Xasin
Copy link
Author

Xasin commented Jun 26, 2022

I can confirm, the problem no longer exists when I use rvm to switch to Ruby3.0.0
Interestingly enough, the output of to_hash for the Gnuplot call is still an array. The exact same version of the gnuplot library and iruby library are used.

Perhaps there is a difference in either error type handling (i.e. the error is caught in Ruby 3.0 but not 2.7), or some form of implicit conversion that turns it into a hash properly?

Either way, using RVM to switch to Ruby 3.0 is acceptable for my use case, but it would be nice to either suppress these warnings for lower versions or add a short note to the documentation saying that there is a small issue with the lower ruby version.

@kojix2
Copy link
Member

kojix2 commented Jun 26, 2022

Ok, thanks. I did confirm that the error occurs in 2.7.6.

@kojix2
Copy link
Member

kojix2 commented Jun 26, 2022

I see an error when a Gnuplot class object is passed to "*args".

kernel.rb

      events.trigger(:post_run_cell, result) unless silent # result is_a Gnuplot object

event_manager.rb

    def trigger(event, *args, **kwargs)
      check_available_event(event)
      @callbacks[event].each do |fn|
        fn.call(*args, **kwargs)
      end
    end

@kojix2
Copy link
Member

kojix2 commented Jun 26, 2022

Maybe if I change it like this it will work. I'm not sure if this is enough.

    def trigger(*args, **kwargs)
      event = args.shift
      check_available_event(event)
      @callbacks[event].each do |fn|
        fn.call(*args, **kwargs)
      end
    end

@kojix2
Copy link
Member

kojix2 commented Jun 29, 2022

No, the above change does not solve the problem.
Gnuplot::Plot uses method_missing and reacts to to_h and to_hash. This seems to be causing the problem.

kojix2 added a commit to kojix2/iruby that referenced this issue Jun 29, 2022
@kojix2 kojix2 closed this as completed in 41beabd Jul 5, 2022
@kojix2
Copy link
Member

kojix2 commented Jul 6, 2022

Fixed.
However, I recommend using numo-gnuplot.

https://github.com/ruby-numo/numo-gnuplot

@Xasin
Copy link
Author

Xasin commented Jul 6, 2022

Thank you kindly for the fix!
That just makes the behaviour a bit more consistent. Also, I appreciate the mention of Numo-Gnuplot

I have used Numo-narray before, and should look at the other tools that they offer.

Have a lovely day ^^

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

No branches or pull requests

2 participants