ruby-numo / numo-gnuplot

Gnuplot wrapper for Ruby/Numo
BSD 3-Clause "New" or "Revised" License
51 stars 8 forks source link

A workaround for endless waiting with 5.4 series #25

Closed sm514a closed 1 year ago

sm514a commented 1 year ago

Althogh there is no issue with gnuplot 5.2.8, with 5.4.x my script end up with endless waiting without output. This is a workaround in order to avoid this endless waiting. My environmen is windows gnuplot and cygwin ruby.

For reference, here is error output when I enter cntrl-C.

      1: from /usr/share/gems/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:367:in `send_cmd' 
/usr/share/gems/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:367:in `gets': Interrupt

This is a debug information I have with @debug = true.

<print GPVAL_VERSION
>5.4
<set term pngcairo transparent font "meiryo, 16" size 1280,1600 noenhanced
>
>gnuplot> rint '_end_of_cmd_'
>               ^
>         line 0: invalid command
>
icm7216 commented 1 year ago

Thank you for finding this issue. I have a similar problem. I tried your patch but unfortunately it did not solve my problem.

For now, I think using version 5.2.8 of gnuplot for numo-gnuplot is a good choice. I will wait to use the gnuplot 5.4 series until it is stable.

My environment:


Below are the results of my trial.

To isolate the problem, I tested gnuplot and numo-gnuplot separately.

From gnuplot/Bugs I found some problems with the gnuplot 5.4 series. stdin problems in the Windows environment have been reported.

I have created a gnuplot script and a data file for testing. Each script differs in the way it directs data to the plot command.

file name content
simple_plot.plt Simple command
plot_from_data_file.plt load data file
plot_from_data_block.plt Use named data blocks
plot_from_inline_data.plt Use inline data
matrix.dat data file

simple_plot.plt

set title "simple plot"
plot sin(x)

plot_from_data_file.plt

set title "plot from data file"
plot 'matrix.dat' matrix with image

plot_from_data_block.plt

set title "plot from data block"
$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD
plot '$map1' matrix with image

plot_from_inline_data.plt

set title "plot from inline data"
plot '-' matrix with image
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
e

matrix.dat

5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3

Load and run scripts

Load the script from gnuplot. Both gnuplot 5.2.8 and 5.4.5 can be plotted successfully.

>gnuplot -p simple_plot.plt

>gnuplot -p plot_from_data_file.plt

>gnuplot -p plot_from_data_block.plt

>gnuplot -p plot_from_inline_data.plt

Run scripts via pipe

In this test, the script is piped into gnuplot's stdin.

As a result of testing, the only scripts that plot successfully with both gnuplot 5.2.8 and 5.4.5 are simple_plot.plt and plot_from_data_file.plt.

The results are not the same for plot_from_data_block.plt and plot_from_inline_data.plt, which is a form of embedding data in the gnuplot command stream. gnuplot 5.2.8 can successfully plot, but gnuplot 5.4.5 has an error.

Result of gnuplot 5.4.5

>type simple_plot.plt | gnuplot -persist

>type plot_from_data_file.plt | gnuplot -persist

>type plot_from_data_block.plt | gnuplot -persist

gnuplot> 5 plot '$map1' matrix with image
         ^
         line 6: invalid command

>type plot_from_inline_data.plt | gnuplot -persist
         line 2: Matrix does not represent a grid

gnuplot> 5 0 0 0 1 0
         ^
         line 2: invalid command

gnuplot> 0 0 0 2 3
         ^
         line 2: invalid command

gnuplot> 0 1 2 4 3
         ^
         line 2: invalid command

gnuplot> e
         ^
         line 2: invalid command

Test with ruby numo-gnuplot

For this test, the above gnuplot script is run from numo-gnuplot.

plot_test.rb

require "numo/gnuplot"

files = %w[
simple_plot.plt
plot_from_data_file.plt
plot_from_data_block.plt
plot_from_inline_data.plt
]

Numo.gnuplot do
  debug_on
  files.each do |file|
    fp = File.open(file, "r")
    lines = fp.readlines.join
    fp.close

    puts "file name: #{file}"
    puts "script:\n---\n#{lines}---\n\n"

    begin
      run(lines)
    rescue => e
      puts e.full_message
    end

    puts "  => Press Enter to continue"
    gets
  end
  debug_off
end 

Similar to the above results, the only scripts that successfully plot in both gnuplot 5.2.8 and 5.4.5 are simple_plot.plt and plot_from_data_file.plt.

plot_from_data_block.plt and plot_from_inline_data.plt, which embed data in gnuplot's command stream, gnuplot 5.2.8 plotted successfully, but gnuplot 5.4.5 gave an error.

Result of numo-gnuplot

>ruby plot_test.rb
file name: simple_plot.plt
script:
---
set title "simple plot"
plot sin(x)
---

