Class: Quandl::Operation::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/operation/transform.rb

Class Method Summary collapse

Class Method Details

.perform(data, type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/quandl/operation/transform.rb', line 7

def perform( data, type )
  # original order
  order = Parse.sort_order?(data)
  # operations expect data in ascending order
  data = Parse.sort( data, :asc )
  # operations expect float values
  data = Parse.values_to_float(data)
  # transform
  data = transform_and_log( data, type )
  # return to original order
  data = Parse.sort( data, :desc ) if order == :desc
  # onwards
  data
end

.transform(data, type) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
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
155
156
157
158
159
160
161
162
163
# File 'lib/quandl/operation/transform.rb', line 41

def transform( data, type)
  return data if data.blank?
  #Transforms table from actual data points
  #to differences between points (:diff)
  #or a ratio between points(:rdiff)
  #or a ratio between the latest point and an earlier point (:rdiff_from)
  #If type is other than these two, nothing is done.

  # ensure that type is in the expected format
  type = type.try(:to_sym)
  # nothing to do unless valid transform
  return data unless valid_transformation?( type )

  temparr = Array.new
  #first make a keylist
  keylist = data.transpose.first
  # now sort the keylist from oldest to newest
  # unless there is only one point
  if keylist.count > 1
    keylist = keylist.reverse if keylist[0] > keylist[1] # better performance if we do this first
    keylist.sort!
  end

  #find number of columns
  numcols = data.first.size - 1
  if type == :normalize
    divisor = Array.new(numcols,nil)
    0.upto(keylist.length - 1) do |i|
      temparr[i] = []
      curr_row = data[i][1..-1]
      0.upto(numcols-1) do |x|
        if curr_row[x].nil?
          temparr[i][x] = nil
        elsif divisor[x].nil?
          if curr_row[x].to_f != 0 
            divisor[x] = curr_row[x].to_f
            temparr[i][x] = 100.0
          else
            temparr[i][x] = 0
          end
        else
          temparr[i][x] = curr_row[x] / divisor[x] * 100.0
        end
      end
    end
    0.upto(keylist.length-1) do |i|
      data[i] = [keylist[i], temparr[i]].flatten
    end
  elsif [:diff, :rdiff].include? type
    #now build temparr
    1.upto(keylist.length - 1) do |i|
      temparr[i] = []
      curr_row = data[i][1..-1]
      prev_row = data[i-1][1..-1]
      0.upto(numcols-1) do |x| 
        if type == :diff
          if !curr_row[x].nil? and !prev_row[x].nil?
            temparr[i][x] = Float(curr_row[x]) - Float(prev_row[x])
          else
            temparr[i][x] = nil
          end
        else
          if !curr_row[x].nil? and !prev_row[x].nil? and prev_row[x] != 0
            temparr[i][x] = ( Float(curr_row[x]) - Float(prev_row[x]) ) / Float( prev_row[x] )
          else
            temparr[i][x] = nil
          end
        end
      end
    end

    #now put temparr into datapac
    1.upto(keylist.length-1) do |i|
      data[i] = [keylist[i], temparr[i]].flatten
    end

    #delete the first date in datapac (because there is no diff for that)
    data.delete_at(0)
  elsif type == :rdiff_from
    num_rows = keylist.length - 1 
    initial = Array.new(numcols,nil)
    num_rows.downto(0) do |i|
      temparr[i] = []
      curr_row = data[i][1..-1]
      0.upto(numcols-1) do |x|
        if curr_row[x].nil?
          temparr[i][x] = nil
        elsif initial[x].nil?
          initial[x] = curr_row[x]
          temparr[i][x] = 0.0
        elsif curr_row[x] == 0
          temparr[i][x] = nil
        else
          temparr[i][x] = ( Float(initial[x]) - Float(curr_row[x]) ) / Float(curr_row[x]) 
        end
      end
    end


    0.upto(keylist.length-1) do |i|
      data[i] = [keylist[i], temparr[i]].flatten
    end
  else
    cumulsum = Array.new(numcols,0)
    sumstarted = Array.new(numcols,false)
    #now build temparr
    0.upto(keylist.length - 1) do |i|
      temparr[i] = []
      curr_row = data[i][1..-1]
      0.upto(numcols-1) do |x|
        if !curr_row[x].nil?
          cumulsum[x] = cumulsum[x] + curr_row[x]
          sumstarted[x] = true
        end
        temparr[i][x] = cumulsum[x] if sumstarted[x]
      end
    end
    0.upto(keylist.length-1) do |i|
      data[i] = [keylist[i],temparr[i]].flatten
    end
  end
  data
end

.transform_and_log(data, type) ⇒ Object



22
23
24
25
26
27
# File 'lib/quandl/operation/transform.rb', line 22

def transform_and_log( data, type)
  t1 = Time.now
  r = transform( data, type)
  Quandl::Logger.debug "#{self.name}.perform(#{data.try(:count)} rows, #{type}) (#{t1.elapsed.microseconds}ms)" if t1.elapsed.microseconds > 1
  r
end

.valid?(type) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/quandl/operation/transform.rb', line 33

def valid?(type)
  valid_transformations.include?( type.try(:to_sym) )
end

.valid_transformation?(type) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/quandl/operation/transform.rb', line 29

def valid_transformation?(type)
  valid?(type)
end

.valid_transformationsObject



37
38
39
# File 'lib/quandl/operation/transform.rb', line 37

def valid_transformations
  [ :diff, :rdiff, :cumul, :normalize, :rdiff_from ]
end