Class: Numo::Gnuplot::ImageData

Inherits:
PlotData
  • Object
show all
Defined in:
lib/numo/gnuplot.rb

Direct Known Subclasses

RgbAlphaData, RgbImageData

Instance Method Summary collapse

Methods inherited from PlotData

array_shape, #data_format, #line_str

Constructor Details

#initialize(data) ⇒ ImageData

Returns a new instance of ImageData.



944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/numo/gnuplot.rb', line 944

def initialize(data)
  @text = false
  if data.respond_to?(:shape)
    @data = data
    check_shape
    if defined? Numo::NArray
      @format =
        case @data
        when Numo::DFloat; "%float64"
        when Numo::SFloat; "%float32"
        when Numo::Int8;   "%int8"
        when Numo::UInt8;  "%uint8"
        when Numo::Int16;  "%int16"
        when Numo::UInt16; "%uint16"
        when Numo::Int32;  "%int32"
        when Numo::UInt32; "%uint32"
        when Numo::Int64;  "%int64"
        when Numo::UInt64; "%uint64"
        else
          raise ArgumentError,"not supported NArray type"
        end
    end
  elsif data.kind_of? Array
    n = nil
    @data = []
    data.each do |a|
      @data.concat(a)
      m = a.size
      if n && n != m
        raise IndexError,"element size differs (%d should be %d)"%[m, n]
      end
      n = m
    end
    @shape = [data.size,n]
    @format = "%float64"
  else
    raise ArgumentError,"argument should be data array"
  end
end

Instance Method Details

#check_shapeObject

:nodoc: all



937
938
939
940
941
942
# File 'lib/numo/gnuplot.rb', line 937

def check_shape
  if @data.shape.size != 2
    raise IndexError,"array should be 2-dimensional"
  end
  @shape = @data.shape
end

#cmd_strObject



984
985
986
987
988
989
990
# File 'lib/numo/gnuplot.rb', line 984

def cmd_str
  if @text
    "'-' matrix"
  else
    "'-' binary array=(#{@shape[1]},#{@shape[0]}) format='#{@format}'"
  end
end

#data_strObject



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/numo/gnuplot.rb', line 992

def data_str
  if @text
    f = data_format
    s = ""
    @data.to_a.each do |b|
      s << b.map{|e| f%e}.join(" ")+"\n"
    end
    s+"\ne"
  elsif defined? Numo::NArray
    if @data.kind_of?(Numo::NArray)
      @data.to_string
    else
      Numo::DFloat.cast(@data).to_string
    end
  else
    @data.pack("d*")
  end
end