diff options
author | mguetlein <martin.guetlein@gmail.com> | 2011-02-03 18:14:20 +0100 |
---|---|---|
committer | mguetlein <martin.guetlein@gmail.com> | 2011-02-03 18:14:20 +0100 |
commit | aca886bf82e7ebf5a9d76642614418ed983ab45e (patch) | |
tree | bdd9b22fccfff637dddd0b10d4800a8e41fe8382 | |
parent | 7906047a423d94692581ac6aa15295924c8b89db (diff) |
adding weighted_accuracy, change accuracy computation, some minor changes
-rwxr-xr-x | application.rb | 2 | ||||
-rwxr-xr-x | example.rb | 2 | ||||
-rwxr-xr-x | lib/ot_predictions.rb | 4 | ||||
-rwxr-xr-x | lib/predictions.rb | 21 | ||||
-rwxr-xr-x | lib/validation_db.rb | 3 | ||||
-rwxr-xr-x | reach_reports/reach_service.rb | 14 | ||||
-rwxr-xr-x | reach_reports/reach_test.rb | 10 | ||||
-rwxr-xr-x | report/report_factory.rb | 2 | ||||
-rwxr-xr-x | report/validation_access.rb | 1 | ||||
-rwxr-xr-x | test/test_examples.rb | 82 | ||||
-rwxr-xr-x | test/test_examples_util.rb | 60 | ||||
-rwxr-xr-x | validation/validation_application.rb | 4 | ||||
-rwxr-xr-x | validation/validation_format.rb | 2 | ||||
-rwxr-xr-x | validation/validation_test.rb | 91 |
14 files changed, 221 insertions, 77 deletions
diff --git a/application.rb b/application.rb index 319a95d..c18b05e 100755 --- a/application.rb +++ b/application.rb @@ -13,8 +13,6 @@ end require "example.rb" -# this allows GET access to all validation URIS that do NOT end with /<number> or /<number>/ -OpenTox::Authorization.whitelist( /\/[0-9]+(\/?)$/, "GET", true ) get '/examples/?' do LOGGER.info "list examples" @@ -124,7 +124,7 @@ class Example log "done" @@summary end - task.uri + return_task(task) end # performs all curl calls listed in examples after ">>>", next line is added if line ends with "\" diff --git a/lib/ot_predictions.rb b/lib/ot_predictions.rb index e28f35b..b0e9720 100755 --- a/lib/ot_predictions.rb +++ b/lib/ot_predictions.rb @@ -165,7 +165,9 @@ module Lib end raise "no array" unless v.is_a?(Array) if v.size>1 - raise "multiple values" + v.uniq! + raise "not yet implemented: multiple non-equal values "+compound.to_s+" "+v.inspect if v.size>1 + v = v[0] elsif v.size==1 v = v[0] else diff --git a/lib/predictions.rb b/lib/predictions.rb index e73dda6..5850024 100755 --- a/lib/predictions.rb +++ b/lib/predictions.rb @@ -163,19 +163,36 @@ module Lib def percent_correct raise "no classification" unless @feature_type=="classification" return 0 if @num_with_actual_value==0 - return 100 * @num_correct / @num_with_actual_value.to_f + return 100 * @num_correct / (@num_with_actual_value - @num_unpredicted).to_f end def percent_incorrect raise "no classification" unless @feature_type=="classification" return 0 if @num_with_actual_value==0 - return 100 * @num_incorrect / @num_with_actual_value.to_f + return 100 * @num_incorrect / (@num_with_actual_value - @num_unpredicted).to_f end def accuracy return percent_correct / 100.0 end + def weighted_accuracy + raise "no classification" unless @feature_type=="classification" + total = 0 + correct = 0 + (0..@predicted_values.size-1).each do |i| + if @predicted_values[i]!=nil + total += @confidence_values[i] + correct += @confidence_values[i] if @actual_values[i]==@predicted_values[i] + end + end + if total==0 || correct == 0 + return 0 + else + return correct / total + end + end + def percent_unpredicted return 0 if @num_with_actual_value==0 return 100 * @num_unpredicted / @num_with_actual_value.to_f diff --git a/lib/validation_db.rb b/lib/validation_db.rb index 4b852f9..c4cb2a2 100755 --- a/lib/validation_db.rb +++ b/lib/validation_db.rb @@ -18,10 +18,9 @@ module Lib # :classification_statistics VAL_CLASS_PROPS_SINGLE_SUM = [ :num_correct, :num_incorrect, :confusion_matrix ] VAL_CLASS_PROPS_SINGLE_AVG = [ :percent_correct, :percent_incorrect, - :weighted_area_under_roc, :accuracy ] + :weighted_area_under_roc, :accuracy, :weighted_accuracy ] VAL_CLASS_PROPS_SINGLE = VAL_CLASS_PROPS_SINGLE_SUM + VAL_CLASS_PROPS_SINGLE_AVG - # :class_value_statistics VAL_CLASS_PROPS_PER_CLASS_SUM = [ :num_false_positives, :num_false_negatives, :num_true_positives, :num_true_negatives ] diff --git a/reach_reports/reach_service.rb b/reach_reports/reach_service.rb index 5e11dc1..f5b4b12 100755 --- a/reach_reports/reach_service.rb +++ b/reach_reports/reach_service.rb @@ -132,10 +132,12 @@ module ReachReports val_datasets = [] if algorithm - cvs = Lib::Crossvalidation.find_all_uniq({:algorithm_uri => algorithm.uri}) + cvs = Lib::Crossvalidation.find_all_uniq({:algorithm_uri => algorithm.uri, :finished => true}) # PENDING: cv classification/regression hack cvs = cvs.delete_if do |cv| - val = Validation::Validation.first( :all, :conditions => { :crossvalidation_id => cv.id } ) + #val = Validation::Validation.first( :all, :conditions => { :crossvalidation_id => cv.id } ) + val = Validation::Validation.first( :crossvalidation_id => cv.id ) + raise "should not happen: no validations found for crossvalidation "+cv.id.to_s unless val (val.classification_statistics!=nil) != (feature_type=="classification") end @@ -166,11 +168,11 @@ module ReachReports val = YAML.load( OpenTox::RestClientWrapper.get File.join(cv.crossvalidation_uri,"statistics") ) case feature_type when "classification" - lmo << "percent_correct: "+val[:classification_statistics][:percent_correct].to_s - lmo << "weighted AUC: "+val[:classification_statistics][:weighted_area_under_roc].to_s + lmo << "percent_correct: "+val[OT.classificationStatistics][OT.percentCorrect].to_s + lmo << "weighted AUC: "+val[OT.classificationStatistics][OT.weightedAreaUnderRoc].to_s when "regression" - lmo << "root_mean_squared_error: "+val[:regression_statistics][:root_mean_squared_error].to_s - lmo << "r_square "+val[:regression_statistics][:r_square].to_s + lmo << "root_mean_squared_error: "+val[OT.regressionStatistics][OT.rootMeanSquaredError].to_s + lmo << "r_square "+val[OT.regressionStatistics][OT.rSquare].to_s end reports = OpenTox::RestClientWrapper.get File.join(CONFIG[:services]["opentox-validation"],"report/crossvalidation?crossvalidation_uris="+cv.crossvalidation_uri) if reports and reports.chomp.size>0 diff --git a/reach_reports/reach_test.rb b/reach_reports/reach_test.rb index 4f7113b..9a354e7 100755 --- a/reach_reports/reach_test.rb +++ b/reach_reports/reach_test.rb @@ -115,7 +115,15 @@ class ReachTest < Test::Unit::TestCase # puts last_response.body #model_uri = "http://ambit.uni-plovdiv.bg:8080/ambit2/model/173393" - model_uri = "http://local-ot/model/1" + + model_uri = "http://local-ot/majority/class/model/58" +# m = OpenTox::Model::Generic.find(model_uri) +# puts m.metadata[OT.algorithm] if m +# a = OpenTox::Algorithm::Generic.find(m.metadata[OT.algorithm]) +# puts a.metadata.inspect +# exit + +# model_uri = "http://local-ot/model/1" #http://local-ot/majority/class/model/15 #model_uri = "http://local-ot/majority/class/model/15" # model_uri = "http://local-ot/majority/class/model/91" diff --git a/report/report_factory.rb b/report/report_factory.rb index a5234a3..8fa185e 100755 --- a/report/report_factory.rb +++ b/report/report_factory.rb @@ -5,7 +5,7 @@ VAL_ATTR_TRAIN_TEST = [ :model_uri, :training_dataset_uri, :test_dataset_uri, :p VAL_ATTR_CV = [ :algorithm_uri, :dataset_uri, :num_folds, :crossvalidation_fold ] # selected attributes of interest when performing classification -VAL_ATTR_CLASS = [ :percent_correct, :weighted_area_under_roc, +VAL_ATTR_CLASS = [ :accuracy, :weighted_accuracy, :weighted_area_under_roc, :area_under_roc, :f_measure, :true_positive_rate, :true_negative_rate ] VAL_ATTR_REGR = [ :root_mean_squared_error, :mean_absolute_error, :r_square ] diff --git a/report/validation_access.rb b/report/validation_access.rb index 9c45165..03ec0e5 100755 --- a/report/validation_access.rb +++ b/report/validation_access.rb @@ -125,7 +125,6 @@ class Reports::ValidationDB < Reports::ValidationAccess end def feature_type( validation, subjectid=nil ) - raise "subjectid is nil" unless subjectid OpenTox::Model::Generic.new(validation.model_uri).feature_type(subjectid) #get_model(validation).classification? end diff --git a/test/test_examples.rb b/test/test_examples.rb index 192f1a4..29fe8cd 100755 --- a/test/test_examples.rb +++ b/test/test_examples.rb @@ -355,17 +355,87 @@ module ValidationExamples end ######################################################################################################## - class NtuaModel < ModelValidation def initialize - @model_uri = "http://opentox.ntua.gr:4000/model/0d8a9a27-3481-4450-bca1-d420a791de9d" + @model_uri = "http://opentox.ntua.gr:4000/model/0d8a9a27-3481-4450-bca1-d420a791de9d" @test_dataset_uri = "http://apps.ideaconsult.net:8080/ambit2/dataset/54" #@prediction_feature=http://apps.ideaconsult.net:8080/ambit2/feature/22200 end end ######################################################################################################## + + class HamsterTrainingTest < TrainingTestValidation + def initialize +# @test_target_dataset_file = File.new("data/hamster_carcinogenicity.yaml","r") +# @training_dataset_file = File.new("data/hamster_carcinogenicity.train.yaml","r") +# @test_dataset_file = File.new("data/hamster_carcinogenicity.test.yaml","r") + + @test_target_dataset_file = File.new("data/hamster_carcinogenicity.csv","r") + @training_dataset_file = File.new("data/hamster_carcinogenicity.train.csv","r") + @test_dataset_file = File.new("data/hamster_carcinogenicity.test.csv","r") + + + #@prediction_feature = "http://local-ot/toxmodel/feature#Hamster%20Carcinogenicity%20(DSSTOX/CPDB)" + #@prediction_feature = "http://local-ot/dataset/1/feature/hamster_carcinogenicity" + end + end + + class MajorityHamsterTrainingTest < HamsterTrainingTest + def initialize + @algorithm_uri = File.join(CONFIG[:services]["opentox-majority"],"/class/algorithm") + super + end + end + + ######################################################################################################## + + class RepdoseSplit < SplitTestValidation + def initialize + @dataset_file = File.new("data/repdose_classification.csv","r") + end + end + + class LazarRepdoseSplit < RepdoseSplit + def initialize + @algorithm_uri = File.join(CONFIG[:services]["opentox-algorithm"],"lazar") + @algorithm_params = "feature_generation_uri="+File.join(CONFIG[:services]["opentox-algorithm"],"fminer/bbrc") + super + end + end + + class MajorityRepdoseSplit < RepdoseSplit + def initialize + @algorithm_uri = File.join(CONFIG[:services]["opentox-majority"],"/class/algorithm") + super + end + end + + ######################################################################################################## + + class RepdoseCrossValidation < CrossValidation + def initialize + @dataset_file = File.new("data/repdose_classification.csv","r") + end + end + + class LazarRepdoseCrossValidation < RepdoseCrossValidation + def initialize + @algorithm_uri = File.join(CONFIG[:services]["opentox-algorithm"],"lazar") + @algorithm_params = "feature_generation_uri="+File.join(CONFIG[:services]["opentox-algorithm"],"fminer/bbrc") + super + end + end + + class MajorityRepdoseCrossValidation < RepdoseCrossValidation + def initialize + @algorithm_uri = File.join(CONFIG[:services]["opentox-majority"],"/class/algorithm") + super + end + end + + ######################################################################################################## @@list = { "1" => [ LazarHamsterSplit, MajorityHamsterSplit ], @@ -416,6 +486,14 @@ module ValidationExamples "14b" => [ MajorityEPAFHMCrossvalidation ], "15a" => [ NtuaModel ], + + "16" => [ LazarRepdoseSplit, MajorityRepdoseSplit ], + "16a" => [ LazarRepdoseSplit ], + "16b" => [ MajorityRepdoseSplit ], + + "17" => [ LazarRepdoseCrossValidation, MajorityRepdoseCrossValidation ], + "17a" => [ LazarRepdoseCrossValidation ], + "17b" => [ MajorityRepdoseCrossValidation ], } def self.list diff --git a/test/test_examples_util.rb b/test/test_examples_util.rb index 8344594..51cf4cc 100755 --- a/test/test_examples_util.rb +++ b/test/test_examples_util.rb @@ -47,11 +47,11 @@ module ValidationExamples to_compare << v.validation_uri if v.validation_uri and v.validation_error==nil end return nil if to_compare.size < 2 - begin + #begin return validation_post "report/algorithm_comparison",{ :validation_uris => to_compare.join("\n") }, subjectid - rescue => ex - return "error creating comparison report "+ex.message - end + #rescue => ex + #return "error creating comparison report "+ex.message + #end end def self.validation_post(uri, params, subjectid ) @@ -92,9 +92,9 @@ module ValidationExamples def self.wait(uri) if uri.task_uri? task = OpenTox::Task.find(uri.to_s.chomp) - task.wait_for_completion nil,5 + task.wait_for_completion #raise "task failed: "+uri.to_s+", description: '"+task.description.to_s+"'" if task.error? - LOGGER.error "task failed:\n"+task.errorReport.to_yaml if task.error? + LOGGER.error "task failed :\n"+task.to_yaml if task.error? uri = task.result_uri end uri @@ -267,50 +267,50 @@ module ValidationExamples end def delete - begin + #begin if @validation_uri =~ /crossvalidation/ cv = "crossvalidation/" else cv = "" end Util.validation_delete '/'+cv+@validation_uri.split('/')[-1] if @validation_uri - rescue => ex - puts "Could not delete validation: "+ex.message - end - begin + #rescue => ex + #puts "Could not delete validation: "+ex.message + #end + #begin Util.validation_delete '/report/'+report_type+'/'+@report_uri.split('/')[-1] if @report_uri - rescue => ex - puts "Could not delete report:' "+@report_uri+" "+ex.message - end + #rescue => ex + #puts "Could not delete report:' "+@report_uri+" "+ex.message + #end @uploaded_datasets.each do |d| - begin + # begin puts "deleting dataset "+d OpenTox::RestClientWrapper.delete(d,{:subjectid => SUBJECTID}) - rescue => ex - puts "Could not delete dataset:' "+d+" "+ex.message - end +# rescue => ex + #puts "Could not delete dataset:' "+d+" "+ex.message + #end end end def report - begin + #begin @report_uri = Util.validation_post '/report/'+report_type,{:validation_uris => @validation_uri}, @subjectid if @validation_uri Util.validation_get "/report/"+report_uri.split("/")[-2]+"/"+report_uri.split("/")[-1], @subjectid if @report_uri - rescue => ex - puts "could not create report: "+ex.message - raise ex - @report_error = ex.message - end + #rescue => ex + #puts "could not create report: "+ex.message + #raise ex + #@report_error = ex.message + #end end def validate - begin + #begin @validation_uri = Util.validation_post '/'+validation_type, get_params, @subjectid - rescue => ex - puts "could not validate: "+ex.message - @validation_error = ex.message - LOGGER.error ex.message - end + #rescue => ex + #puts "could not validate: "+ex.message + #@validation_error = ex.message + #LOGGER.error ex.message + #end end def compare_yaml_vs_rdf diff --git a/validation/validation_application.rb b/validation/validation_application.rb index b444b3f..9b517cf 100755 --- a/validation/validation_application.rb +++ b/validation/validation_application.rb @@ -549,11 +549,9 @@ get '/:id' do "All validations: "+url_for("/",:full)+"\n"+ "All validation reports: "+url_for("/report/validation",:full) OpenTox.text_to_html validation.to_yaml,related_links,description - when /application\/x-yaml|\*\/\*/ + else #default is yaml content_type "application/x-yaml" validation.to_yaml - else - raise OpenTox::BadRequestError.new "MIME type '"+request.env['HTTP_ACCEPT'].to_s+"' not supported, valid Accept-Headers: \"application/rdf+xml\", \"application/x-yaml\", \"text/html\"." end end diff --git a/validation/validation_format.rb b/validation/validation_format.rb index e72e7ec..f69ceac 100755 --- a/validation/validation_format.rb +++ b/validation/validation_format.rb @@ -10,8 +10,6 @@ module Validation # the hash is directly printed in to_yaml, or added to the owl-structure def get_content_as_hash() - LOGGER.debug self.validation_uri - h = {} (Lib::VAL_PROPS - [:validation_uri]).each do |p| h[p] = self.send(p) diff --git a/validation/validation_test.rb b/validation/validation_test.rb index 26f12e7..27fd6b1 100755 --- a/validation/validation_test.rb +++ b/validation/validation_test.rb @@ -20,10 +20,10 @@ LOGGER.datetime_format = "%Y-%m-%d %H:%M:%S " LOGGER.formatter = Logger::Formatter.new if AA_SERVER - #TEST_USER = "mgtest" - #TEST_PW = "mgpasswd" - TEST_USER = "guest" - TEST_PW = "guest" + TEST_USER = "mgtest" + TEST_PW = "mgpasswd" +# TEST_USER = "guest" +# TEST_PW = "guest" SUBJECTID = OpenTox::Authorization.authenticate(TEST_USER,TEST_PW) raise "could not log in" unless SUBJECTID puts "logged in: "+SUBJECTID.to_s @@ -60,6 +60,18 @@ class ValidationTest < Test::Unit::TestCase begin $test_case = self +# begin +# #OpenTox::RestClientWrapper.get "http://local-ot/validation/runtime-error",{:accept => "application/rdf+xml"} +# puts OpenTox::RestClientWrapper.post "http://opentox.ntua.gr:4000/model/0d8a9a27-3481-4450-bca1-d420a791de9d", +# {:dataset=>"http://apps.ideaconsult.net:8080/ambit2/dataset/54"},{:accept => "text/uri-list", :subjectid => SUBJECTID} +# #puts OpenTox::RestClientWrapper.post "http://opentox.ntua.gr:4000/model/0d8a9a27-3481-4450-bca1-d420a791de9d",{},{:accept => "text/uri-list", :subjectid => "AQIC5wM2LY4SfcwUNX97nTvaSTdYJ+nTUqZsR0UitJ4+jlc=@AAJTSQACMDE=#"} +# rescue => err +# rep = OpenTox::ErrorReport.create(err, "") +# puts rep.to_yaml +# end + + # "http://opentox.ntua.gr:4000/model/0d8a9a27-3481-4450-bca1-d420a791de9d" + # get "/19999",nil,'HTTP_ACCEPT' => "text/html" # exit # @@ -126,23 +138,41 @@ class ValidationTest < Test::Unit::TestCase # m = OpenTox::Model::Generic.find("http://local-ot/model/1323333") # puts m.to_yaml - # post "/validate_datasets",{ - # :test_dataset_uri=>"http://local-ot/dataset/506", - # :prediction_dataset_uri=>"http://local-ot/dataset/526", - # :test_target_dataset_uri=>"http://local-ot/dataset/504", - # :prediction_feature=>"http://local-ot/dataset/504/feature/LC50_mmol", - # :model_uri=>"http://local-ot/model/48"} - # #:regression=>"true"} - ## :classification=>"true"} - # puts last_response.body - - #run_test("13a","http://local-ot/validation/39",nil,false) #,"http://local-ot/validation/28")#,"http://local-ot/validation/394"); +# post "/validate_datasets",{ +# :test_dataset_uri=>"http://local-ot/dataset/390", +# :prediction_dataset_uri=>"http://local-ot/dataset/392", +# :test_target_dataset_uri=>"http://local-ot/dataset/388", +# :prediction_feature=>"http://local-ot/dataset/388/feature/repdose_classification", +# :model_uri=>"http://local-ot/model/31"} +# #:regression=>"true"} +# # :classification=>"true"} +# uri = last_response.body +# val = wait_for_task(uri) +# puts val +# get "/"+val.split("/")[-1] + #run_test("17a") #,"http://local-ot/validation/39",nil,false) #,"http://local-ot/validation/28")#,"http://local-ot/validation/394"); + +# p = { +# :dataset_uri=>"http://local-ot/dataset/388", +# :algorithm_uri => "http://local-ot/majority/class/algorithm", +# :prediction_feature=>"http://local-ot/dataset/388/feature/repdose_classification", +# :num_folds => 2 } + #cv = OpenTox::Crossvalidation.create(p) +# cv = OpenTox::Crossvalidation.find("http://local-ot/validation/crossvalidation/14") +# puts cv.uri +# puts cv.find_or_create_report.uri +# puts cv.summary.inspect + #puts OpenTox::Authorization.list_policy_uris(SUBJECTID).inspect #puts OpenTox::Authorization.list_policy_uris(SUBJECTID).inspect - run_test("15a",nil,nil,false) #,{:dataset_uri=>"http://local-ot/dataset/45", :prediction_feature => "http://local-ot/dataset/45/feature/Hamster%20Carcinogenicity"}) + #run_test("1b") #,{:dataset_uri => "http://local-ot/dataset/313", :prediction_feature => "http://local-ot/dataset/313/feature/repdose_classification"}) + + model = OpenTox::Model::Generic.find("http://local-ot/majority/class/model/58") + OpenTox::QMRFReport.create(model) + #get "/12123123123123123" #get "/chain" @@ -176,9 +206,12 @@ class ValidationTest < Test::Unit::TestCase #prepare_examples #do_test_examples # USES CURL, DO NOT FORGET TO RESTART VALIDATION SERVICE #do_test_examples_ortona - + + rescue => ex + rep = OpenTox::ErrorReport.create(ex, "") + puts rep.to_yaml ensure - OpenTox::Authorization.logout(SUBJECTID) if AA_SERVER + #OpenTox::Authorization.logout(SUBJECTID) if AA_SERVER end end @@ -186,7 +219,7 @@ class ValidationTest < Test::Unit::TestCase Sinatra::Application end - def run_test(select=nil, validation_uri=nil, report_uri=nil, delete=false, overwrite={}) + def run_test(select=nil, overwrite={}, delete=false ) if AA_SERVER && SUBJECTID && delete policies_before = OpenTox::Authorization.list_policy_uris(SUBJECTID) @@ -199,7 +232,6 @@ class ValidationTest < Test::Unit::TestCase ex = v.new ex.subjectid = SUBJECTID - ex.validation_uri = validation_uri overwrite.each do |k,v| ex.send(k.to_s+"=",v) end @@ -208,13 +240,26 @@ class ValidationTest < Test::Unit::TestCase ex.upload_files ex.check_requirements ex.validate + LOGGER.debug "validation done '"+ex.validation_uri.to_s+"'" - puts ex.validation_uri+"?subjectid="+CGI.escape(SUBJECTID) if SUBJECTID and !delete and ex.validation_uri + if !delete and ex.validation_uri + if SUBJECTID + puts ex.validation_uri+"?subjectid="+CGI.escape(SUBJECTID) + else + puts ex.validation_uri + end + end + end - ex.report_uri = report_uri unless ex.report_uri ex.report - puts ex.report_uri+"?subjectid="+CGI.escape(SUBJECTID) if SUBJECTID and !delete and ex.report_uri + if !delete and ex.report_uri + if SUBJECTID + puts ex.report_uri+"?subjectid="+CGI.escape(SUBJECTID) + else + puts ex.report_uri + end + end end ##ex.verify_yaml ##ex.compare_yaml_vs_rdf |