summaryrefslogtreecommitdiff
path: root/lib/regression.rb
blob: c4c83d2516ad2b14c0e5c98b9ed78da3be9fee12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module OpenTox
  module Algorithm
    
    class Regression

      def self.local_weighted_average substance:, neighbors:
        weighted_sum = 0.0
        sim_sum = 0.0
        neighbors.each do |neighbor|
          sim = neighbor["similarity"]
          activities = neighbor["measurements"]
          activities.each do |act|
            weighted_sum += sim*act
            sim_sum += sim
          end if activities
        end
        sim_sum == 0 ? prediction = nil : prediction = weighted_sum/sim_sum
        {:value => prediction}
      end

      def self.local_fingerprint_regression substance:, neighbors:, method: pls#, method_params="sigma=0.05"
        values = []
        fingerprints = {}
        weights = []
        fingerprint_ids = neighbors.collect{|n| Compound.find(n["_id"]).fingerprint}.flatten.uniq.sort

        neighbors.each do |n|
          fingerprint = Substance.find(n["_id"]).fingerprint
          activities = n["measurements"]
          activities.each do |act|
            values << act
            weights << n["similarity"]
            fingerprint_ids.each do |id|
              fingerprints[id] ||= []
              fingerprints[id] << fingerprint.include?(id) 
            end
          end if activities
        end

        variables = []
        data_frame = [values]

        fingerprints.each do |k,v| 
          unless v.uniq.size == 1
            data_frame << v.collect{|m| m ? "T" : "F"}
            variables << k
          end
        end

        if variables.empty?
          prediction = local_weighted_average substance, neighbors
          prediction[:warning] = "No variables for regression model. Using weighted average of similar substances."
          prediction
        else
          substance_features = variables.collect{|f| substance.fingerprint.include?(f) ? "T" : "F"} 
          prediction = r_model_prediction method, data_frame, variables, weights, substance_features
          if prediction.nil? or prediction[:value].nil?
            prediction = local_weighted_average substance, neighbors
            prediction[:warning] = "Could not create local PLS model. Using weighted average of similar substances."
            prediction
          else
            prediction[:prediction_interval] = [prediction[:value]-1.96*prediction[:rmse], prediction[:value]+1.96*prediction[:rmse]]
            prediction[:value] = prediction[:value]
            prediction[:rmse] = prediction[:rmse]
            prediction
          end
        end
      
      end

      def self.local_physchem_regression substance:, neighbors:, method: pls

        activities = []
        weights = []
        pc_ids = neighbors.collect{|n| n["common_descriptors"].collect{|d| d[:id]}}.flatten.uniq.sort
        data_frame = []
        data_frame[0] = []
        
        neighbors.each_with_index do |n,i|
          activities = n["measurements"]
          activities.each do |act|
            data_frame[0][i] = act
            weights << n["similarity"]
            n["common_descriptors"].each do |d| 
              j = pc_ids.index(d[:id])+1
              data_frame[j] ||= []
              data_frame[j][i] = d[:scaled_value]
            end
          end if activities
          (0..pc_ids.size).each do |j| # for R: fill empty values with NA
            data_frame[j] ||= []
            data_frame[j][i] ||= "NA"
          end
        end

        data_frame = data_frame.each_with_index.collect do |r,i|
          if r.uniq.size == 1 # remove properties with a single value 
            r = nil
            pc_ids[i-1] = nil # data_frame frame has additional activity entry
          end
          r
        end
        data_frame.compact!
        pc_ids.compact!

        if pc_ids.empty?
          prediction = local_weighted_average substance, neighbors
          prediction[:warning] = "No relevant variables for regression model. Using weighted average of similar substances."
          prediction
        else
          query_descriptors = pc_ids.collect { |i| substance.scaled_values[i] }
          query_descriptors = query_descriptors.each_with_index.collect do |v,i|
            unless v
              v = nil
              data_frame[i] = nil
              pc_ids[i] = nil
            end
            v
          end
          query_descriptors.compact!
          data_frame.compact!
          pc_ids.compact!
          prediction = r_model_prediction method, data_frame, pc_ids.collect{|i| "\"#{i}\""}, weights, query_descriptors
          if prediction.nil?
            prediction = local_weighted_average substance, neighbors
            prediction[:warning] = "Could not create local PLS model. Using weighted average of similar substances."
          end
          p prediction
          prediction
        end
      
      end

      def self.r_model_prediction method, training_data, training_features, training_weights, query_feature_values
        R.assign "weights", training_weights
        r_data_frame = "data.frame(#{training_data.collect{|r| "c(#{r.join(',')})"}.join(', ')})"
=begin
rlib = File.expand_path(File.join(File.dirname(__FILE__),"..","R"))
        File.open("tmp.R","w+"){|f|
          f.puts "suppressPackageStartupMessages({
  library(iterators,lib=\"#{rlib}\")
  library(foreach,lib=\"#{rlib}\")
  library(ggplot2,lib=\"#{rlib}\")
  library(grid,lib=\"#{rlib}\")
  library(gridExtra,lib=\"#{rlib}\")
  library(pls,lib=\"#{rlib}\")
  library(caret,lib=\"#{rlib}\")
  library(doMC,lib=\"#{rlib}\")
  registerDoMC(#{NR_CORES})
})"

          f.puts "data <- #{r_data_frame}\n"
          f.puts "weights <- c(#{training_weights.join(', ')})"
          f.puts "features <- c(#{training_features.join(', ')})"
          f.puts "names(data) <- append(c('activities'),features)" #
          f.puts "ctrl <- rfeControl(functions = #{method}, method = 'repeatedcv', repeats = 5, verbose = T)"
          f.puts "lmProfile <- rfe(activities ~ ., data = data, rfeControl = ctrl)"

          f.puts "model <- train(activities ~ ., data = data, method = '#{method}')"
          f.puts "fingerprint <- data.frame(rbind(c(#{query_feature_values.join ','})))"
          f.puts "names(fingerprint) <- features" 
          f.puts "prediction <- predict(model,fingerprint)"
        }
=end
        
        R.eval "data <- #{r_data_frame}"
        R.assign "features", training_features
        begin
          R.eval "names(data) <- append(c('activities'),features)" #
          R.eval "model <- train(activities ~ ., data = data, method = '#{method}', na.action = na.pass, allowParallel=TRUE)"
          R.eval "fingerprint <- data.frame(rbind(c(#{query_feature_values.join ','})))"
          R.eval "names(fingerprint) <- features" 
          R.eval "prediction <- predict(model,fingerprint)"
          value = R.eval("prediction").to_f
          rmse = R.eval("getTrainPerf(model)$TrainRMSE").to_f
          r_squared = R.eval("getTrainPerf(model)$TrainRsquared").to_f
          prediction_interval = value-1.96*rmse, value+1.96*rmse
          {
            :value => value,
            :rmse => rmse,
            :r_squared => r_squared,
            :prediction_interval => prediction_interval
          }
        rescue 
          return nil
        end
      end

    end
  end
end