class GraphKit::DataKit
Constants
- ALLOWED_RANKS
ALLOWED_RANKS
= [[1], [1,1], [1,1,1], [1,1,2], [1,1,1,1], [1,1,2,2], [1,1,1,3]]- AXES
Public Class Methods
autocreate(hash)
click to toggle source
# File lib/graphkit.rb, line 590 def self.autocreate(hash) new.autocreate(hash) end
new(options = {})
click to toggle source
Calls superclass method
# File lib/graphkit.rb, line 584 def initialize(options = {}) super() self[:axes] = {} absorb options end
Public Instance Methods
allowed_ranks()
click to toggle source
# File lib/graphkit.rb, line 664 def allowed_ranks ALLOWED_RANKS end
autocreate(hash)
click to toggle source
# File lib/graphkit.rb, line 594 def autocreate(hash) logf :autocreate hash.each do |key, value| # puts value.inspect if AXES.include? key self[:axes][key] = AxisKit.autocreate(value) else raise ArgumentError.new("bad key value pair in autocreate: #{key.inspect}, #{value.inspect}") end # puts self[key].inspect end # pp self first = true self.title = AXES.map{|axis| axes[axis] ? axes[axis].title : nil}.compact.reverse.inject("") do |str, name| str + name + (first ? (first = false; ' vs ') : ', ') end self.title = self.title.sub(/, $/, '').sub(/ vs $/, '') check_integrity self end
axes_array()
click to toggle source
attr_accessor :labels, :ranges, :has_legend, :units, :dimensions
# File lib/graphkit.rb, line 581 def axes_array self.axes.values_at(*AXES).compact end
check_integrity()
click to toggle source
# File lib/graphkit.rb, line 615 def check_integrity logf :check_integrity check(['title.class', [String, NilClass]], ['with.class', [String, NilClass]], ['axes.class', Hash]) axes.keys.each do |key| check(["key #{key} from a datakit.axes", key, AXES]) check(["self.axes[#{key.inspect}].class", AxisKit]) self.axes[key].check_integrity end # axes.values.map{|axiskit| axiskit.data.to_a.size}.each_with_index do |size, index| # raise IntegrityError.new("Axis data sets in this datakit have different sizes than the function #{size}, #{f.shape[0]}") unless size == new_size # size # end # puts 'checking f.class', f.class # check(['f.class', CodeRunner::FunctionKit]) # shape = f.shape log 'checking ranks' rnks = ranks log rnks raise IntegrityError.new("The combination of ranks of your data cannot be plotted. Your data has a set of axes with ranks #{rnks.inspect}. (NB, rank 1 corresponds to a vector, rank 2 to a matrix and 3 to a third rank tensor). The only possible sets of types are #{allowed_ranks.inspect}") unless allowed_ranks.include? rnks passed = true case rnks when [1], [1,1], [1,1,1], [1,1,1,1] axes.values.map{|axiskit| axiskit.shape}.inject do |old, new| # puts old, new passed = false unless new == old old end when [1,1,2], [1,1,2,2] # passed = false unless axes[:x].shape == axes[:y].shape passed = false unless axes[:z].shape == [axes[:x].shape[0], axes[:y].shape[0]] passed = false unless axes[:z].shape == axes[:f].shape if axes[:f] when [1,1,1,3] #axes.values_at(:x, :y, :z).map{|axiskit| axiskit.shape}.inject do |old, new| #passed = false unless new == old #old #end passed = false unless axes[:f].shape == [axes[:x].shape[0], axes[:y].shape[0], axes[:z].shape[0]] end raise IntegrityError.new(%[The dimensions of this data do not match: \n#{axes.inject(""){|str, (axis, axiskit)| str + "#{axis}: #{axiskit.shape}\n"}}\nranks: #{rnks}]) unless passed # log 'finished checking ranks' logfc :check_integrity # raise IntegrityError.new("function data must be a vector, or have the correct dimensions (or shape) for the axes: function dimensions: #{shape}; axes dimesions: #{axes_shape}") unless shape.size == 1 or axes_shape == shape return true end
convert() { |xdat, ydat| ... }
click to toggle source
# File lib/graphkit.rb, line 555 def convert(&block) xdat = self.axes[:x].data.to_gslv ydat = self.axes[:y].data.to_gslv xnew = GSL::Vector.alloc(xdat.size) ynew = GSL::Vector.alloc(xdat.size) for i in 0...xdat.size xnew[i], ynew[i] = yield(xdat[i], ydat[i]) end self.axes[:x].data=xnew self.axes[:y].data=ynew #p 'dk', self self end
dup()
click to toggle source
# File lib/graphkit.rb, line 751 def dup # puts 'Datakit.dup' new = self.class.new(self) new.axes.each do |axis, value| new.axes[axis] = value.dup end new end
exclude_outliers()
click to toggle source
# File lib/graphkit.rb, line 779 def exclude_outliers raise "Can only get rid of outliers for 1D or 2D data" if axes.size > 2 # self.outliers = [] if axes.size == 1 data = axes[:x].data i = 0 loop do break if i > data.size - 2 should_be = (data[i+1] + data[i-1]) / 2.0 deviation = (should_be - data[i]).abs / data[i].abs if deviation > outlier_tolerance data.delete_at(i) i-=1 end i+=1 end else x_data = axes[:x].data data = axes[:y].data i = 0 loop do jump = 1 loop do break if i > data.size - 1 - jump break unless x_data[i+jump] == x_data[i-jump] jump += 1 end break if i > data.size - 1 - jump should_be = data[i-jump] + (data[i+jump] - data[i-jump]) / (x_data[i+jump] - x_data[i-jump]) * (x_data[i] - x_data[i-jump]) #ie y1 + gradient * delta x deviation = (should_be - data[i]).abs / data[i].abs if deviation > outlier_tolerance data.delete_at(i) x_data.delete_at(i) i-=1 end i+=1 end end # p self.outliers end
extend_using(other)
click to toggle source
# File lib/graphkit.rb, line 696 def extend_using(other) raise "A dataset can only be extended using another dataset with the same ranks: the ranks of this dataset are #{ranks} and the ranks of the other dataset are #{other.ranks}" unless ranks == other.ranks axes.each do |key, axiskit| axiskit.extend_using(other.axes[key]) end end
gnuplot(io)
click to toggle source
# File lib/graphkit/gnuplot.rb, line 378 def gnuplot(io) axs = self.axes.values_at(*AXES).compact #ep 'axs', axs dl = data_length = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes cml_sh = sh.map do |sh| cml = 1 sh.reverse.map{|dim| cml *= dim; cml}.reverse end dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact #ep 'edat', edat end case ranks when [1], [1,1], [1,1,1], [1,1,1,1] dl.times do |n| dat.each{|d| io << d[n] << " "} io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors io << "\n" end when [1,1,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][1] == 1 end when [2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][1] == 1 end when [1,1,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3] sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][2] == 1 end io << "\n" unless sh[-1][1] == 1 end when [2,2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][1] == 1 end when [3,3,3,3] #pp dat #pp dat #pp sh sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]} \n" #d.each{|dt| io << dt << " "} #io << "\n" end io << "\n" unless sh[-1][2] == 1 end io << "\n" unless sh[-1][1] == 1 end end end
gnuplot_plot_options()
click to toggle source
# File lib/graphkit/gnuplot.rb, line 363 def gnuplot_plot_options self[:gnuplot_plot_options] ||= GnuplotPlotOptions.new end
Also aliased as: gp
mtm()
click to toggle source
# File lib/graphkit/mm.rb, line 31 def mtm self[:mtm] ||= MathematicaOptions.new end
plot_area_size()
click to toggle source
# File lib/graphkit.rb, line 760 def plot_area_size ans = [] # p data (axes.values_at(*AXES).compact).each do |axiskit| if range = axiskit.range ans.push range next else # p 'hello' # p data[0].axes[key] axdata = axiskit.data #(key == :f) ? data[0].f.data : (next unless data[0].axes[key]; data[0].axes[key].data) next unless axdata #p 'axdatamin', axdata.min ans.push [axdata.min, axdata.max] end end ans end
rank_c_switch()
click to toggle source
# File lib/graphkit.rb, line 682 def rank_c_switch #i = -1 #puts ALLOWED_RANKS.map{|r| i+=1;"#{i} --> #{r}"} switch = ALLOWED_RANKS.index(ranks) switch end
ranks()
click to toggle source
# File lib/graphkit.rb, line 689 def ranks logf :ranks ans = shapes.map{|shape| shape.size} logfc :ranks return ans end
shapes()
click to toggle source
def ranks_c_switch_hash hash = {} allowed_ranks.each_with_index do |rank,i| hash = i end p hash hash end
# File lib/graphkit.rb, line 675 def shapes logf :shapes ans = axes.values_at(*AXES).compact.inject([]){|arr, axis| arr.push axis.shape} logfc :shapes return ans end
to_csv(options={})
click to toggle source
# File lib/graphkit/csv.rb, line 26 def to_csv(options={}) io = options[:io] || StringIO.new if options[:append] if File.exists?(io) options[:header] = nil end csv_file = File.open(io, 'a') else csv_file = File.open(io, 'w') end unless options[:header].nil? header = options[:header].to_s csv_file.write(header + "\n") end axs = self.axes.values_at(*AXES).compact dl = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact end io = '' case ranks when [1], [1,1], [1,1,1], [1,1,1,1] dl.times do |n| dat.each{|d| io << d[n].to_s << ","} io << " " << edat.map{|e| e[n].to_s}.join(", ") if self.errors io << "\n" end when [1,1,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j]] io << d[0] << ", " << d[1] << ", " << d[2] << "\n" end io << "\n" unless sh[-1][1] == 1 end when [2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] io << d[0] << ", " << d[1] << ", " << d[2] << "\n" end io << "\n" unless sh[-1][1] == 1 end when [1,1,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" end io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3] sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" end io << "\n" unless sh[-1][2] == 1 end io << "\n" unless sh[-1][1] == 1 end when [2,2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]] d.each{|dt| io << dt << " "} io << "\n" end io << "\n" unless sh[-1][1] == 1 end when [3,3,3,3] #pp dat #pp dat #pp sh sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] io << "#{dat[0][i,j,k]},#{dat[1][i,j,k]},#{dat[2][i,j,k]},#{dat[3][i,j,k]}\n" #d.each{|dt| io << dt << " "} #io << "\n" end io << "\n" unless sh[-1][2] == 1 end io << "\n" unless sh[-1][1] == 1 end end return stringio.string unless options[:io] csv_file.write(io) csv_file.close() end
to_mathematica()
click to toggle source
# File lib/graphkit/mm.rb, line 34 def to_mathematica case ranks when [1], [1,1], [1, 1, 1] "{#{(axes.values.map{|ax| ax.data.to_a}.transpose.map do |datapoint| "{#{datapoint.map{|coord| coord.to_s}.join(',')}}" end).join(',')}}" end end
vtk_legacy_cell_types(options={})
click to toggle source
# File lib/graphkit/vtk_legacy_ruby.rb, line 422 def vtk_legacy_cell_types(options={}) pointcount = options[:pointcount] io = options[:io] || StringIO.new axs = self.axes.values_at(*AXES).compact #ep 'axs', axs dl = data_length = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes cml_sh = sh.map do |sh| cml = 1 sh.reverse.map{|dim| cml *= dim; cml}.reverse end dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact #ep 'edat', edat end case ranks when [1], [1,1], [1,1,1], [1,1,1,1] (dl-1).times do |n| io << "3" # Lines io << "\n" end #when [1,1,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[2][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [2,2,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[2][i,j] #d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [1,1,2,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[3][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [1,1,1,3] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #sh[-1][2].times do |k| #next unless dat[3][i,j,k] #d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] #io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" #end #io << "\n" unless sh[-1][2] == 1 #end #io << "\n" unless sh[-1][1] == 1 #end when [1,1,2], [2,2,2], [1,1,2,2], [2,2,2,2] ni, nj = sh[-1] type = case [ni > 1 ? 1 : nil, nj > 1 ? 1 : nil].compact.size when 2 7 # Polygons see www.vtk.org/VTK/img/file-formats.pdf when 1 3 # Lines when 0 1 # Verteces end sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) io << type << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3],[3,3,3,3] #pp dat #pp dat #pp sh ni, nj, nk = sh[-1] type = case [ni > 1 ? 1 : nil, nj > 1 ? 1 : nil, nk > 1 ? 1 : nil].compact.size when 3 11 # 3D cells, see www.vtk.org/VTK/img/file-formats.pdf when 2 7 # Polygons when 1 3 # Lines when 0 1 # Verteces end #ep ni, nj, nk; gets sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] #io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]}\n" next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) and (nk==1 or (k+1) < nk) io << type << "\n" #d.each{|dt| io << dt << " "} #io << "\n" end #io << "\n" unless sh[-1][2] == 1 end #io << "\n" unless sh[-1][1] == 1 end end return io.string unless options[:io] end
vtk_legacy_cells(options={})
click to toggle source
# File lib/graphkit/vtk_legacy_ruby.rb, line 295 def vtk_legacy_cells(options={}) pointcount = options[:pointcount] io = options[:io] || StringIO.new axs = self.axes.values_at(*AXES).compact #ep 'axs', axs dl = data_length = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes cml_sh = sh.map do |sh| cml = 1 sh.reverse.map{|dim| cml *= dim; cml}.reverse end dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact #ep 'edat', edat end case ranks when [1], [1,1], [1,1,1], [1,1,1,1] (dl-1).times do |n| #dat.each{|d| io << d[n] << " "} #io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors next unless dat[-1][n] io << "2 #{n + pointcount} #{n + pointcount + 1}" io << "\n" end #when [1,1,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[2][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [2,2,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[2][i,j] #d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [1,1,2,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[3][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" #end #io << "\n" unless sh[-1][1] == 1 #end #when [1,1,1,3] #ni, nj, nk = sh[-1] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #sh[-1][2].times do |k| #next unless dat[3][i,j,k] #d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] #io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n" #end #io << "\n" unless sh[-1][2] == 1 #end #io << "\n" unless sh[-1][1] == 1 #end when [1,1,2], [2,2,2], [1,1,2,2], [2,2,2,2] ni, nj = sh[-1] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) cell = [ i*(nj) + j , nj > 1 ? i*(nj) + (j+1) : nil, ni > 1 ? (i+1)*(nj) + j : nil, ni > 1 && nj > 1 ? (i+1)*(nj) + (j+1) : nil, ].compact.map{|pnt| pnt+pointcount} cell = [cell[0],cell[1],cell[3],cell[2]] if cell.size == 4 io << cell.size << " " << cell.join(" ") << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3],[3,3,3,3] #pp dat #pp dat #pp sh ni, nj, nk = sh[-1] #ep ni, nj, nk; gets sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] #io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]}\n" next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) and (nk==1 or (k+1) < nk) cell = [ i*(nj*nk) + j*nk + k, nk > 1 ? i*(nj*nk) + j*nk + (k+1) : nil, nj > 1 ? i*(nj*nk) + (j+1)*nk + (k) : nil, nj > 1 && nk > 1 ? i*(nj*nk) + (j+1)*nk + (k+1) : nil, ni > 1 ? (i+1)*(nj*nk) + j*nk + k : nil, ni > 1 && nk > 1 ? (i+1)*(nj*nk) + j*nk + (k+1) : nil, ni > 1 && nj > 1 ? (i+1)*(nj*nk) + (j+1)*nk + (k) : nil, ni > 1 && nj > 1 && nk > 1 ? (i+1)*(nj*nk) + (j+1)*nk + (k+1) : nil, ].compact.map{|pnt| pnt+pointcount} cell = [cell[0],cell[1],cell[3],cell[2]] if cell.size == 4 io << cell.size << " " << cell.join(" ") << "\n" #d.each{|dt| io << dt << " "} #io << "\n" end #io << "\n" unless sh[-1][2] == 1 end #io << "\n" unless sh[-1][1] == 1 end end return io.string unless options[:io] end
vtk_legacy_point_data(options={})
click to toggle source
# File lib/graphkit/vtk_legacy_ruby.rb, line 196 def vtk_legacy_point_data(options={}) io = options[:io] || StringIO.new axs = self.axes.values_at(*AXES).compact #ep 'axs', axs dl = data_length = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes cml_sh = sh.map do |sh| cml = 1 sh.reverse.map{|dim| cml *= dim; cml}.reverse end dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact #ep 'edat', edat end case ranks when [1], [1,1], [1,1,1], [1,1,1,1] dl.times do |n| next unless dat[-1][n] #dat.each{|d| io << d[n] << " "} #io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors io << "#{dat[3] ? dat[3][n] : 0}\n" end #when [1,1,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[2][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" #end ##io << "\n" unless sh[-1][1] == 1 #end when [1,1,2], [2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] #d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] #io << d[0] << ", " << d[1] << ", " << d[2] << "\n" io << "0\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] #d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] io << dat[3][i,j] << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3] sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] io << dat[3][i,j,k] << "\n" end #io << "\n" unless sh[-1][2] == 1 end #io << "\n" unless sh[-1][1] == 1 end when [2,2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] io << "#{dat[3][i,j]}\n" end end when [3,3,3,3] #pp dat #pp dat #pp sh sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] io << "#{dat[3][i,j,k]}\n" #d.each{|dt| io << dt << " "} #io << "\n" end #io << "\n" unless sh[-1][2] == 1 end #io << "\n" unless sh[-1][1] == 1 end end return io.string unless options[:io] end
vtk_legacy_points(options={})
click to toggle source
# File lib/graphkit/vtk_legacy_ruby.rb, line 87 def vtk_legacy_points(options={}) io = options[:io] || StringIO.new axs = self.axes.values_at(*AXES).compact #ep 'axs', axs dl = data_length = axs[-1].shape.product dat = axs.map{|ax| ax.data} sh = shapes cml_sh = sh.map do |sh| cml = 1 sh.reverse.map{|dim| cml *= dim; cml}.reverse end dat = dat.map do |d| d.kind_of?(Array) ? TensorArray.new(d) : d end if self.errors raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1] edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact #ep 'edat', edat end case ranks when [1] dl.times do |n| next unless dat[-1][n] dat.times.each{|idx| io << "0 " << idx << " #{dat[0][n]}"} io << "\n" end when [1,1] dl.times do |n| next unless dat[-1][n] io << "0 " << dat[0][n] << ' ' << dat[1][n] #io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors io << "\n" end when [1,1,1],[1,1,1,1] dl.times do |n| next unless dat[-1][n] io << dat[0][n] << ' ' << dat[1][n] << ' ' << dat[2][n] io << "\n" end when [1,1,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j]] io << d[0] << " " << d[1] << " " << d[2] << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [2,2,2], [2,2,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[2][i,j] d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]] io << d[0] << " " << d[1] << " " << d[2] << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,2,2] sh[-1][0].times do |i| sh[-1][1].times do |j| next unless dat[3][i,j] d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]] io << d[0] << " " << d[1] << " " << d[2] << "\n" end #io << "\n" unless sh[-1][1] == 1 end when [1,1,1,3] sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]] io << d[0] << " " << d[1] << " " << d[2] << "\n" end end end #when [2,2,2,2] #sh[-1][0].times do |i| #sh[-1][1].times do |j| #next unless dat[3][i,j] ##d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]] #io << "#{dat[0][i,j]} #{dat[1][i,j]} #{dat[2][i,j]}\n" ##d.each{|dt| io << dt << " "} ##io << "\n" #end #end when [3,3,3,3] #pp dat #pp dat #pp sh sh[-1][0].times do |i| sh[-1][1].times do |j| sh[-1][2].times do |k| next unless dat[3][i,j,k] #p [i,j,k] #d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]] io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]}\n" #d.each{|dt| io << dt << " "} #io << "\n" end end end end return io.string unless options[:io] end
vtk_legacy_size()
click to toggle source
# File lib/graphkit/vtk_legacy_ruby.rb, line 83 def vtk_legacy_size axs = self.axes.values_at(*AXES).compact return axs[-1].shape.product end