Skip to content

Commit

Permalink
Added axis option. Useful for future interpolation of data.
Browse files Browse the repository at this point in the history
  • Loading branch information
João Duarte committed Jan 29, 2009
1 parent e59fd5c commit ac43fc2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/sequel_vectorized.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'narray'

class Sequel::Dataset
def vectorize
def vectorize options={}

result = {}
axis = options[:axis]

# transform dataset to hash of arrays
map {|row| row.each{|att,value| (result[att] ||= []) << value}}
Expand All @@ -24,6 +25,31 @@ def vectorize
result[k][] = v.map {|i| (i == true) ? 1 : 0 }

end

end

axis ? _process(result, axis) : result

end

private
def _process data, axis
column = axis[:column]
range = axis[:range]
step = axis[:step]

data_places = data[column]/step.to_f - range.first/step.to_f
data[column] = NArray.float((range.last - range.first)/step.to_f).indgen!(range.first,60)

data.each do |k,v|
next if k == column
if v.kind_of? NArray and v[0].kind_of? Numeric then
data[k] = NArray.float((range.last - range.first)/step.to_f)
data[k][data_places] = v
end
end

data
end

end
30 changes: 30 additions & 0 deletions spec/sequel_vectorized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,34 @@
}
end

it "is possible to pass an :axis option" do
axis = {
:column => :ts,
:step => 60,
:range => [Time.local(2008,1,1,12).to_f, Time.local(2008,1,1,15).to_f],
:interpolate => true
}

lambda {
@events.vectorize axis
}.should_not raise_error
end

it "returns new narrays of size (range.last-range.first)/step if :axis is given" do

axis = {
:column => :ts,
:step => 60,
:range => [Time.local(2008,1,1,12).to_f, Time.local(2008,1,1,15).to_f],
:interpolate => false
}

ret = @events.vectorize :axis => axis

ret.should == {
:value => NArray.to_na([[0]*30, 2.2, [0]*59, 1.1, [0]*59,3.3, [0]*29].flatten),
:ts => NArray.float(180).indgen(Time.local(2008,1,1,12).to_f,60)
}
end

end

0 comments on commit ac43fc2

Please sign in to comment.