From d9c9d78e49d886ea91386adbbd2b523347df226e Mon Sep 17 00:00:00 2001 From: "helma@in-silico.ch" Date: Mon, 29 Oct 2018 20:34:39 +0100 Subject: dataset predictions fixed --- lib/dataset.rb | 23 +++++++++++++++++++++++ lib/feature.rb | 12 ++++++++++++ lib/model.rb | 3 ++- lib/validation-statistics.rb | 3 +-- 4 files changed, 38 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/dataset.rb b/lib/dataset.rb index 41d7b5c..78f5633 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -86,6 +86,10 @@ module OpenTox features.select{|f| f._type.match("SubstanceProperty")} end + def prediction_features + features.select{|f| f._type.match("Prediction")} + end + # Writers # Add a value for a given substance and feature @@ -352,6 +356,25 @@ module OpenTox sdf end + def predictions + predictions = {} + substances.each do |s| + predictions[s] ||= {} + prediction_feature = prediction_features.first + predictions[s][:value] = values(s,prediction_feature).first + predictions[s][:warnings] = [] + warnings_features.each { |w| predictions[s][:warnings] += values(s,w) } + if predictions[s][:value] and prediction_feature.is_a? NominalLazarPrediction + prediction_feature.accept_values.each do |v| + f = LazarPredictionProbability.find_by(:name => v, :model_id => prediction_feature.model_id, :training_feature_id => prediction_feature.training_feature_id) + predictions[s][:probabilities] ||= {} + predictions[s][:probabilities][v] = values(s,f).first + end + end + end + predictions + end + # Dataset operations # Merge an array of datasets diff --git a/lib/feature.rb b/lib/feature.rb index be07e7a..c18b0b8 100644 --- a/lib/feature.rb +++ b/lib/feature.rb @@ -54,18 +54,30 @@ module OpenTox class NominalLazarPrediction < NominalFeature field :model_id, type: BSON::ObjectId field :training_feature_id, type: BSON::ObjectId + def name + "#{self[:name]} Prediction" + end end class LazarPredictionProbability < NominalLazarPrediction + def name + "probability(#{self[:name]})" + end end # Numeric lazar prediction class NumericLazarPrediction < NumericFeature field :model_id, type: BSON::ObjectId field :training_feature_id, type: BSON::ObjectId + def name + "#{name} Prediction" + end end class LazarPredictionInterval < NumericLazarPrediction + def name + "prediction_interval_#{self[:name]}" + end end class NominalSubstanceProperty < NominalFeature diff --git a/lib/model.rb b/lib/model.rb index fc98e09..7eaa469 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -346,7 +346,8 @@ module OpenTox # add predictions to dataset predictions.each do |substance_id,p| - d.add substance_id,warning_feature,p[:warnings].join(" ") if p[:warnings] + substance_id = BSON::ObjectId.from_string(substance_id) + d.add substance_id,warning_feature,p[:warnings].join(" ") unless p[:warnings].empty? unless p[:value].nil? d.add substance_id,f,p[:value] p[:probabilities].each {|name,p| d.add substance_id,probability_features[name],p} if p[:probabilities] diff --git a/lib/validation-statistics.rb b/lib/validation-statistics.rb index e440731..7bae891 100644 --- a/lib/validation-statistics.rb +++ b/lib/validation-statistics.rb @@ -150,8 +150,7 @@ module OpenTox y = {:all => [],:without_warnings => []} self.nr_predictions = {:all =>0,:without_warnings => 0} predictions.each do |cid,pred| - p pred - if pred[:value] and pred[:measurements] + !if pred[:value] and pred[:measurements] and !pred[:measurements].empty? self.nr_predictions[:all] +=1 x[:all] << pred[:measurements].median y[:all] << pred[:value] -- cgit v1.2.3