Class: Pulo::Frame
- Inherits:
-
Object
- Object
- Pulo::Frame
- Defined in:
- lib/pulo/frames/frame.rb
Instance Attribute Summary collapse
-
#column_count ⇒ Object
readonly
Returns the value of attribute column_count.
-
#column_names ⇒ Object
readonly
Returns the value of attribute column_names.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#row_count ⇒ Object
readonly
Returns the value of attribute row_count.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #[](column) ⇒ Object
- #append_column(name, hidden = false, &formula) ⇒ Object
- #append_row(values) ⇒ Object
- #clone ⇒ Object
- #copy_definition ⇒ Object
- #delete_column(name_or_index) ⇒ Object
- #export_csv(path) ⇒ Object
- #first_row ⇒ Object
- #formula_columns ⇒ Object
-
#group(group_function) ⇒ Object
applies the given function to each row returns a hash where each element (keyed by the group) is a frame containing the group.
- #group_reduce(group_function, column_defns) ⇒ Object
-
#initialize ⇒ Frame
constructor
A new instance of Frame.
- #insert_row(previous_row, values) ⇒ Object
- #inspect ⇒ Object
- #last_row ⇒ Object
- #lookup(value, column) ⇒ Object
- #recalc_all(with_timing = false) ⇒ Object
- #rename_column(old_name, new_name) ⇒ Object
- #sort(&sort_function) ⇒ Object
- #to_s(&filter) ⇒ Object
- #value_columns ⇒ Object
Constructor Details
#initialize ⇒ Frame
Returns a new instance of Frame.
13 14 15 16 17 18 19 |
# File 'lib/pulo/frames/frame.rb', line 13 def initialize @rows=[] @columns=[] @column_names={} @column_count=0 @row_count=0 end |
Instance Attribute Details
#column_count ⇒ Object (readonly)
Returns the value of attribute column_count.
11 12 13 |
# File 'lib/pulo/frames/frame.rb', line 11 def column_count @column_count end |
#column_names ⇒ Object (readonly)
Returns the value of attribute column_names.
11 12 13 |
# File 'lib/pulo/frames/frame.rb', line 11 def column_names @column_names end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
11 12 13 |
# File 'lib/pulo/frames/frame.rb', line 11 def columns @columns end |
#row_count ⇒ Object (readonly)
Returns the value of attribute row_count.
11 12 13 |
# File 'lib/pulo/frames/frame.rb', line 11 def row_count @row_count end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
11 12 13 |
# File 'lib/pulo/frames/frame.rb', line 11 def rows @rows end |
Instance Method Details
#[](column) ⇒ Object
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/pulo/frames/frame.rb', line 231 def [](column) if column.is_a?(Integer) raise IndexError,"No column number #{column} defined." unless @columns[column] @columns[column] else column_no=@column_names[column] raise IndexError,"Column with name '#{column}' not found." unless column_no @columns[column_no] end end |
#append_column(name, hidden = false, &formula) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/pulo/frames/frame.rb', line 168 def append_column(name,hidden=false,&formula) col=FrameColumn.new(name,self, hidden, &formula) @columns << col @column_names.merge!({name=>@column_count}) @rows.each do |rw| cell=FrameCell.new(col,rw) col.append_row(cell) rw.append_column(cell) end @column_count+=1 col end |
#append_row(values) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/pulo/frames/frame.rb', line 183 def append_row(values) if values.is_a?(Array) vals=values.each row=FrameRow.new(self,@row_count) @columns.each do |col| if values.length==column_count v = vals.next else v = col.value_column? ? vals.next : nil end cell=FrameCell.new(col,row,v) col.append_row(cell) row.append_column(cell) end @rows<<row @row_count+=1 row else raise 'Expecting an array of values as the append row.' end end |
#clone ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/pulo/frames/frame.rb', line 125 def clone frame=copy_definition @rows.each do |row| frame.append_row row.to_a end frame end |
#copy_definition ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/pulo/frames/frame.rb', line 133 def copy_definition frame=Frame.new @columns.each do |col| frame.append_column(col.name,col.hidden?,&col.formula) end frame end |
#delete_column(name_or_index) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pulo/frames/frame.rb', line 141 def delete_column(name_or_index) if name_or_index.is_a?(Integer) raise IndexError,"No column number #{name_or_index} defined." unless @columns[name_or_index] index=name_or_index name=@columns[index].name else index=@column_names[name_or_index] raise IndexError,"Column with name '#{name_or_index}' not found." unless index name=name_or_index end @rows.each {|row| row.delete_column(index)} @column_names.delete(name) @column_names.each do |item| @column_names[item[0]]-=1 if @column_names[item[0]]>=index end @columns.delete_at(index) @column_count-=1 end |
#export_csv(path) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pulo/frames/frame.rb', line 21 def export_csv(path) CSV.open(path, 'wb') do |csv| csv << @columns.map {|col| col.name} csv << @columns.map {|col| if col.column_class.respond_to?(:quantity_name) col.column_class.quantity_name else col.column_class.name end } csv << @columns.map {|col| if col.column_class.respond_to?(:quantity_name) col.column_unit.abbreviation else '' end } @rows.each do |row| csv<<row.to_a.map{|cell| if cell.class.respond_to?(:quantity_name) cell.value else cell end } end end end |
#first_row ⇒ Object
242 243 244 |
# File 'lib/pulo/frames/frame.rb', line 242 def first_row @rows.first end |
#formula_columns ⇒ Object
284 285 286 |
# File 'lib/pulo/frames/frame.rb', line 284 def formula_columns @columns.find_all { |col| !col.value_column?} end |
#group(group_function) ⇒ Object
applies the given function to each row returns a hash where each element (keyed by the group) is a frame containing the group
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pulo/frames/frame.rb', line 53 def group(group_function) #t=Time.now groups={} @rows.each do |row| res=group_function.call(row) if groups[res] groups[res].append_row(row.to_a) else frm=self.copy_definition frm.append_row(row.to_a) groups.merge!({res=>frm}) end end #puts "Groups in #{((Time.now-t)*1000).to_i} ms." groups end |
#group_reduce(group_function, column_defns) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pulo/frames/frame.rb', line 70 def group_reduce(group_function,column_defns) #get groups groups=group(group_function) #t=Time.now output=Frame.new output.append_column 'Group' #setup the columns column_defns.keys.each do |col| output.append_column(col) end #add a row to the output for each group produced in the earlier stage groups.each do |group| row=column_defns.map do |defn| if defn[1].class==Proc defn[1].call group[1] else #assume an array with method for descriptive statistics and a column name c=group[1][defn[1][1]] v=case defn[1][0] when :min c.to_a.min when :max c.to_a.max else DescriptiveStatistics.send(defn[1][0],c.to_a) end if c.column_class.respond_to?(:quantity_name) && defn[1][0]!=:number c.column_class.send(c.column_unit.name,v) else v end end end row.insert 0,group[0] output.append_row row end #puts "Reduce in #{((Time.now-t)*1000).to_i} ms." output end |
#insert_row(previous_row, values) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/pulo/frames/frame.rb', line 204 def insert_row(previous_row,values) if values.is_a?(Array) row_no=previous_row.row_number+1 vals=values.each row=FrameRow.new(self,row_no) @columns.each do |col| if values.length==column_count v = vals.next else v = col.value_column? ? vals.next : nil end cell=FrameCell.new(col,row,v) col.insert_row(row_no,cell) row.append_column(cell) end @rows.insert(row_no,row) @row_count+=1 @rows.drop(row_no+1).each {|r| r.row_number+=1} else raise 'Expecting an array of values as the append row.' end end |
#inspect ⇒ Object
276 277 278 |
# File 'lib/pulo/frames/frame.rb', line 276 def inspect "Frame Object" end |
#last_row ⇒ Object
245 246 247 |
# File 'lib/pulo/frames/frame.rb', line 245 def last_row @rows.last end |
#lookup(value, column) ⇒ Object
249 250 251 |
# File 'lib/pulo/frames/frame.rb', line 249 def lookup(value,column) self[column].lookup(value) end |
#recalc_all(with_timing = false) ⇒ Object
227 228 229 |
# File 'lib/pulo/frames/frame.rb', line 227 def recalc_all(with_timing=false) formula_columns.each { |col| col.recalc with_timing } end |
#rename_column(old_name, new_name) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/pulo/frames/frame.rb', line 160 def rename_column(old_name,new_name) col=self[old_name] col_no=@column_names[old_name] @column_names.delete(old_name) @column_names.merge!({new_name=>col_no}) col.name=new_name end |
#sort(&sort_function) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pulo/frames/frame.rb', line 114 def sort(&sort_function) int=@rows.map do |row| [row.row_number,sort_function.call(row)] end.sort_by {|elm| elm[1]}.map{|elm| elm[0]} frm=self.copy_definition int.each do |row_no| frm.append_row self.rows[row_no].to_a_values end frm end |
#to_s(&filter) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/pulo/frames/frame.rb', line 253 def to_s(&filter) @columns.each {|c| c.recalc_width} ret="\n " ret+=@columns.select {|c| !c.hidden?}.map{|s| s.name.ljust(s.width,' ')}.join(' ') + "\n" ret+='========= ' ret+=@columns.select {|c| !c.hidden?}.map{|s| ''.ljust(s.width,'=')}.join('= ') + "\n" ret+=' ' ret+=@columns.select {|c| !c.hidden?}.map{|s| if s.column_class.respond_to?(:quantity_name) (s.column_class.quantity_name + (s.value_column? ? '' : '*')).ljust(s.width,' ') else (s.column_class.to_s + (s.value_column? ? '' : '*')).ljust(s.width,' ') end }.join(' ') + "\n" ret+='--------- ' ret+=@columns.select {|c| !c.hidden?}.map{|s| ''.ljust(s.width,'-')}.join('- ') + "\n" ret+=(@rows.select{|row| !block_given? || filter.call(row)}.map { |row| row.to_s}).join("\n") ret+="\n" + @rows.length.to_s + " rows\n" ret end |
#value_columns ⇒ Object
280 281 282 |
# File 'lib/pulo/frames/frame.rb', line 280 def value_columns @columns.find_all { |col| col.value_column?} end |