object_table 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 456118e9dd3562463800a5598c5155868025e8ff
4
- data.tar.gz: c5a2853e7d278c7f8a910a8f0416d7773a90fad7
3
+ metadata.gz: fcf93afcb7a81c3a9fb40c8ffc7d8b1bd506c845
4
+ data.tar.gz: 9d6a155f502215a3f57639108bfe908ea4e853f8
5
5
  SHA512:
6
- metadata.gz: 15fd49613ca84147fdccf70b7dfd8ec95432960bac7a3835cbca0a52e2de73743bdda7c2b050a61714172a8a21644c66e29ba50f5a2ccb8b8e1eadb1a78a0d0a
7
- data.tar.gz: 965062babf9d465060d58e769968c11304b6982569c41aad05d6e7a62ce5a8a30ce66260e102ebb01f5154319887ec6a30d521947e1c6bb24ee57514de7b6591
6
+ metadata.gz: 0a4a23f3e527ee524e2d528637ed939caaa0e48bde230df277cfaccbb50a4f62824081be6990fd3307cc5108f485066ecb05f2d79563999b6365154ab04151e7
7
+ data.tar.gz: b23605bf82b78fb04fce0a4f0f4a8f6e049a2f3b15fbf3c9ee2029f71370f9b0fc2e064269edbe7bbb110cfa6a79ca8d3acfb2450292efe36df34a0c4676a23e
@@ -14,9 +14,9 @@ class ObjectTable::Grouped
14
14
 
15
15
  def _groups
16
16
  names, keys = _keys()
17
- groups = (0...@parent.nrows).zip(keys).group_by{|row, key| key}
17
+ groups = keys.length.times.group_by{|i| keys[i]}
18
18
  groups.each do |k, v|
19
- groups[k] = NArray.to_na(v.map(&:first))
19
+ groups[k] = NArray.to_na(v)
20
20
  end
21
21
  [names, groups]
22
22
  end
@@ -36,23 +36,24 @@ class ObjectTable::Grouped
36
36
  [names, keys]
37
37
  end
38
38
 
39
- %w{ all? any? collect collect_concat count flat_map map none? one? }.each do |method|
40
- define_method(method) do |*args, &block|
41
- names, groups = _groups()
42
- groups.send(method, *args) do |k, v|
39
+ def each(&block)
40
+ names, groups = _groups()
41
+ if block
42
+ groups.each do |k, v|
43
43
  keys = names.zip(k)
44
44
  __group_cls__.new(@parent, Hash[keys], v).apply &block
45
45
  end
46
+ return @parent
46
47
  end
47
- end
48
48
 
49
- def each(&block)
50
- names, groups = _groups()
51
- groups.each do |k, v|
52
- keys = names.zip(k)
53
- __group_cls__.new(@parent, Hash[keys], v).apply &block
49
+ Enumerator.new(groups.length) do |y|
50
+ groups.each do |k, v|
51
+ keys = names.zip(k)
52
+ y.yield __group_cls__.new(@parent, Hash[keys], v)
53
+ end
54
+ @parent
54
55
  end
55
- @parent
56
+
56
57
  end
57
58
 
58
59
  def apply(&block)
@@ -1,3 +1,3 @@
1
1
  class ObjectTable
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -27,8 +27,7 @@ class ObjectTable::View
27
27
  end
28
28
 
29
29
  def clone
30
- cols = ObjectTable::BasicGrid[@parent.columns.map{|k, v| [k, v[indices]]}]
31
- __table_cls__.new(cols)
30
+ __table_cls__.new(columns)
32
31
  end
33
32
 
34
33
  def inspect(*args)
data/lib/object_table.rb CHANGED
@@ -46,6 +46,7 @@ class ObjectTable
46
46
  raise 'Mismatch in column names' unless (colnames | x.keys) == (colnames & x.keys)
47
47
 
48
48
  x.each do |k, v|
49
+ v = v.to_a if v.is_a?(Range)
49
50
  new_values[k].push NArray.to_na(v)
50
51
  end
51
52
  end
@@ -53,7 +54,6 @@ class ObjectTable
53
54
  return self if new_values.empty?
54
55
  new_rows = new_values.values.first.map{|x| x.shape[-1]}.reduce(:+)
55
56
  return self unless (new_rows and new_rows != 0)
56
- new_rows += nrows
57
57
 
58
58
  new_values.each do |k, v|
59
59
  @columns[k] = @columns[k].stack(*v)
@@ -89,35 +89,36 @@ class ObjectTable
89
89
  type = (options[:type] || 'inner')
90
90
  key = [key] unless key.is_a?(Array)
91
91
 
92
+ right_cols = other.colnames - key
93
+ left_cols = colnames
94
+
92
95
  lkeys = key.map{|k| get_column(k).to_a}.transpose
93
96
  rkeys = key.map{|k| other[k].to_a}.transpose
94
97
 
95
- rgroups = rkeys.each_with_index.group_by(&:first)
96
- rgroups.each{|k, v| rgroups[k] = v.transpose[-1]}
97
-
98
+ rgroups = rkeys.length.times.group_by{|i| rkeys[i]}
98
99
  rindex = rgroups.values_at(*lkeys)
99
- lindex = lkeys.each_with_index.zip(rindex).flat_map{|(k, i), r| [i] * r.length if r}
100
- lindex.compact!
101
-
102
- right_cols = other.colnames - key
103
- left_cols = colnames
104
100
 
105
101
  if type == 'left' or type == 'outer'
106
- lmissing = NArray.to_na(rindex.map{|x| x ? 0 : 1}).where.to_a
102
+ common, lmissing = rindex.each_with_index.partition(&:first)
103
+ lmissing = lmissing.transpose[-1]
104
+ lindex = common.flat_map{|r, i| [i] * r.length}
105
+ rindex.flatten!.compact!
106
+
107
107
  lindex += lmissing
