Class: GraphKit::DataKit
- Includes:
- Log
- Defined in:
- lib/graphkit.rb,
lib/graphkit/gnuplot.rb
Overview
end
Constant Summary collapse
- AXES =
GraphKit::AXES
Class Method Summary collapse
Instance Method Summary collapse
- #autocreate(hash) ⇒ Object
- #check_integrity ⇒ Object
- #dup ⇒ Object
- #exclude_outliers ⇒ Object
- #extend_using(other) ⇒ Object
- #gnuplot ⇒ Object
- #gnuplot_options ⇒ Object (also: #gp)
- #gnuplot_ranks ⇒ Object
-
#initialize(options = {}) ⇒ DataKit
constructor
attr_accessor :labels, :ranges, :has_legend, :units, :dimensions.
- #plot_area_size ⇒ Object
- #ranks ⇒ Object
- #shapes ⇒ Object
Methods inherited from KitHash
Methods included from Kit
Methods inherited from Hash
Constructor Details
#initialize(options = {}) ⇒ DataKit
attr_accessor :labels, :ranges, :has_legend, :units, :dimensions
454 455 456 457 458 |
# File 'lib/graphkit.rb', line 454 def initialize( = {}) super() self[:axes] = {} absorb end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Kit
Class Method Details
.autocreate(hash) ⇒ Object
460 461 462 |
# File 'lib/graphkit.rb', line 460 def self.autocreate(hash) new.autocreate(hash) end |
Instance Method Details
#autocreate(hash) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/graphkit.rb', line 464 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 |
#check_integrity ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/graphkit.rb', line 485 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' allowed_ranks = [[1], [1,1], [1,1,1], [1,1,2], [1,1,1,1], [1,1,2,2], [1,1,1,3]] 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 |
#dup ⇒ Object
601 602 603 604 605 606 607 608 |
# File 'lib/graphkit.rb', line 601 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 ⇒ Object
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/graphkit.rb', line 628 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) ⇒ Object
546 547 548 549 550 551 |
# File 'lib/graphkit.rb', line 546 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 ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/graphkit/gnuplot.rb', line 290 def gnuplot # p axes.values_at(*AXES).compact.zip(gnuplot_ranks) Gnuplot::DataSet.new(axes.values_at(*AXES).compact.zip(gnuplot_ranks).map{|axis, rank| axis.data_for_gnuplot(rank) }) do |ds| (keys - [:axes, :outlier_tolerance, :outliers, :gnuplot_options]).each do |key| ds.set(key, self[key]) end if .each do |opt, val| ds.set(opt, val) end end # ds.title = title # ds.with = [:lines, :points].inject(""){|str, opt| self[opt] ? str + opt.to_s : str } # p ds.with # ds.with = "lines" # ds.linewidth = 4 end end |
#gnuplot_options ⇒ Object Also known as: gp
309 310 311 312 |
# File 'lib/graphkit/gnuplot.rb', line 309 def ||= GnuplotPlotOptions.new end |
#gnuplot_ranks ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/graphkit/gnuplot.rb', line 274 def gnuplot_ranks case axes.size when 1,2 return ranks when 3 return [1,1,2] when 4 case ranks[2] when 1 return [1,1,1,3] when 2 return [1,1,2,2] end end end |
#plot_area_size ⇒ Object
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/graphkit.rb', line 610 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 ans.push [axdata.min, axdata.max] end end ans end |
#ranks ⇒ Object
539 540 541 542 543 544 |
# File 'lib/graphkit.rb', line 539 def ranks logf :ranks ans = shapes.map{|shape| shape.size} logfc :ranks return ans end |
#shapes ⇒ Object
532 533 534 535 536 537 |
# File 'lib/graphkit.rb', line 532 def shapes logf :shapes ans = axes.values_at(*AXES).compact.inject([]){|arr, axis| arr.push axis.shape} logfc :shapes return ans end |