Class: Numo::Gnuplot::PlotData

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

Direct Known Subclasses

ImageData, SPlotRecord

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ PlotData

Returns a new instance of PlotData.



828
829
830
831
832
833
834
835
# File 'lib/numo/gnuplot.rb', line 828

def initialize(*data)
  if data.empty?
    raise ArgumentError,"no data"
  end
  @data = data.map{|a| a.respond_to?(:flatten) ? a.flatten : a}
  @n = @data.map{|a| a.size}.min
  @text = true
end

Class Method Details

.array_shape(a) ⇒ Object



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'lib/numo/gnuplot.rb', line 896

def self.array_shape(a)
  if a.kind_of?(Array)
    is_2d = true
    is_1d = true
    size_min = nil
    a.each do |b|
      if b.kind_of?(Array)
        is_1d = false
        if b.any?{|c| c.kind_of?(Array)}
          is_2d = false
        elsif size_min.nil? || b.size < size_min
          size_min = b.size
        end
      else
        is_2d = false
      end
      break if !is_1d && !is_2d
    end
    if is_1d
      [a.size]
    elsif is_2d
      [a.size, size_min]
    else
      raise GnuplotError, "not suitable Array for data"
    end
  elsif a.respond_to?(:shape)
    a.shape
  elsif PlotItem.is_data(a)
    [a.size]
  else
    raise GnuplotError, "not suitable type for data"
  end
end

.quote_data(v) ⇒ Object



877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/numo/gnuplot.rb', line 877

def self.quote_data(v)
  case v
  when Float,Rational
    DATA_FORMAT % v
  when Numeric
    v.to_s
  else
    s = v.to_s.gsub(/\n/,'\n')
    if /^(e|.*[ \t].*)$/ =~ s
      if /"/ =~ s
        raise GnuplotError,"Datastrings cannot include"+
          " double quotation and space simultaneously"
      end
      s = '"'+s+'"'
    end
    s
  end
end

Instance Method Details

#as_array(a) ⇒ Object



845
846
847
848
849
850
851
852
# File 'lib/numo/gnuplot.rb', line 845

def as_array(a)
  case a
  when Numo::NArray,Array
    a
  else
    a.to_a
  end
end

#cmd_strObject



837
838
839
840
841
842
843
# File 'lib/numo/gnuplot.rb', line 837

def cmd_str
  if @text
    "'-'"
  else
    "'-' binary record=#{@n} format='%float64'"
  end
end

#data_formatObject

:nodoc: all



824
825
826
# File 'lib/numo/gnuplot.rb', line 824

def data_format
  @data_format || DATA_FORMAT
end

#data_strObject



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/numo/gnuplot.rb', line 854

def data_str
  if @text
    s = ""
    @n.times{|i| s << line_str(i)+"\n"}
    s + "e\n"
  elsif defined? Numo::NArray
    m = @data.size
    x = Numo::DFloat.zeros(@n,m)
    m.times{|i| x[true,i] = as_array(@data[i])[0...@n]}
    x.to_string
  else
    s = []
    @n.times{|i| s.concat @data.map{|a| a[i]}}
    s.pack("d*")
  end
end

#line_str(i) ⇒ Object



871
872
873
874
875
# File 'lib/numo/gnuplot.rb', line 871

def line_str(i)
  @data.map do |a|
    PlotData.quote_data(a[i])
  end.join(" ")
end