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, #as_array, #data_format, #line_str, quote_data

Constructor Details

#initialize(data) ⇒ ImageData

Returns a new instance of ImageData.


970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'lib/numo/gnuplot.rb', line 970

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


963
964
965
966
967
968
# File 'lib/numo/gnuplot.rb', line 963

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

#cmd_strObject


1010
1011
1012
1013
1014
1015
1016
# File 'lib/numo/gnuplot.rb', line 1010

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

#data_strObject


1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/numo/gnuplot.rb', line 1018

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 && @data.kind_of?(Numo::NArray)
    @data.to_string
  elsif @data.kind_of?(Array)
    @data.pack("d*")
  elsif @data.repond_to?(:to_a)
    @data.to_a.pack("d*")
  else
    raise TypeError,"invalid data type: #{@data.class}"
  end
end