Class: ObjectTable

Inherits:
Object
  • Object
show all
Includes:
TableMethods
Defined in:
lib/object_table.rb,
lib/object_table/version.rb

Defined Under Namespace

Modules: Printable, TableChild, TableMethods, ViewMethods Classes: BasicGrid, Column, Group, Grouped, MaskedColumn, StaticView, View

Constant Summary collapse

VERSION =
"0.2.3"

Instance Attribute Summary collapse

Attributes included from TableMethods

#R

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TableMethods

#==, #_get_sort_index, #apply, #clone, #colnames, #group_by, #method_missing, #ncols, #nrows, #pop_column, #respond_to?, #set_column, #sort_by, #where

Methods included from Printable

get_printable_column, get_printable_line_numbers, #inspect

Constructor Details

#initialize(columns = {}) ⇒ ObjectTable

Returns a new instance of ObjectTable.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/object_table.rb', line 14

def initialize(columns = {})
  super()

  unless columns.is_a? BasicGrid
    columns = BasicGrid[columns]
  end
  columns._ensure_uniform_columns!
  @columns = columns

  @columns.each do |k, v|
    @columns[k] = ObjectTable::Column.make(v)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ObjectTable::TableMethods

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



12
13
14
# File 'lib/object_table.rb', line 12

def columns
  @columns
end

Class Method Details

.stack(*values) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/object_table.rb', line 64

def self.stack(*values)
  return self.new if values.empty?
  base = values.shift

  case base
  when ObjectTable::BasicGrid
    base = self.new(base.clone)
  when ObjectTable, ObjectTable::View
    base = base.clone
  else
    raise "Don't know how to join a #{base.class}"
  end
  base.stack!(*values)
end

Instance Method Details

#__group_cls__Object



165
166
167
# File 'lib/object_table.rb', line 165

def __group_cls__
  self.class::Group
end

#__static_view_cls__Object



157
158
159
# File 'lib/object_table.rb', line 157

def __static_view_cls__
  self.class::StaticView
end

#__table_cls__Object



169
170
171
# File 'lib/object_table.rb', line 169

def __table_cls__
  self.class
end

#__view_cls__Object



161
162
163
# File 'lib/object_table.rb', line 161

def __view_cls__
  self.class::View
end

#add_column(name, typecode = 'object', *args) ⇒ Object



28
29
30
31
# File 'lib/object_table.rb', line 28

def add_column(name, typecode='object', *args)
  col = ObjectTable::Column.new(typecode, *args, nrows)
  columns[name] = col
end

#join(other, key, options = {}) ⇒ Object



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/object_table.rb', line 88

def join(other, key, options={})
  type = (options[:type] || 'inner')
  key = [key] unless key.is_a?(Array)

  lkeys = key.map{|k| get_column(k).to_a}.transpose
  rkeys = key.map{|k| other[k].to_a}.transpose

  rgroups = rkeys.each_with_index.group_by(&:first)
  rgroups.each{|k, v| rgroups[k] = v.transpose[-1]}

  rindex = rgroups.values_at(*lkeys)
  lindex = lkeys.each_with_index.zip(rindex).flat_map{|(k, i), r| [i] * r.length if r}
  lindex.compact!

  right_cols = other.colnames - key
  left_cols = colnames

  if type == 'left' or type == 'outer'
    lmissing = NArray.to_na(rindex.map{|x| x ? 0 : 1}).where.to_a
    lindex += lmissing
    rindex += [-1] * lmissing.length
  end

  if type == 'right' or type == 'outer'
    left_cols -= key
    rmissing = rgroups.values - rindex
    rmissing.flatten!
    lindex += [-1] * rmissing.length
    rindex += rmissing
  end

  rindex.flatten!.compact!

  lindex = NArray.to_na(lindex)
  rindex = NArray.to_na(rindex)
  lblank = lindex.eq(-1)
  rblank = rindex.eq(-1)

  left = left_cols.map do |k|
    col = get_column(k)
    padding = [nil] * (col.rank - 1)
    col = col[*padding, lindex]
    col[*padding, lblank] = [nil]
    [k, col]
  end

  right = right_cols.map do |k|
    col = other[k]
    padding = [nil] * (col.rank - 1)
    col = col[*padding, rindex]
    col[*padding, rblank] = [nil]
    [k, col]
  end

  keys = []
  if type == 'right' or type == 'outer'
    keys = key.map do |k|
      col = get_column(k)
      padding = [nil] * (col.rank - 1)
      col = col[*padding, lindex]
      col[*padding, lblank] = other[k][*padding, rmissing]
      [k, col]
    end
  end

  self.class.new(keys + left + right)
end

#sort_by!(*keys) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/object_table.rb', line 79

def sort_by!(*keys)
  sort_index = _get_sort_index(keys)

  columns.each do |k, v|
    columns[k] = v[sort_index]
  end
  self
end

#stack!(*others) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/object_table.rb', line 33

def stack!(*others)
  new_values = Hash[colnames.zip(ncols.times.map{[]})]

  others.each do |x|
    case x
    when ObjectTable::TableMethods
      x = x.columns
    when ObjectTable::BasicGrid
      x._ensure_uniform_columns!
    end

    raise "Don't know how to append a #{x.class}" unless x.is_a?(ObjectTable::BasicGrid)
    next if x.empty?
    raise 'Mismatch in column names' unless (colnames | x.keys) == (colnames & x.keys)

    x.each do |k, v|
      new_values[k].push NArray.to_na(v)
    end
  end

  return self if new_values.empty?
  new_rows = new_values.values.first.map{|x| x.shape[-1]}.reduce(:+)
  return self unless (new_rows and new_rows != 0)
  new_rows += nrows

  new_values.each do |k, v|
    @columns[k] = @columns[k].stack(*v)
  end
  self
end