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.


941
942
943
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
# File 'lib/numo/gnuplot.rb', line 941

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


934
935
936
937
938
939
# File 'lib/numo/gnuplot.rb', line 934

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

#cmd_strObject


981
982
983
984
985
986
987
# File 'lib/numo/gnuplot.rb', line 981

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

#data_strObject


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

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