Class: Mikon::Series
- Inherits:
-
Object
- Object
- Mikon::Series
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mikon/plot.rb,
lib/mikon/core/series.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#name(new_name = nil) ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #%(arg) ⇒ Object
- #*(arg) ⇒ Object
- #+(arg) ⇒ Object
- #-(arg) ⇒ Object
- #/(arg) ⇒ Object
- #[](arg) ⇒ Object
- #coerce(other) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(name, source, options = {}) ⇒ Series
constructor
A new instance of Series.
- #length ⇒ Object
- #plot(args = {}) ⇒ Object
- #to_a ⇒ Object
- #to_darr ⇒ Object
- #to_html(threshold = 5) ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s(threshold = 5) ⇒ Object
Constructor Details
#initialize(name, source, options = {}) ⇒ Series
Returns a new instance of Series.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mikon/core/series.rb', line 11 def initialize(name, source, ={}) = { index: nil }.merge() case when source.is_a?(Array) || source.is_a?(NMatrix) @data = Mikon::DArray.new(source) when source.is_a?(Mikon::DArray) @data = source else raise "Non-acceptable Arguments Error" end @index = [:index] @name = name _check_if_valid end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
9 10 11 |
# File 'lib/mikon/core/series.rb', line 9 def index @index end |
#name(new_name = nil) ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/mikon/core/series.rb', line 9 def name @name end |
Instance Method Details
#%(arg) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/mikon/core/series.rb', line 107 def %(arg) if arg.is_a?(Numeric) Series.new(self.name, @data%arg, index: self.index) else raise ArgumentError end end |
#*(arg) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/mikon/core/series.rb', line 91 def *(arg) if arg.is_a?(Numeric) Series.new(self.name, @data*arg, index: self.index) else raise ArgumentError end end |
#+(arg) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/mikon/core/series.rb', line 123 def +(arg) if arg.is_a?(Mikon::Series) && arg.length == self.length Series.new(self.name, arg.coerce(@data).inject(:+), index: self.index) else raise ArgumentError end end |
#-(arg) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/mikon/core/series.rb', line 115 def -(arg) if arg.is_a?(Mikon::Series) && arg.length == self.length Series.new(self.name, arg.coerce(@data).inject(:-), index: self.index) else raise ArgumentError end end |
#/(arg) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/mikon/core/series.rb', line 99 def /(arg) if arg.is_a?(Numeric) Series.new(self.name, @data/arg, index: self.index) else raise ArgumentError end end |
#[](arg) ⇒ Object
44 45 46 47 48 |
# File 'lib/mikon/core/series.rb', line 44 def [](arg) pos = @index.index(arg) raise "There is no index named" + arg.to_s if pos.nil? @data[pos] end |
#coerce(other) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/mikon/core/series.rb', line 131 def coerce(other) if other.is_a?(Mikon::DArray) return other, @data elsif other.is_a?(Numeric) return self, other else raise ArgumentError end end |
#each(&block) ⇒ Object
40 41 42 |
# File 'lib/mikon/core/series.rb', line 40 def each(&block) @data.each(&block) end |
#length ⇒ Object
36 37 38 |
# File 'lib/mikon/core/series.rb', line 36 def length @data.length end |
#plot(args = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mikon/plot.rb', line 5 def plot(args={}) args = { :type => :histogram }.merge(args) plot = Nyaplot::Plot.new case args[:type] when :histogram plot.add(:histogram, @data.to_a) when :line plot.add(:line, @index, @data.to_a) end plot end |
#to_a ⇒ Object
83 84 85 |
# File 'lib/mikon/core/series.rb', line 83 def to_a @data.to_a end |
#to_darr ⇒ Object
87 88 89 |
# File 'lib/mikon/core/series.rb', line 87 def to_darr @data end |
#to_html(threshold = 5) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/mikon/core/series.rb', line 50 def to_html(threshold=5) html = "<table><tr><th></th><th>" + self.name.to_s + "</th></tr>" @index.each.with_index do |index, pos| next if pos > threshold && pos != self.length-1 html += "<tr><th>" + index.to_s + "</th><td>" + @data[pos].to_s + "</td></tr>" html += "<tr><th>...</th><td>...</td></tr>" if pos == threshold end html + "</table>" end |
#to_json(*args) ⇒ Object
70 71 72 |
# File 'lib/mikon/core/series.rb', line 70 def to_json(*args) @data.to_json end |
#to_s(threshold = 5) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/mikon/core/series.rb', line 60 def to_s(threshold=5) arr = [] @index.each.with_index do |index, pos| next nil if pos > threshold && pos != self.length-1 arr.push({"" => index, @name => @data[pos]}) arr.push({"" => "...", @name => "..."}) if pos == threshold end Formatador.display_table(arr.select{|el| !(el.nil?)}) end |