<set title "simple plot"
plot sin(x)
  => Press Enter to continue

file name: plot_from_data_file.plt
script:
---
set title "plot from data file"
plot 'matrix.dat' matrix with image
---

<set title "plot from data file"
plot 'matrix.dat' matrix with image
  => Press Enter to continue

file name: plot_from_data_block.plt
script:
---
set title "plot from data block"
$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD
plot '$map1' matrix with image
---

<set title "plot from data block"
$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD
plot '$map1' matrix with image
>
>gnuplot> 5 plot '$map1' matrix with image
>         ^
>         line 6: invalid command
>
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:307:in `run':  (Numo::GnuplotError)
gnuplot> 5 plot '$map1' matrix with image
         ^
         line 6: invalid command
        from plot_test.rb:21:in `block (2 levels) in <main>'
        from plot_test.rb:12:in `each'
        from plot_test.rb:12:in `block in <main>'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:5:in `instance_eval'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:5:in `gnuplot'
        from plot_test.rb:10:in `<main>'
  => Press Enter to continue

file name: plot_from_inline_data.plt
script:
---
set title "plot from inline data"
plot '-' matrix with image
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
e
---

<set title "plot from inline data"
plot '-' matrix with image
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
e
>         line 8: Matrix does not represent a grid
>
>
>gnuplot> 5 0 0 0 1 0
>         ^
>         line 8: invalid command
>
>
>gnuplot> 0 0 0 2 3
>         ^
>         line 8: invalid command
>
>
>gnuplot> 0 1 2 4 3
>         ^
>         line 8: invalid command
>
>
>gnuplot> e
>         ^
>         line 8: invalid command
>
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:307:in `run':  (Numo::GnuplotError)
line 8: Matrix does not represent a grid

gnuplot> 5 0 0 0 1 0
         ^
         line 8: invalid command
 :
        from plot_test.rb:21:in `block (2 levels) in <main>'
        from plot_test.rb:12:in `each'
        from plot_test.rb:12:in `block in <main>'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:5:in `instance_eval'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/numo-gnuplot-0.2.4/lib/numo/gnuplot.rb:5:in `gnuplot'
        from plot_test.rb:10:in `<main>'
  => Press Enter to continue

From these results, I think that the cause of this issue is likely to be a problem in the internal processing of inline data and datablock in gnuplot 5.4.5.

sorry for the long post

sm514a commented 1 year ago

@icm7216

Thank you for your helpful comment and thorough investigation.

It is unfortunate this doesn't work in your environment. I guess by putthing extra line feed somewhere else may solve, but it may not be appropriate for this elegant libray.

If this problem is caused by gnuplot's bug and they are working to fix it, we can wait for a while for their new release as you suggested.

icm7216 commented 1 year ago

Thanks @sm514a

I found latest gnuplot 5.5.0 development version binary for Windows in gnuplot download. So I have tried it and the results are good.

I hope this fix will be included in the next release of gnuplot 5.4.x. Please try it!

download gp550-20221028-win64-mingw.zip and extract the archive.

> curl http://tmacchant33.starfree.jp/gnuplot_files/gp550-20221028-win64-mingw.zip -o gp550.zip
> powershell Expand-Archive -Path gp550.zip -DestinationPath c:\gp55dev"

Set environment variables before running.

gp55dev_test.rb

gnuplot_path = "c:\\gp55dev\\gnuplot\\bin"
ENV["GNUPLOT"] = gnuplot_path
ENV["PATH"] = [gnuplot_path, ENV["PATH"]].join(";")

system("gnuplot -V")
system("ruby plot_test.rb")

All scripts will work correctly.

>ruby gp55dev_test.rb
gnuplot 5.5 patchlevel 0
file name: simple_plot.plt
script:
---
set title "simple plot"
plot sin(x)
---

<set title "simple plot"
plot sin(x)
  => Press Enter to continue

file name: plot_from_data_file.plt
script:
---
set title "plot from data file"
plot 'matrix.dat' matrix with image
---

<set title "plot from data file"
plot 'matrix.dat' matrix with image
  => Press Enter to continue

file name: plot_from_data_block.plt
script:
---
set title "plot from data block"
$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD
plot '$map1' matrix with image
---

<set title "plot from data block"
$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD
plot '$map1' matrix with image
  => Press Enter to continue

file name: plot_from_inline_data.plt
script:
---
set title "plot from inline data"
plot '-' matrix with image
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
e
---

<set title "plot from inline data"
plot '-' matrix with image
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
e
  => Press Enter to continue
sm514a commented 1 year ago

@icm7216

That is good news! Thank you. I hope we get new release soon and from that we can work with the latest gnuplot.

So, keep this code as simple as possible without adding useless line feed for the new release. I would like to close this request here. Thank you for your support!