summaryrefslogtreecommitdiff
path: root/lib/regression.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2016-03-15 17:40:40 +0100
committerChristoph Helma <helma@in-silico.ch>2016-03-15 17:40:40 +0100
commit7c3bd90c26dfeea2db3cf74a1cefc23d8dece7c0 (patch)
tree045d18b43e30ef3bf9a548230e45986b591535a6 /lib/regression.rb
parent0c5d2e678908a2d4aea43efbedbedc2c0439be30 (diff)
validation tests pass
Diffstat (limited to 'lib/regression.rb')
-rw-r--r--lib/regression.rb43
1 files changed, 23 insertions, 20 deletions
diff --git a/lib/regression.rb b/lib/regression.rb
index e0b109e..b8efd30 100644
--- a/lib/regression.rb
+++ b/lib/regression.rb
@@ -1,25 +1,23 @@
module OpenTox
module Algorithm
- # TODO add LOO errors
class Regression
def self.local_weighted_average compound, params
weighted_sum = 0.0
sim_sum = 0.0
- confidence = 0.0
neighbors = params[:neighbors]
neighbors.each do |row|
sim = row["tanimoto"]
- confidence = sim if sim > confidence # distance to nearest neighbor
- row["features"][params[:prediction_feature_id].to_s].each do |act|
- weighted_sum += sim*Math.log10(act)
- sim_sum += sim
+ if row["features"][params[:prediction_feature_id].to_s]
+ row["features"][params[:prediction_feature_id].to_s].each do |act|
+ weighted_sum += sim*Math.log10(act)
+ sim_sum += sim
+ end
end
end
- confidence = 0 if confidence.nan?
sim_sum == 0 ? prediction = nil : prediction = 10**(weighted_sum/sim_sum)
- {:value => prediction,:confidence => confidence}
+ {:value => prediction}
end
# TODO explicit neighbors, also for physchem
@@ -31,15 +29,18 @@ module OpenTox
weights = []
fingerprint_ids = neighbors.collect{|row| Compound.find(row["_id"]).fingerprint}.flatten.uniq.sort
+ #p neighbors
neighbors.each_with_index do |row,i|
neighbor = Compound.find row["_id"]
fingerprint = neighbor.fingerprint
- row["features"][params[:prediction_feature_id].to_s].each do |act|
- activities << Math.log10(act)
- weights << row["tanimoto"]
- fingerprint_ids.each_with_index do |id,j|
- fingerprints[id] ||= []
- fingerprints[id] << fingerprint.include?(id)
+ if row["features"][params[:prediction_feature_id].to_s]
+ row["features"][params[:prediction_feature_id].to_s].each do |act|
+ activities << Math.log10(act)
+ weights << row["tanimoto"]
+ fingerprint_ids.each_with_index do |id,j|
+ fingerprints[id] ||= []
+ fingerprints[id] << fingerprint.include?(id)
+ end
end
end
end
@@ -86,12 +87,14 @@ module OpenTox
neighbors.each_with_index do |row,i|
neighbor = Compound.find row["_id"]
- row["features"][params[:prediction_feature_id].to_s].each do |act|
- activities << Math.log10(act)
- weights << row["tanimoto"] # TODO cosine ?
- neighbor.physchem.each do |pid,v| # insert physchem only if there is an activity
- physchem[pid] ||= []
- physchem[pid] << v
+ if row["features"][params[:prediction_feature_id].to_s]
+ row["features"][params[:prediction_feature_id].to_s].each do |act|
+ activities << Math.log10(act)
+ weights << row["tanimoto"] # TODO cosine ?
+ neighbor.physchem.each do |pid,v| # insert physchem only if there is an activity
+ physchem[pid] ||= []
+ physchem[pid] << v
+ end
end
end
end