ruby-numo / numo-gnuplot

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

allows to "End of animation sequence" message #11

Closed icm7216 closed 7 years ago

icm7216 commented 7 years ago

This patch comes to be able to continue in the "End of animation sequence" message when creating animation gif.

icm7216 commented 7 years ago

このパッチはアニメーション gif 作成時のエラーを解決します。

Numo::GnuplotError: 
End of animation sequence
C:/Ruby231/lib/ruby/gems/2.3.0/gems/numo-gnuplot-0.2.1/lib/numo/gnuplot.rb:303:in `run'
icm7216 commented 7 years ago

これで jupyter notebook で lifegame が動くようになりました。

require 'numo/narray'
require 'numo/gnuplot'
require "base64"

class LifeGame

  def initialize(nx,ny,m)
    @data = Numo::UInt8.zeros(ny,nx)
    @data[m..ny-1-m,m..nx-1-m] = Numo::UInt8.new(ny-2*m,nx-2*m).rand(2)
    @step = 0
  end

  def update
    b = Numo::UInt8.zeros(*@data.shape)
    b[1..-2,1..-2] =
      @data[0..-3,0..-3] + @data[0..-3,1..-2] + @data[0..-3,2..-1] +
      @data[1..-2,0..-3] + @data[1..-2,2..-1] +
      @data[2..-1,0..-3] + @data[2..-1,1..-2] + @data[2..-1,2..-1]
    @data.store((b.eq 3) | ((b.eq 2) & Numo::Bit.cast(@data)))
    @step += 1
  end

  attr_reader :data,:step
end

nx,ny = 90, 60
life = LifeGame.new(nx,ny,1)
filename = "lifegame.gif" 

Numo.gnuplot do
  reset
  set output: filename
  set term: "gif", animate:true, delay:10, size:[600,400]
  set :nokey
  set size: {ratio:1.0*ny/nx}
  set xrange: -1..nx
  set yrange: -1..ny
  unset :colorbox
  set palette_defined:'(0 "white", 1 "green")'

  101.times do |i|
    life.update if i > 0
    set title:"lifegame step=#{i}"
    plot life.data, :flipy, with:"image"
  end
  set :output
end

img = Base64.strict_encode64(File.binread(filename))
IRuby.html %Q(<img src="data:image/gif;base64,#{img}" />)

lifegame