Skip to content
Newer
Older
100644 192 lines (140 sloc) 5.37 KB
8b68399 @jsvd Now returns empty hash if result set is empty even if axis is passed
authored Jun 5, 2009
1 require 'rubygems'
1111978 Initial Import
João Duarte authored Jan 28, 2009
2 require 'spec'
3 require 'sequel'
4
5 describe Sequel::Dataset do
6
7 before :all do
8
9 @DB = Sequel::sqlite
10
11 @DB.create_table :items do
12 text :name, :unique => true, :null => false
13 boolean :active, :default => true
14 column :price, :float
15 end
16
17 @DB.create_table :events do
18 float :value
19 float :ts
20 end
21
22 @items = @DB[:items]
23
24 @items << {:name => 'abc', :price => 97}
25 @items << {:name => 'def', :price => 11, :active => false}
26 @items << {:name => 'ghi', :price => 18}
27
28 @events = @DB[:events]
29
30 @events << {:value => 2.2, :ts => Time.local(2008,1,1,12,30).to_f}
31 @events << {:value => 1.1, :ts => Time.local(2008,1,1,13,30).to_f}
32 @events << {:value => 3.3, :ts => Time.local(2008,1,1,14,30).to_f}
33
34 end
35
36 it "doesn't complain when including in Sequel::Dataset class" do
37 lambda {
38 require 'lib/sequel_vectorized'
39 }.should_not raise_error
40 end
41
42 it "transforms numeric columns to narray" do
43
44 ret = @events.select(:value).vectorize
45 ret[:value].should be_an_instance_of NArray
46 ret[:value].should == NArray.to_na([2.2, 1.1, 3.3])
47
48 end
49
50 it "creates narray of floats for any kind of numeric array" do
51
52 ret = @events.vectorize
53 ret.each {|k,v| v.should be_an_instance_of NArray }
54 ret.should == {
55 :value => NArray.to_na([2.2, 1.1, 3.3]),
56 :ts => NArray.to_na([1199190600.0, 1199194200.0, 1199197800.0])
57 }
58
59 end
60
61 it "creates narray of bytes from an array of booleans" do
62
63 ret = @items.select(:active).vectorize
64 ret[:active].should == NArray.to_na([1,0,1])
65
66 end
67
68 # spec an example filter
69 it "is possible to vectorize the result set of a filter" do
70 @events.filter(:value > 2).vectorize.should == {
71 :value => NArray.to_na([2.2, 3.3]),
72 :ts => NArray.to_na([Time.local(2008,1,1,12,30).to_f, Time.local(2008,1,1,14,30).to_f])
73 }
74 end
75
ac43fc2 Added axis option. Useful for future interpolation of data.
João Duarte authored Jan 29, 2009
76 it "is possible to pass an :axis option" do
77 axis = {
78 :column => :ts,
79 :step => 60,
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
80 :range => Time.local(2008,1,1,12).to_f ... Time.local(2008,1,1,15).to_f,
ac43fc2 Added axis option. Useful for future interpolation of data.
João Duarte authored Jan 29, 2009
81 :interpolate => true
82 }
83
84 lambda {
85 @events.vectorize axis
86 }.should_not raise_error
87 end
88
89 it "returns new narrays of size (range.last-range.first)/step if :axis is given" do
90
91 axis = {
92 :column => :ts,
93 :step => 60,
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
94 :range => Time.local(2008,1,1,12).to_f ... Time.local(2008,1,1,15).to_f,
ac43fc2 Added axis option. Useful for future interpolation of data.
João Duarte authored Jan 29, 2009
95 :interpolate => false
96 }
97
98 ret = @events.vectorize :axis => axis
99
8b68399 @jsvd Now returns empty hash if result set is empty even if axis is passed
authored Jun 5, 2009
100 new_ret = ret.delete_if {|k,v| k.to_s.match(/__\w+/) }
101
102 new_ret.should == {
ac43fc2 Added axis option. Useful for future interpolation of data.
João Duarte authored Jan 29, 2009
103 :value => NArray.to_na([[0]*30, 2.2, [0]*59, 1.1, [0]*59,3.3, [0]*29].flatten),
104 :ts => NArray.float(180).indgen(Time.local(2008,1,1,12).to_f,60)
105 }
106 end
107
6f697b5 Interpolation by axis now works. Depends ruby wrapper to GSL
João Duarte authored Jan 30, 2009
108 it "interpolates NArrays if :axis option :interpolate is true" do
109 # TODO: not spec'ed
110 axis = {
111 :column => :ts,
112 :step => 60,
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
113 :range => Time.local(2008,1,1,12).to_f ... Time.local(2008,1,1,15).to_f,
6f697b5 Interpolation by axis now works. Depends ruby wrapper to GSL
João Duarte authored Jan 30, 2009
114 :interpolate => true
115 }
116
117 ret = @events.vectorize :axis => axis
118
119 ret[:ts].should == NArray.float(180).indgen(Time.local(2008,1,1,12).to_f,60)
120
121 end
6d4e64b Added dot notation
João Duarte authored Jan 30, 2009
122
123
124 it "returns a vectorized result set that inherits from Hash and has dot notation" do
125
126 axis = {
127 :column => :ts,
128 :step => 60,
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
129 :range => Time.local(2008,1,1,12).to_f ... Time.local(2008,1,1,15).to_f,
6d4e64b Added dot notation
João Duarte authored Jan 30, 2009
130 :interpolate => false
131 }
132
133 ret = @events.vectorize :axis => axis
134
135 ret.should be_a_kind_of Hash
136
137 ret.should respond_to :ts
138 ret.should respond_to :value
139
140 end
8b68399 @jsvd Now returns empty hash if result set is empty even if axis is passed
authored Jun 5, 2009
141
142 it "should be empty if there is no data" do
143 @items.filter(:name => 'be').vectorize.should be_empty
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
144 @items.filter(:name => 'be').vectorize(:axis => {:column => :price, :range => 0 ... 100, :step => 20 }).should be_empty
145 @items.filter(:name => 'abc').vectorize(:axis => {:column => :price, :range => 0 ... 100, :step => 20 }).should_not be_empty
146 end
147
c72e34c @jsvd Prevent exception if trying to interpolate insufficient data
authored Aug 25, 2009
148 it "should not raise error if there's not enough data for interpolation" do
149
150 axis = {
151 :column => :ts,
152 :step => 60,
153 :range => Time.local(2008,1,1,12,30).to_f .. Time.local(2008,1,1,12,31).to_f,
154 :interpolate => true
155 }
156
157 lambda { @events.vectorize :axis => axis }.should_not raise_error
158 @events.vectorize(:axis => axis).should include :__value
159 @events.vectorize(:axis => axis).should include :__raw_mask
160 @events.vectorize(:axis => axis)[:__value].size.should == 1
161 @events.vectorize(:axis => axis)[:__raw_mask].size.should == 2
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
162
c72e34c @jsvd Prevent exception if trying to interpolate insufficient data
authored Aug 25, 2009
163 end
164
165 it "should not raise error for an interval" do
69d84e7 @jsvd Fixed open/close end range handling
authored Aug 24, 2009
166
167 @events << {:value => 2.2, :ts => Time.local(2009,8,14,21,0).to_f}
168 @events << {:value => 2.2, :ts => Time.local(2009,8,14,22,0).to_f}
169 @events << {:value => 1.1, :ts => Time.local(2009,8,14,23,20).to_f}
170 @events << {:value => 3.3, :ts => Time.local(2009,8,15,0).to_f}
171 @events << {:value => 3.3, :ts => Time.local(2009,8,15,1).to_f}
172
173 axis = {
174 :column => :ts,
175 :step => 60,
176 :range => Time.local(2009,8,14,22).to_f .. Time.local(2009,8,15,0).to_f,
177 :interpolate => false
178 }
179
180 lambda { @events.vectorize :axis => axis }.should_not raise_error
181 axis[:interpolate] = true
182 lambda { @events.vectorize :axis => axis }.should_not raise_error
183
184 axis[:range] = Time.local(2009,8,14,22).to_f .. Time.local(2009,8,15,0).to_f
185
186 lambda { @events.vectorize :axis => axis }.should_not raise_error
187 axis[:interpolate] = false
188 lambda { @events.vectorize :axis => axis }.should_not raise_error
189
8b68399 @jsvd Now returns empty hash if result set is empty even if axis is passed
authored Jun 5, 2009
190 end
1111978 Initial Import
João Duarte authored Jan 28, 2009
191 end
Something went wrong with that request. Please try again.