108
108
  rindex += [-1] * lmissing.length
109
+ else
110
+ lindex = rindex.each_with_index.flat_map{|r, i| [i] * r.length if r}.compact
111
+ rindex.flatten!.compact!
109
112
  end
110
113
 
114
+
111
115
  if type == 'right' or type == 'outer'
112
116
  left_cols -= key
113
- rmissing = rgroups.values - rindex
114
- rmissing.flatten!
117
+ rmissing = (0...other.nrows).to_a - rindex
115
118
  lindex += [-1] * rmissing.length
116
119
  rindex += rmissing
117
120
  end
118
121
 
119
- rindex.flatten!.compact!
120
-
121
122
  lindex = NArray.to_na(lindex)
122
123
  rindex = NArray.to_na(rindex)
123
124
  lblank = lindex.eq(-1)
@@ -161,6 +161,21 @@ describe ObjectTable::Grouped do
161
161
 
162
162
  expect(keys).to match_array(correct_keys)
163
163
  end
164
+
165
+ context 'with no block' do
166
+ it 'should return an enumerator' do
167
+ expect(grouped.each).to be_a Enumerator
168
+ end
169
+
170
+ it 'should enumerate the groups' do
171
+ groups = [even_group, odd_group]
172
+ grouped.each.each do |group|
173
+ expect(groups).to include group
174
+ groups -= [group]
175
+ end
176
+ end
177
+
178
+ end
164
179
  end
165
180
 
166
181
  describe '#apply' do
@@ -236,54 +251,4 @@ describe ObjectTable::Grouped do
236
251
  end
237
252
  end
238
253
 
239
- describe 'enumerators' do
240
- shared_examples 'enumerator method' do |method, *args, block, _expected|
241
- let(:expected){ _expected }
242
-
243
- describe "##{method}" do
244
-
245
- it 'should work with a block without args' do
246
- result = grouped.send(method, *args){ block.call(self) }
247
- if result.is_a? Array
248
- expect(result).to match_array expected
249
- else
250
- expect(result).to eql expected
251
- end
252
- end
253
-
254
- it 'should work with a block with args' do
255
- result = grouped.send(method, *args, &block)
256
- if result.is_a? Array
257
- expect(result).to match_array expected
258
- else
259
- expect(result).to eql expected
260
- end
261
- end
262
-
263
- end
264
-
265
- end
266
-
267
- it_behaves_like 'enumerator method', 'all?', lambda{|grp| grp.col1[0] == 1}, false
268
- it_behaves_like 'enumerator method', 'any?', lambda{|grp| grp.col1[0] == 1}, true
269
-
270
- it_behaves_like 'enumerator method', 'collect', lambda{|grp| grp.nrows} do
271
- let(:expected){ [even.length, odd.length] }
272
- end
273
- it_behaves_like 'enumerator method', 'map', lambda{|grp| grp.nrows} do
274
- let(:expected){ [even.length, odd.length] }
275
- end
276
-
277
- it_behaves_like 'enumerator method', 'collect_concat', lambda{|grp| grp.col1.to_a} do
278
- let(:expected){ table.col1 }
279
- end
280
- it_behaves_like 'enumerator method', 'flat_map', lambda{|grp| grp.col1.to_a} do
281
- let(:expected){ table.col1 }
282
- end
283
-
284
- it_behaves_like 'enumerator method', 'count', lambda{|grp| grp.col1[0] == 1}, 1
285
- it_behaves_like 'enumerator method', 'none?', lambda{|grp| grp.col1[0] > 100}, true
286
- it_behaves_like 'enumerator method', 'one?', lambda{|grp| grp.col1[0] < 100}, false
287
- end
288
-
289
254
  end
@@ -218,7 +218,7 @@ describe ObjectTable do
218
218
  ObjectTable.new(col1: [1, 2, 3], col2: 5),
219
219
  ObjectTable.new(col1: 10, col2: 50),
220
220
  ObjectTable.new(col2: [10, 30], col1: 15).where{col2.eq 10},
221
- ObjectTable::BasicGrid[col2: [1, 2], col1: [3, 4]],
221
+ ObjectTable::BasicGrid[col2: [1, 2], col1: 3..4],
222
222
  ]
223
223
  end
224
224
 
@@ -345,7 +345,7 @@ EOS
345
345
  end
346
346
  end
347
347
 
348
- describe '.clone' do
348
+ describe '#clone' do
349
349
  let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: 5) }
350
350
  let(:clone){ subject.clone }
351
351
 
@@ -360,10 +360,18 @@ EOS
360
360
 
361
361
  it 'should have cloned columns' do
362
362
  subject.columns.each do |k, v|
363
- expect(clone.columns[k].to_a).to eql v.to_a
363
+ expect(clone.columns[k]).to eq v
364
364
  expect(clone.columns[k]).to_not be v
365
365
  end
366
366
  end
367
+
368
+ context 'with matrixy columns' do
369
+ let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: NArray.float(10, 3).random!) }
370
+
371
+ it 'should be equivalent to the original table' do
372
+ expect(clone).to eql subject
373
+ end
374
+ end
367
375
  end
368
376
 
369
377
  describe '#sort_by' do
@@ -110,7 +110,7 @@ RSpec.shared_examples 'a table view' do |cls|
110
110
  end
111
111
 
112
112
  context 'with an NArray' do
113
- let(:value){ NArray.int(3, 4, view.nrows).random! }
113
+ let(:value){ NArray.float(3, 4, view.nrows).random! }
114
114
 
115
115
  it 'should use the narray parameters' do
116
116
  subject
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cheney Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-16 00:00:00.000000000 Z
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: narray