summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMartin Gütlein <martin.guetlein@gmail.com>2010-03-25 20:04:39 +0100
committerMartin Gütlein <martin.guetlein@gmail.com>2010-03-25 20:04:39 +0100
commit675980f769955698209ac723eaa4d34eba9c3204 (patch)
treea0cf1ef3b5b1adbe5b54a5cfb4f5798f18bf5a0c /lib
parent14d2a68564061d63166cd409bf4fd30dc841d2b8 (diff)
adjust to new ruby api wrapper version
Diffstat (limited to 'lib')
-rw-r--r--lib/ot_predictions.rb88
-rw-r--r--lib/rdf_provider.rb2
-rw-r--r--lib/test_util.rb15
-rw-r--r--lib/wrapper.rb310
4 files changed, 48 insertions, 367 deletions
diff --git a/lib/ot_predictions.rb b/lib/ot_predictions.rb
index 2098ab1..d81bd29 100644
--- a/lib/ot_predictions.rb
+++ b/lib/ot_predictions.rb
@@ -5,6 +5,8 @@ module Lib
class OTPredictions < Predictions
+ CHECK_VALUES = ENV['RACK_ENV']=="test"
+
def identifier(instance_index)
return compound(instance_index)
end
@@ -26,26 +28,31 @@ module Lib
prediction_dataset = OpenTox::Dataset.find prediction_dataset_uri
raise "test dataset not found: '"+test_dataset_uri.to_s+"'" unless test_dataset
raise "prediction dataset not found: '"+prediction_dataset_uri.to_s+"'" unless prediction_dataset
+ raise "test dataset feature not found: '"+prediction_feature+"', available features: "+test_dataset.features.inspect if test_dataset.features.index(prediction_feature)==nil
+ raise "prediction dataset feature not found: '"+predicted_variable+"', available features: "+prediction_dataset.features.inspect if prediction_dataset.features.index(predicted_variable)==nil
+
+ class_values = OpenTox::Feature.domain(prediction_feature)
+
+ @compounds = test_dataset.compounds
+ raise "test dataset is empty" unless @compounds.size>0
+ raise "more predicted than test compounds test:"+@compounds.size.to_s+" < prediction:"+
+ prediction_dataset.compounds.size.to_s if @compounds.size < prediction_dataset.compounds.size
- class_values = OpenTox::Feature.range(prediction_feature)
+ if CHECK_VALUES
+ prediction_dataset.compounds.each do |c|
+ raise "predicted compound not found in test dataset" if @compounds.index(c)==nil
+ end
+ end
actual_values = []
- @compounds = []
- test_dataset.data.each do |compound,featuresValues|
+ @compounds.each do |c|
- @compounds.push compound
- #@compounds << (OpenTox::Compound.new :uri=>compound.to_s).smiles
-
- value = nil
- LOGGER.warn "value is hash, but no feature value '"+prediction_feature.to_s+
- "', hash: '"+featuresValues.inspect+"'" if featuresValues.is_a?(Hash) and !featuresValues.has_key?(prediction_feature)
- value = featuresValues[prediction_feature] if featuresValues.is_a?(Hash)
- value = value[0] if value.is_a?(Array) and value.length == 1
- value = nil if value.to_s.size==0 # PENDING: hack to avoid illegal boolean values
+ value = test_dataset.get_value(c, prediction_feature)
if is_classification
value = value.to_s unless value==nil
- raise "illegal class_value of actual value "+value.to_s+" class: "+value.class.to_s unless value==nil or class_values.index(value)!=nil
+ raise "illegal class_value of actual value "+value.to_s+" class: "+
+ value.class.to_s unless value==nil or class_values.index(value)!=nil
actual_values.push class_values.index(value)
else
begin
@@ -58,50 +65,25 @@ module Lib
end
end
- raise "test dataset is empty" unless @compounds.size>0
-
- predicted_values = Array.new(actual_values.size)
- confidence_values = Array.new(actual_values.size)
-
- prediction_dataset.data.each do |compound,featuresValues|
-
- index = @compounds.index(compound)
- raise "compound "+compound.to_s+" not found in\n"+@compounds.inspect if index==nil
-
- value = nil
- LOGGER.warn "value is hash, but no feature value '"+predicted_variable.to_s+
- "', hash: '"+featuresValues.inspect+"'" if featuresValues.is_a?(Hash) and !featuresValues.has_key?(predicted_variable)
- value = featuresValues[predicted_variable] if featuresValues.is_a?(Hash)
- value = value[0] if value.is_a?(Array) and value.length == 1
- value = nil if value.to_s.size==0
-
- if is_classification
-
- ### PENDING ####
- confidence = nil
- if value.is_a?(Hash)
- confidence = value["confidence"].to_f.abs if value.has_key?("confidence")
- value = value["classification"].to_s if value.has_key?("classification")
- else
- value = value.to_s unless value==nil # PENDING: hack to avoid illegal boolean values
- end
- ################
-
- raise "illegal class_value of predicted value "+value.to_s+" class: "+value.class.to_s unless value==nil or class_values.index(value)!=nil
- predicted_values[index] = class_values.index(value)
- confidence_values[index] = confidence if confidence!=nil
+ predicted_values = []
+ confidence_values = []
+ @compounds.each do |c|
+ if prediction_dataset.compounds.index(c)==nil
+ predicted_values << nil
+ confidence_values << nil
else
- begin
- value = value.to_f unless value==nil or value.is_a?(Numeric)
- rescue
- LOGGER.warn "no numeric value for regression: '"+value.to_s+"'"
- value = nil
+ if is_classification
+ value = prediction_dataset.get_predicted_class(c, predicted_variable)
+ value = value.to_s unless value==nil
+ raise "illegal class_value of predicted value "+value.to_s+" class: "+value.class.to_s unless value==nil or class_values.index(value)!=nil
+ predicted_values << class_values.index(value)
+ confidence_values << prediction_dataset.get_prediction_confidence(c, predicted_variable)
+ else
+ raise "TODO regression"
end
- predicted_values[index] = value
end
- index += 1
end
-
+
super(predicted_values, actual_values, confidence_values, is_classification, class_values)
raise "illegal num compounds "+num_info if @compounds.size != @predicted_values.size
end
diff --git a/lib/rdf_provider.rb b/lib/rdf_provider.rb
index 1715566..ae45b79 100644
--- a/lib/rdf_provider.rb
+++ b/lib/rdf_provider.rb
@@ -47,7 +47,7 @@ module Lib
end
class HashToOwl
- include OpenTox::Owl
+ #include OpenTox::Owl
def self.to_rdf( rdf_provider )
owl = HashToOwl.new()
diff --git a/lib/test_util.rb b/lib/test_util.rb
index aa13937..fd75e66 100644
--- a/lib/test_util.rb
+++ b/lib/test_util.rb
@@ -7,11 +7,20 @@ module Lib
# updloads a dataset
def upload_data(ws, file)
+
+ case file.path
+ when /yaml$/
+ type = "text/x-yaml"
+ when /owl$/
+ type = "application/rdf+xml"
+ else
+ raise "unknown type for file: "+file.path.to_s
+ end
data = File.read(file.path)
- task_uri = RestClient.post ws, data, :content_type => "application/rdf+xml"
- print "uploading dataset "+task_uri.to_s+" - "
- data_uri = OpenTox::Task.find(task_uri).wait_for_resource
+ task_uri = RestClient.post ws, data, :content_type => type
+ data_uri = task_uri
+ #data_uri = OpenTox::Task.find(task_uri).wait_for_resource
puts "done: "+data_uri.to_s
add_resource(data_uri)
return data_uri
diff --git a/lib/wrapper.rb b/lib/wrapper.rb
deleted file mode 100644
index d620651..0000000
--- a/lib/wrapper.rb
+++ /dev/null
@@ -1,310 +0,0 @@
-
-class String
-
- def is_uri?
- begin
- URI::parse(self)
- rescue URI::InvalidURIError
- false
- end
- end
-
-end
-
-module OpenTox
-
- class AmbitTask
- include Owl
-
-
-# def wait_for_completion
-# until self.completed? or self.failed?
-# sleep 1
-# end
-# end
-
- def running?
- me = @model.subject(RDF['type'],OT["Task"])
- status = @model.object(me, OT['hasStatus']).literal.value.to_s
- puts status
- status=="Running"
- end
-
- def reload
- data = ""
- IO.popen("curl -i -X GET "+uri.to_s+" 2> /dev/null") do |f|
- while line = f.gets
- data += line
- end
- end
- #data = OpenTox::RestClientWrapper.get uri
-
- puts "reload "+data.to_s
- self.rdf = data
- end
-
- def wait_while_running
- while running?
- sleep 1
- reload
- end
- end
-
- def self.from_uri(uri)
-
- begin
- t = AmbitTask.new #(uri)
- t.uri = uri
- data = OpenTox::RestClientWrapper.get uri
- puts "loaded ambit task "+data.to_s
- t.rdf = data
- #t.running?
- t
- rescue => ex
- raise ex
- #=> ex
- #puts "error "+ex.message.to_s
- nil
- end
-
- end
-
- end
-
- class Task
-
-
- def self.as_task
-
- task = OpenTox::Task.create
- pid = Spork.spork(:logger => LOGGER) do
- task.started
- LOGGER.debug "task #{task.uri} started #{Time.now}"
- begin
- result = yield
- rescue => ex
- raise ex
- LOGGER.error ex.message
- task.failed
- break
- end
- task.completed(result)
- end
- LOGGER.debug "task PID: " + pid.to_s
- task.pid = pid
- task.uri
-
- end
-
-
- def wait_for_resource
- wait_for_completion
- if failed?
- LOGGER.error "task failed "+uri.to_s
- return nil
- end
- return resource
- end
-
- end
-
- module Feature
- def self.range( feature_uri )
- if feature_uri =~ /ambit/
- return nil
- else
- return ["true", "false"]
- end
- end
- end
-
- module Model
- class PredictionModel
- include Owl
-
- #attr_reader :uri
-
- def self.build( algorithm_uri, algorithm_parms )
- model_task_uri = OpenTox::RestClientWrapper.post algorithm_uri,algorithm_parms
- model_uri = OpenTox::Task.find(model_task_uri).wait_for_resource.to_s
- if model_uri
- return PredictionModel.find(model_uri)
- else
- return nil
- end
- end
-
- def self.find( uri )
- begin
- #RestClient.get(uri,:accept => "application/rdf+xml")
- #PredictionModel.new(uri)
-
- model = PredictionModel.new #(uri)
- model.uri= uri
- data = RestClient.get(uri,:accept => "application/rdf+xml")
- #LOGGER.debug "creating model from data: "+data.to_s
- model.rdf = data
- raise "uri not set: '"+model.uri+"'" unless model.uri.to_s.size>5
- model
-
- rescue => ex
- LOGGER.error "could not get model with uri: '"+uri+"', error-msg: "+ex.message.to_s
- raise ex
- #=> ex
- #puts "error "+ex.message.to_s
- nil
- end
- end
-
- def predict_dataset( dataset_uri )
- LOGGER.debug "model "+@uri.to_s+" predicts dataset: "+dataset_uri.to_s
- #prediction_task_uri = RestClientWrapper.post @uri,{:dataset_uri => dataset_uri}
- #puts prediction_task_uri
- #return OpenTox::Task.find(prediction_task_uri).wait_for_resource
-
- #res = RestClientWrapper.post @uri,{:dataset_uri => dataset_uri}
-
- res = ""
- IO.popen("curl -X POST -d dataset_uri='"+dataset_uri+"' "+@uri.to_s+" 2> /dev/null") do |f|
- while line = f.gets
- res += line
- end
- end
- puts "done "+res.to_s
-
- raise "neither prediction-dataset or task-uri: "+res.to_s unless res.to_s.is_uri?
-
- #HACK
- if res.to_s =~ /dataset.*\/[0-9]+$/ # lazar
- return res
- elsif res.to_s =~ /\/task\// #pantelis
- ambitTask = OpenTox::AmbitTask.from_uri(res.to_s)
- ambitTask.wait_while_running
- raise "done"
- else
- raise "not sure about prediction result: "+res.to_s
- end
- end
-
- def classification?
- #TODO
- return true
- end
-
- def predictedVariables
-
-# puts OT[self.owl_class]
-# puts @model.subject(RDF['type'],OT[self.owl_class])
-
-
-# me = @model.subject(RDF['type'],OT[self.owl_class])
-# puts "title "+@model.object(me, DC['title']).to_s
-# puts "pred "+@model.object(me, DC['predictedVariables']).to_s
-# puts "rights "+@model.object(me, DC['rights']).to_s
-#
-# puts "title "+@model.object(me, OT['title']).to_s
-# puts "pred "+@model.object(me, OT['predictedVariables']).to_s
-# puts "rights "+@model.object(me, OT['rights']).to_s
-#
-#
-# puts "1 "+@model.subjects(RDF['type'], OT['Feature']).each{|s| s.inspect.to_s}.join("\n")
-# puts "f "+@model.subjects(RDF['type'], OT['predictedVariables']).each{|s| s.inspect.to_s}.join("\n")
-# puts @model.object(me, OT['Feature']).to_s
-#
-# puts @model.subjects(RDF['type'],OT[self.owl_class])
-# puts identifier
-# puts title
-# puts @model.to_s
-# puts @uri
-# puts @model.get_resource(@uri)
-# puts "XXX"
-#
-# @model.subjects(RDF['type'], OT['Feature']).each do |s|
-# puts "s "+s.to_s
-# puts "o "+@model.object(s, RDF['type']).to_s
-# @model.subjects(OT['independentVariables'],s).each do |s2|
-# puts "s2a "+s2.to_s
-# end
-# @model.subjects(OT['dependentVariables'],s).each do |s2|
-# puts "s2b "+s2.to_s
-# end
-# @model.subjects(OT['predictedVariables'],s).each do |s2|
-# puts "s2c "+s2.to_s
-# end
-# end
-
- me = @model.subject(RDF['type'],OT[self.owl_class])
- return @model.object(me, OT['predictedVariables']).uri.to_s
-
- #LOGGER.debug "getting lazar model"
- #m = OpenTox::Model::Lazar.find(@uri)
- #LOGGER.debug "getting lazar model DONE"
- #LOGGER.debug "getting predict values"
- #p = m.predictedVariables
- #LOGGER.debug "getting predict values DONE"
- #return p
- end
-
- def destroy
- RestClientWrapper.delete @uri
- end
-
- #protected
- #def initialize(uri)
- # @uri = uri
- #end
- end
-
- end
-
- module RestClientWrapper
-
- def self.get(uri, headers=nil)
- execute( "get", uri, nil, headers )
- end
-
- def self.post(uri, payload=nil, headers=nil)
- execute( "post", uri, payload, headers )
- end
-
- def self.delete(uri, headers=nil)
- execute( "delete", uri, nil, headers )
- end
-
- private
- def self.execute( rest_call, uri, payload, headers )
-
- do_halt 400,"uri is null",uri,payload,headers unless uri
- begin
- if payload
- RestClient.send(rest_call, uri, payload, headers)
- else
- RestClient.send(rest_call, uri, headers)
- end
- rescue RestClient::RequestFailed, RestClient::RequestTimeout => ex
- do_halt 502,ex.message,uri,payload,headers
- rescue SocketError, RestClient::ResourceNotFound => ex
- do_halt 400,ex.message,uri,payload,headers
- rescue Exception => ex
- do_halt 500,"add error '"+ex.class.to_s+"'' to rescue in OpenTox::RestClientWrapper::execute(), msg: '"+ex.message.to_s+"'",uri,payload,headers
- end
- end
-
- def self.do_halt(status, msg, uri, payload, headers)
-
- message = msg+""
- message += ", uri: '"+uri.to_s+"'" if uri
- message += ", payload: '"+payload.inspect+"'" if payload
- message += ", headers: '"+headers.inspect+"'" if headers
-
- if defined?(halt)
- halt(status,message)
- elsif defined?($sinatra)
- $sinatra.halt(status,message)
- else
- raise "halt '"+status.to_s+"' '"+message+"'"
- end
- end
- end
-
-end \ No newline at end of file