summaryrefslogtreecommitdiff
path: root/lazar.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2010-11-19 16:51:14 +0100
committerChristoph Helma <helma@in-silico.ch>2010-11-19 16:51:14 +0100
commit2d0c76b7838f5f31552a09c54a81e621d1618d2d (patch)
treea1b1a071322dec8739e09d4bf0b369b8235fdf81 /lazar.rb
parent6d9653546115e73b732eaadfcb3d1dbe9ab7d99a (diff)
working predictions for toxcreate
Diffstat (limited to 'lazar.rb')
-rw-r--r--lazar.rb65
1 files changed, 13 insertions, 52 deletions
diff --git a/lazar.rb b/lazar.rb
index 3b8a0cf..8ad9fed 100644
--- a/lazar.rb
+++ b/lazar.rb
@@ -1,8 +1,3 @@
-# R integration
-# workaround to initialize R non-interactively (former rinruby versions did this by default)
-# avoids compiling R with X
-R = nil
-require "rinruby"
require "haml"
#require "lazar-helper"
@@ -36,23 +31,6 @@ get '/:id/?' do
end
end
-=begin
-get '/:id/algorithm/?' do
- response['Content-Type'] = 'text/plain'
- YAML.load(ModelStore.get(params[:id]).yaml).algorithm
-end
-
-get '/:id/trainingDataset/?' do
- response['Content-Type'] = 'text/plain'
- YAML.load(ModelStore.get(params[:id]).yaml).trainingDataset
-end
-
-get '/:id/feature_dataset/?' do
- response['Content-Type'] = 'text/plain'
- YAML.load(ModelStore.get(params[:id]).yaml).feature_dataset_uri
-end
-=end
-
# Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model
# @param [Body] lazar Model representation in YAML format
# @return [String] Model URI
@@ -71,47 +49,30 @@ end
# @param [optional,String] dataset_uri URI of the dataset to be predicted
# @param [optional,String] compound_uri URI of the compound to be predicted
# @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml`
-# @return [text/uri-list,application/rdf+xml,application/x-yaml] URI of prediction task (dataset prediction) or prediction in requested representation
+# @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction)
post '/:id/?' do
- start = Time.now
@lazar = YAML.load ModelStore.get(params[:id]).yaml
halt 404, "Model #{params[:id]} does not exist." unless @lazar
halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri]
- s = Time.now
+ response['Content-Type'] = 'text/uri-list'
if compound_uri
- #begin
- @prediction = @lazar.predict(compound_uri,true)
- #rescue
- #LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} "
- #halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed."
- #end
- #accept = request.env['HTTP_ACCEPT']
- #accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil?
- LOGGER.debug "Total: #{Time.now - start} seconds"
- #case accept
- #when /yaml/
- @prediction.uri
- #else # RestClientWrapper does not send accept header
- #when /application\/rdf\+xml/
- #@prediction.to_rdfxml
- #else
- #halt 400, "MIME type \"#{request.env['HTTP_ACCEPT']}\" not supported."
- #end
+ begin
+ cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri)
+ return cache.dataset_uri if cache
+ prediction_uri = @lazar.predict(compound_uri,true).uri
+ PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri)
+ prediction_uri
+ rescue
+ LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} "
+ halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed."
+ end
elsif dataset_uri
- response['Content-Type'] = 'text/uri-list'
task_uri = OpenTox::Task.as_task("Predict dataset",url_for("/#{lazar.id}", :full)) do
- OpenTox::Dataset.find(dataset_uri).compounds.each do |compound_uri|
- #begin
- predict(compound_uri,true)
- #rescue
- #LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} "
- #end
- end
- @prediction.save
+ @lazar.predict(dataset_uri).uri
end
halt 202,task_uri
end